ios - Swift roundf not working with float value -


let amount:float = 2.235 print("\(roundf(self.amounttax * 100) / 100)") 

it returns 2.23

but should 2.24

the result 2.23 because amount * 100 223.5 , rounding of 223 (because 2.235 has no exact representation, 2.234999999999) , , divided 100 results in 2.23.

you may want use ceilf unction instead:

print("(ceilf(amount * 100) / 100)") 

this playground result may give more understanding: enter image description here


Comments

Popular posts from this blog

c# SetCompatibleTextRenderingDefault must be called before the first -

c++ - Fill runtime data at compile time with templates -

C#.NET Oracle.ManagedDataAccess ConfigSchema.xsd -