I have a float value as float s = 1.270001
. I need to get two digits as output i.e 1.27
. But when we use %0.02f
it gives output as 1.28
How to get two digits after decimal point
ex: 1.16 as 1.16
only
`1.89 as 1.89` only but not `1.90`
I have a float value as float s = 1.270001
. I need to get two digits as output i.e 1.27
. But when we use %0.02f
it gives output as 1.28
How to get two digits after decimal point
ex: 1.16 as 1.16
only
`1.89 as 1.89` only but not `1.90`
You can do it by using NumberFormatter
let formatter = NumberFormatter()
formatter.locale = Locale.current
formatter.numberStyle = .decimal
formatter.maximumFractionDigits = 2
formatter.roundingMode = .floor
let stringValue = formatter.string(from:1.270001)!
print(stringValue)
Output:
1.27