I'm trying to separate the decimal and integer parts of a double in swift. I've tried a number of approaches but they all run into the same issue...
let x:Double = 1234.5678
let n1:Double = x % 1.0 // n1 = 0.567800000000034
let n2:Double = x - 1234.0 // same result
let n3:Double = modf(x, &integer) // same result
Without converting it to a string, you can round up to a number of decimal places like this:
let x:Double = 1234.5678 let numberOfPlaces:Double = 4.0 let powerOfTen:Double = pow(10.0, numberOfPlaces) let targetedDecimalPlaces:Double = round((x % 1.0) * powerOfTen) / powerOfTen
Your output would be