math - Getting the decimal part of a double in Swift -


i'm trying separate decimal , integer parts of double in swift. i've tried number of approaches run 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 

is there way 0.5678 instead of 0.567800000000034 without converting number string?

without converting string, can round number of decimal places 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 be

0.5678


Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

Rendering JButton to get the JCheckBox behavior in a JTable by using images does not update my table -