• Post Reply Bookmark Topic Watch Topic
  • New Topic
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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Liutauras Vilda
Sheriffs:
  • Paul Clapham
  • Jeanne Boyarsky
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
Bartenders:
  • Jesse Duncan
  • Frits Walraven
  • Mikalai Zaikin

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

 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
if the fraction part is greater than 5 then round function will return the next number.
 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
If i am wrong please help me.
 
Kai Li
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Math.round(-20.8) should be -21 since round means return the nearest int value.
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yeah me too I thoght like that but I executed it and found that round will return nearest intger.
 
sunny
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
o.k I agree
then Math.round(14.9) returns 15.Am I right.
 
sunny
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
o.k I agree
then Math.round(14.9) returns 15.Am I right.
 
sunny
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
o.k I agree
then Math.round(14.9) returns 15.Am I right.
 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Rajsim,
Thanks : I never noticed that point
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic