programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Campbell Ritchie
• Devaka Cooray
• Ron McLeod
• Paul Clapham
• Liutauras Vilda
Sheriffs:
• paul wheaton
• Jeanne Boyarsky
• Tim Cooke
Saloon Keepers:
• Stephan van Hulst
• Tim Holloway
• Tim Moores
• Mikalai Zaikin
• Carey Brown
Bartenders:

# Math.round(20.5) & Math.round(-20.5)

Greenhorn
Posts: 29
• • Number of slices to send:
Optional 'thank-you' note:
• • Hi, I have a silly question:
1.Math.round(20.5) = 21;
2.Math.round(-20.5) = -20;
3.Math.round(-20.8) = -21;
why case 2 different from case 3?
Kai,

Ranch Hand
Posts: 18944
• • Number of slices to send:
Optional 'thank-you' note:
• • if the fraction part is greater than 5 then round function will return the next number.

Greenhorn
Posts: 23
• • Number of slices to send:
Optional 'thank-you' note:
• • hi ,
I think
3)Math.round(-20.8)=-20
not -21 because it will round it to the next highest integer when fraction is 5 or >5.so -20 is greater than -21.

Kai Li
Greenhorn
Posts: 29
• • Number of slices to send:
Optional 'thank-you' note:
• • Math.round(-20.8) should be -21 since round means return the nearest int value.

Anonymous
Ranch Hand
Posts: 18944
• • Number of slices to send:
Optional 'thank-you' note:
• • Yeah me too I thoght like that but I executed it and found that round will return nearest intger.

sunny
Greenhorn
Posts: 23
• • Number of slices to send:
Optional 'thank-you' note:
• • o.k I agree
then Math.round(14.9) returns 15.Am I right.

sunny
Greenhorn
Posts: 23
• • Number of slices to send:
Optional 'thank-you' note:
• • o.k I agree
then Math.round(14.9) returns 15.Am I right.

sunny
Greenhorn
Posts: 23
• • Number of slices to send:
Optional 'thank-you' note:
• • o.k I agree
then Math.round(14.9) returns 15.Am I right.

Ranch Hand
Posts: 37
• • Number of slices to send:
Optional 'thank-you' note:
• • Kai Li
I found this is really good question ? I don't understand when 20.5 is 21 why -20.5 is not -21. I tried the same with other languages I got it correct but JAVA ?
Is any JAVA gurus are there to help us!!!

Ranch Hand
Posts: 116
• • Number of slices to send:
Optional 'thank-you' note:
• • You don't need to be a Java expert to answer this question.
All you have to do is to just look up the java.lang.Math API.

It says...
public static long round(double a)
Returns the closest long to the argument. The result is rounded to an integer by adding 1/2, taking the floor of the result, and casting the result to type long. In other words, the result is equal to the value of the expression:
(long)Math.floor(a + 0.5d)

Now,
1.Math.round(20.5) = 21;
is
Math.floor ( 20.5 + 0.5 ) -> 21
2.Math.round(-20.5)
is
Math.floor ( -20.5 + 0.5 ) -> Math.floor ( -20 ) -> -20
3.Math.round(-20.8)
is
Math.floor ( -20.8 + 0.5 ) -> Math.floor ( -20.3 ) -> -21
OK?

jafarali
Ranch Hand
Posts: 37
• • Number of slices to send:
Optional 'thank-you' note:
• • Rajsim,
Thanks : I never noticed that point It's a tiny ad. At least, that's what she said. a bit of art, as a gift, the permaculture playing cards https://gardener-gift.com