Alex Rivera | Logout

Limit floating point precision?

Asked 2010-08-01T21:29:09.683
16

Is there a way to round floating points to 2 points? E.g.: 3576.7675745342556 becomes 3576.76.

Edit
Report

1 Answer

21
round(x * 100) / 100.0

If you must keep things floats:

roundf(x * 100) / 100.0

Flexible version using standard library functions:

double GetFloatPrecision(double value, double precision)
{
    return (floor((value * pow(10, precision) + 0.5)) / pow(10, precision)); 
}
answered 2010-08-01T21:29:59.727

Your Answer