Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Shaun E on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Rounding Problem

Status
Not open for further replies.

StefanOfMontreal

Programmer
Feb 1, 2005
3
CA
Hello,
I am facing a problem when I try to round
1.8445 to 2 decimal.
Every where they tell you to perform
Math.round(1.8445*100)/100
wich is Math.round(184.45)=184
and 184/100=1.84
well where I went to school they always said that if you
want to round such a number you would start rounding by the end and if you find a 5 or more you would round the next digit.
so in our example since the last digit is a 5, the 4 at the postion 2(from the end) becomes a 5 and then the 4 at positon 3 would also become a 5.

so the result should be
1.85
Does this makes sense or am I becomming completly naze?

P.S. Before posting an answer please TRY your theory.
because numerous ppl would just say
"Well just try Math.round(1.8445)" and this does not work. Just like Math.round(1.49) also gives 1.

So I guess that round does not do rounding properly ?

Thanks for helping
 
By your rules:

Round: 1 - 1.44 -> 1
Round: 1.45 - 2 -> 2

This is not correct, the point of rounding is that 1/2 goes up and 1/2 goes down. When you are rounding to a whole number, you round up from one decimal point. You don't start all the way to the right and keep rounding numbers to the left.

I'm sorry but if your teachers taught you that in school then they taught you the incorrect way to do rounding.

-kaht

Weaseling out of things is important to learn. It's what separates us from the animals... except the weasel. - Homer Simpson (no, I'm not Homer)
 
Hi StefanOfMontreal.

I was taught to round the same way as you described, but that was when rounding money. The round function is rounding to whole figures like "kaht" described above.

staffa
 
THe way that I was taught is that when you have a '5' in the position you're rounding off the rule is to "round even". Meaning if the digit preceeding the 5 is even, leave it alone, if it's odd, round it up (then it'll be even). Years ago I graphed this method against other methods and it's the only one I tried that didn't accumlate rounding errors over time.

If you're truly worried about an even 5 in a particular position, try adding 1 to the following position. That way an even 5 should always round up. E.G.:
1.00 - 1.49 becomes 1.01 - 1.50 rounds to 1
1.50 - 2.00 becomes 1.51 - 2.01 rounds to 2
That's on old trick.


Tracy Dryden

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard. [dragon]
 
Thank all of you for your opinon.

I'm not so sure there is still an issue in my case since we were dealing with $$ and then I was asked to round based on the 3rd (forgetting the 4th) decimal and then chop the 2 digits after the dot.

I guess my boss was feeling grumpy that day or I did confused him too :)
 
Like kaht, the way I was taught is that you only take the number to the right of the position that you are rounding into consideration. So if you're rounding to the hundredth decimal place, you only need to look that the thousandth position to determine the rounding.

Since the number in the thousandth position of 1.8445 is 4 (which is less than 5) you round down to 1.84

If you started all the way to the right, it would be impossible to round infinite numbers, such as pi.

Adam
 
Just want to draw attention to all to an ms official description of its implementation of round(). They opted for banker's rounding (even round down, odd round up). And also how inconsistent over time in implementing round function. Quite unexpected, I have to say, when I first read it.

So be prepared to make your own "rounding" function according to your need if it is critical.

- tsuji
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top