I would like to round a float in swift but everything I have found tells you to convert it in a string. Is there a way to convert the float itself?
Example: 14.14910001 -> 14.15
You can use round()
with a "scale factor" of 100:
let x = 14.14910001
let y = round(100.0 * x) / 100.0
println(y) // 14.15
But you should note that a binary floating number cannot
represent a number like 14.15 exactly, so this will give the nearest
Double
approximation of 14.15.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With