473,242 Members | 1,479 Online

# HOW TO: round float numbers?

How does one round a float? ie... 4.4 returns 4, while 4.5 returns 5.

I see the floor and ceiling functions, but that floor would take 4.4 and
return 4 and ceiling would return 5. I guess I could find the part after the
decimal, but there doesn't seem to be a way to do that either. Other then
subtracting the floor and multiplying by 10 and taking the floor of that and
checking if its 5 or higher, but that seems really lame. There has to be a
better way.
Jul 22 '05 #1
6 11322
"Nobody" <no****@cox.net> wrote in message
How does one round a float? ie... 4.4 returns 4, while 4.5 returns 5.

I see the floor and ceiling functions, but that floor would take 4.4 and
return 4 and ceiling would return 5. I guess I could find the part after the
decimal, but there doesn't seem to be a way to do that either. Other then
subtracting the floor and multiplying by 10 and taking the floor of that and
checking if its 5 or higher, but that seems really lame. There has to be a
better way.

Here is a simple way: you add 0.5 to the float number and floor it.

float f;
int i = static_cast<int>(f + 0.5);

--
ES Kim
Jul 22 '05 #2

"ES Kim" <no@spam.mail> wrote in message
news:cj**********@news1.kornet.net...
"Nobody" <no****@cox.net> wrote in message
How does one round a float? ie... 4.4 returns 4, while 4.5 returns 5.

I see the floor and ceiling functions, but that floor would take 4.4 and
return 4 and ceiling would return 5. I guess I could find the part after
the
decimal, but there doesn't seem to be a way to do that either. Other then
subtracting the floor and multiplying by 10 and taking the floor of that
and
checking if its 5 or higher, but that seems really lame. There has to be
a
better way.

Here is a simple way: you add 0.5 to the float number and floor it.

float f;
int i = static_cast<int>(f + 0.5);

But that doesn't work for negative numbers.

Here's a better way

inline int round(double x) { return static_cast<int>(x + x > 0.0 ? +0.5
: -0.5); }

This is OK for simple use, but it doesn't check for overflow.

john

Jul 22 '05 #3
"ES Kim" <no@spam.mail> wrote in message
news:cj**********@news1.kornet.net...
"Nobody" <no****@cox.net> wrote in message
How does one round a float? ie... 4.4 returns 4, while 4.5 returns 5.

I see the floor and ceiling functions, but that floor would take 4.4 and
return 4 and ceiling would return 5. I guess I could find the part after the decimal, but there doesn't seem to be a way to do that either. Other then subtracting the floor and multiplying by 10 and taking the floor of that and checking if its 5 or higher, but that seems really lame. There has to be a better way.

Here is a simple way: you add 0.5 to the float number and floor it.

float f;
int i = static_cast<int>(f + 0.5);

--
ES Kim

What 4.45 should be rounded to? If 5 then you may use 0.5555555555555555

Regards,
Slava
Jul 22 '05 #4
> > ES Kim

What 4.45 should be rounded to? If 5 then you may use 0.5555555555555555

Using the "conventional" method of rounding (i.e., not banker's rounding),
4.45 would round to 4, because it's less than 4.5. You don't round the last
digit, then the previous one, then the previous one, etc. You simply round
up for fractions greater or equal to .5. (Assuming rounding to a whole
number, not to a specific decimal place, obviously.)

-Howard

Jul 22 '05 #5

"Howard" <al*****@hotmail.com> wrote in message
news:LJ*********************@bgtnsc05-news.ops.worldnet.att.net...
> ES Kim
>

What 4.45 should be rounded to? If 5 then you may use 0.5555555555555555

Using the "conventional" method of rounding (i.e., not banker's rounding),
4.45 would round to 4, because it's less than 4.5. You don't round the
last
digit, then the previous one, then the previous one, etc. You simply
round
up for fractions greater or equal to .5. (Assuming rounding to a whole
number, not to a specific decimal place, obviously.)

-Howard

I've never heard of banker's rounding. When is it used and why?

john
Jul 22 '05 #6

"John Harrison" <jo*************@hotmail.com> wrote in message
news:2r*************@uni-berlin.de...

I've never heard of banker's rounding. When is it used and why?

john

Bankers' rounding is when you always round to the nearest even penny. So
you'd round 4.5 cents to 4 cents, but 5.5 cents to 6 cents. The reason for
it is because banks tend to add up a lot of (already rounded) values, and a
good deal of those are half-cent values prior to rounding. Using normal
rounding, the half-cent values would always round up, resulting in skewing
of the final total upwards. Using bankers' rounding, the final result is
much closer to the value you'd get by adding the un-rounded values (and then
rounding the final result). So it's used to make their totals more
accurate, basically.

-Howard
Jul 22 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.