@Rashmi solution provides a nicely rounded display of a floating point value.
It does not change the value of the original number.
If one wants to round a floating point value to the nearest 0.01
use round()
#include <math.h>
double d = 1.2345;
d = round(d * 100.0)/100.0;
Notes:
Due to FP limitations, the rounded value may not be exactly a multiple of 0.01
, but will be the closest FP number a given platform allows.
When d
is very close to x.xx5, (x is various digits 0-9) d * 100.0
introduces a rounding in the product before the round()
call. Code may round the wrong way.