• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

why different output whilre using round() function

 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public class C {
public static void main(String [] args){

double y = Math.round(-5.5);
double z= Math.round(5.5);

System.out.println(y);
System.out.println(z);
}
}
 
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Math round() method will return the maximum value.
So,
--> In 5.5 it rounds to the maximum value of 6.0(return type is double)
--> In -5.5, it rounds to the value of -5.0, because -5 is greater than -6.

hope you are clear now.
 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The round method in Math class is implemented something like this
round(double d)
{
return (long)Math.floor(d + 0.5);
}
Thus round(5.5) will return Math.floor(5.5 + 0.5) which is (long)6.0
and round(-5.5) will return Math.floor(-5.5 + 0.5) which is Math.floor(-5.0) which is (long)-5.0.
 
You know it is dark times when the trees riot. I think this tiny ad is their leader:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic