# rounding a double

Y Cheong

Greenhorn

Posts: 9

David Crossett

Ranch Hand

Posts: 102

posted 12 years ago

Aha! You have learned how floats and doubles are not very accurate! Here, try this:

What this code does, of course, is multiply the number by 100, then add .5, then divide by 100 to get back to the hundredths place. Try to work this code out by hand with a calculator and you'll see how it works. The Math.floor() function rounds a number to the nearest integer, NOT greater than the number is. Hence, we bring the hundredths place out to 'integer land' by multiplying by 100 ( or 10 for tenths, or 1000 for thousandths ). Then we add .5 to see if we should go up to the next integer or not ( if the number is 6.456, then we multiply by 100 to get 645.6, then add the .5 making it 646.1, then divide by 100 to get back to 6.46).

I hope this helps - I'm not sure I explained this very well, but I know the code works for what you're trying to do!

What this code does, of course, is multiply the number by 100, then add .5, then divide by 100 to get back to the hundredths place. Try to work this code out by hand with a calculator and you'll see how it works. The Math.floor() function rounds a number to the nearest integer, NOT greater than the number is. Hence, we bring the hundredths place out to 'integer land' by multiplying by 100 ( or 10 for tenths, or 1000 for thousandths ). Then we add .5 to see if we should go up to the next integer or not ( if the number is 6.456, then we multiply by 100 to get 645.6, then add the .5 making it 646.1, then divide by 100 to get back to 6.46).

I hope this helps - I'm not sure I explained this very well, but I know the code works for what you're trying to do!

David Crossett

-nothing important to say, but learnin' plenty-

Jim Yingst

Wanderer

Sheriff

Sheriff

Posts: 18671

posted 12 years ago

What may have gone wrong here is this: Math.round() returns an integer (either int or long, depending on its arguments). If you take an integer / 100, Java will perform

Note that in David's example above, 100.0 was not necessary, because he used the floor() method - which returns a double, not an integer. So it didn't matter if the other number isn the division was 100 or 100.0; either way, the division would use floating-point arithmetic.

**I have seen in another thread, that u can multiply this by 100, then use Math.round, then divide by 100**

What may have gone wrong here is this: Math.round() returns an integer (either int or long, depending on its arguments). If you take an integer / 100, Java will perform

*integer*division, which rounds the

*result*down, much like the floor() function. E.g. 167 / 100 is 1. Not 1.67 as you might expect, not 2 (integer division rounds towards zero, not towards the nearest integer.) You probably do

*not*want integer division at this point, you want floating-point division. You need to tell the compiler that at least one of the numbers involved in the deivision is floating-point. The easy way to achieve this is to divide by 100.0 rather than 100.

Note that in David's example above, 100.0 was not necessary, because he used the floor() method - which returns a double, not an integer. So it didn't matter if the other number isn the division was 100 or 100.0; either way, the division would use floating-point arithmetic.

"I'm not back." - Bill Harding, *Twister*