times 100
1.5555 * 100 = 155.55
round
156
divide by 100
1.56
Printable View
And of course, it should be noted that due to the inaccuracy of floats, you still might not get exactly the answer you expect.
For instance, round( 1.234 * 100.0 ) / 100.0 might give 1.229999999, which would be printed as 1.22 if you are limiting the number of digits.
But that's just the way the cookie crumbles.
Bleh - brainfart. For ome reason I thought it didn't mention rounding.
i don't have a need for rounding to a particular number of decimal points, just to intelligently convert floats into ints.Code:int round(double f)
{
unsigned int i = fabs(f*2);
if(i&1)
{
if(f>0)
{
return (int)++f;
}
return (int)--f;
}
else
{
return (int)f;
}
}
this seems to do the trick, but i thought i'd throw it up in case someone can spot a problem.