# round .5

Show 80 post(s) from this thread on one page
Page 2 of 2 First 12
• 06-12-2009
ಠ_ಠ
Quote:

Originally Posted by iMalc
The answer with rounding should be 1.56, not 1.55

times 100

1.5555 * 100 = 155.55

round

156

divide by 100

1.56
• 06-12-2009
tabstop
Quote:

Originally Posted by iMalc
The answer with rounding should be 1.56, not 1.55

And conveniently, that's what you get. (Or at least that's what I got, when I did it.)
• 06-12-2009
brewbuck
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.
• 06-13-2009
iMalc
Bleh - brainfart. For ome reason I thought it didn't mention rounding.
• 06-23-2009
m37h0d
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;   } }```
i don't have a need for rounding to a particular number of decimal points, just to intelligently convert floats into ints.

this seems to do the trick, but i thought i'd throw it up in case someone can spot a problem.
Show 80 post(s) from this thread on one page
Page 2 of 2 First 12