HOW TO: round float numbers?

N

Nobody

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

ES Kim

Nobody said:
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);
 
J

John Harrison

ES Kim said:
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
 
V

Vyacheslav Kononenko

ES Kim said:
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);
What 4.45 should be rounded to? If 5 then you may use 0.5555555555555555
instead.

Regards,
Slava
 
H

Howard

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
 
J

John Harrison

Howard said:
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
 
H

Howard

John Harrison said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top