How to round a floating point number???
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
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.
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
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
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement