468,773 Members | 2,517 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,773 developers. It's quick & easy.

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 11070
"Nobody" <no****@cox.net> wrote in message
news:XSO5d.114888$yh.83871@fed1read05...
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
news:XSO5d.114888$yh.83871@fed1read05...
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
news:XSO5d.114888$yh.83871@fed1read05...
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
instead.

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

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


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
instead.


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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Jason Tesser | last post: by
5 posts views Thread by Vio | last post: by
8 posts views Thread by David Corby | last post: by
2 posts views Thread by Steve | last post: by
16 posts views Thread by homa | last post: by
2 posts views Thread by Marin | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.