 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Nobody Guest
|
Posted: Mon Sep 27, 2004 6:59 am Post subject: 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.
|
|
| Back to top |
|
 |
ES Kim Guest
|
Posted: Mon Sep 27, 2004 7:06 am Post subject: Re: HOW TO: round float numbers? |
|
|
"Nobody" <nobody (AT) cox (DOT) net> wrote
| Quote: | 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
|
|
| Back to top |
|
 |
John Harrison Guest
|
Posted: Mon Sep 27, 2004 7:29 am Post subject: Re: HOW TO: round float numbers? |
|
|
"ES Kim" <no (AT) spam (DOT) mail> wrote
| Quote: | "Nobody" <nobody (AT) cox (DOT) net> wrote in message
news:XSO5d.114888$yh.83871 (AT) fed1read05 (DOT) ..
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
|
|
| Back to top |
|
 |
Vyacheslav Kononenko Guest
|
Posted: Mon Sep 27, 2004 6:16 pm Post subject: Re: HOW TO: round float numbers? |
|
|
"ES Kim" <no (AT) spam (DOT) mail> wrote
| Quote: | "Nobody" <nobody (AT) cox (DOT) net> wrote in message
news:XSO5d.114888$yh.83871 (AT) fed1read05 (DOT) ..
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
|
|
| Back to top |
|
 |
Howard Guest
|
Posted: Mon Sep 27, 2004 8:28 pm Post subject: Re: HOW TO: round float numbers? |
|
|
| Quote: | 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
|
|
| Back to top |
|
 |
John Harrison Guest
|
Posted: Tue Sep 28, 2004 6:43 am Post subject: Re: HOW TO: round float numbers? |
|
|
"Howard" <alicebt (AT) hotmail (DOT) com> wrote
| Quote: | 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
|
|
| Back to top |
|
 |
Howard Guest
|
Posted: Tue Sep 28, 2004 2:40 pm Post subject: Re: HOW TO: round float numbers? |
|
|
"John Harrison" <john_andronicus (AT) hotmail (DOT) com> wrote
| Quote: |
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
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|