PDA

View Full Version : Rounding a float to X decimal places



qlands
22nd June 2010, 16:22
Hi,

How can I round a float number like 0.00877193 to 0.01?

Thanks,
Carlos.

high_flyer
22nd June 2010, 16:35
1. multiply by 10 until you have a result > 0,and remember how many iterations you needed (power of10)
2. multiply your input number by 10 in the power of iterations-1, and use ceil() on the result. (in this case it will give you 1.0)
3. divide back by 10 in the power of iterations-1.

SixDegrees
22nd June 2010, 22:02
Or, to use your example's case, add 0.005 and simply truncate at the second decimal place.