473,242 Members | 1,479 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,242 software developers and data experts.

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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Jason Tesser | last post by:
I am using Rekall which uses Python as it's scripting language and I have a question about Python. I am developing a POS system and am working on the checkout form. I pass a parameter named...
5
by: Vio | last post by:
I need to test for equality between simple 2 decimal numbers. For example: if (10 + 15.99) == 25.99: do some stuff... The preceding sentence should be TRUE, but to Python it appears FALSE....
8
by: David Corby | last post by:
Hi everybody, I've got a problem. I'm trying to round a double to a particular number of significant digits, in this case 5, but I can't figure out a way around getting _exactly_ what I want...
2
by: Steve | last post by:
I have float value, let say 123.456789. How can I round it to two decimal digits after decimal point? For example: 123.456789 -> 123.45 It should be some simple way ... Thanks
16
by: homa | last post by:
Hi! How do I do to make t-sql not rounding the result that i returned? For example: 0.9616458*60 = 57,698748 (in any calculator) while following: -------------------------------- declare...
4
by: karaparad | last post by:
I need to set a column in Access to round numbers up to the nearest thousand. If this can be done, is there an easy way to set this up? Thanks!
4
by: buntyindia | last post by:
Hi, I have a calculator with seven textBox to add Float numbers upto 2 decimal: I have created following function in js: function total_expenses() { // reteriving all the values from...
6
by: krishnaneeraja | last post by:
Hi, Iam developing windows application using vb.net with c#.net.In this i want to validate textbox accept only negative float numbers like -2.3 etc.... please help me.
2
by: Nimbus | last post by:
In my case, if the float value is 74.716, it has to rounding into 74.72. But is that value is 74.715, it should be 74.71.. can anyone tel me what rounding ways i need to use? Thanks in advance
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.