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

user interface - how to replace an ongoing process of image capture from another process call over the same ImageLabel in python's GUI TKinter -

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

javascript - Restarting Supervisor and effect on FlaskSocketIO -