I have a video and I need to get the seconds and milliseconds separated and the milliseconds rounded down to the nearest tenth. If the video duration starts off at 4.333333333333333 secs I use floor(num * 10) / 10
to get it to 4.3. When I try to get the remainder by itself using .truncatingRemainder(dividingBy: 1)
and/or modf(num).1
I keep getting 0.2999999999999998 instead of 0.3. Why is this happening?
let mixCompositionDuration = CMTimeGetSeconds(mixComposition.duration)
print("mixCompositionDuration: ", mixCompositionDuration) // 4.333333333333333
let dubl = Double(mixCompositionDuration)
print("dubl: ", dubl) // 4.333333333333333
let mixCompositionRoundDown = floor(dubl * 10) / 10
print("mixCompositionRoundDown: ", mixCompositionRoundDown) // 4.3
let remainder = mixCompositionRoundDown.truncatingRemainder(dividingBy: 1)
print("remainder: ", remainder) // 0.2999999999999998
let mod = modf(mixCompositionRoundDown)
print("mod: ", mod) // (4.0, 0.2999999999999998)
let modRemainder = mod.1
print("modRemainder: ", modRemainder) // 0.2999999999999998