Advertisement

How to round a floating point number???

Started by April 05, 2000 03:46 AM
2 comments, last by Void 24 years, 8 months ago
Hi, anyone knows how to write a function to round a floating point number to a specified decimal accuracy??.. thanks..
Yeah,

add 0.5f and then cast to an integer.

rounded integer = (int)(floating point number + 0.5f)

(you should use the c++ style cast when you can ofcourse, but this should work too).

Jaap Suter
____________________________Mmmm, I''ll have to think of one.
Advertisement
thanks.. I got it..

float ROUND(const float value, const int accuracy)
{
double integer, fraction;

// get fraction and integer components
fraction = modf(value, &integer);

return(float(integer + (float(int(fraction*pow(10,accuracy)))) / pow(10, accuracy) ) );
} // end float ROUND
Oops, It seems I didn''t read your original question very well. I only read the topic and gave an answer on that. Sorry.

It seems like you found the answer somewhere else already.

Jaap Suter
____________________________Mmmm, I''ll have to think of one.

This topic is closed to new replies.

Advertisement