Win a copy of Terraform in Action this week in the Cloud forum!
  • 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
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

class Math's round method

 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can anyone tell me the reason for this o/p???
float f4 = -5.5f;
float f7 = 5.5f;

System.out.println("Round f4 is " + Math.round(f4)); ------>-5
System.out.println("Round f7 is " + Math.round(f7)); -------->6
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Because it is rounding "up".
6 is the next higher integer than 5.5
-5 is the next higher integer than -5.5
 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Keep in mind that .5 rounds up to the next highest inter. For example, 1.5 will round up to 2. So, what will -1.5 round up to? -1 and not -2 because you are rounding the number to the next highest integer--which is -1 and not -2.
oops...Cindy beat me by 2 minutes. What she said is correct.
[This message has been edited by js yang (edited June 10, 2001).]
 
payal bansal
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for replying. But do tell me onething------
when float f4 = -5.6f;
Math.round(f4)-------->-6 Why??? Why don't it roundsup to -5?
 
payal bansal
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for replying. But do tell me onething------
when float f4 = -5.6f;
Math.round(f4)-------->-6 Why??? Why don't it roundsup to -5?
 
Sheriff
Posts: 16719
278
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Payal,
The round method will round to the nearest integer, which in the case of -5.6 would be -6. The rounding to -5 would apply for values of -5.5 to -5.0. On a number line:
<pre>
neg <--|--.--.--.--.--|--|--.--.--.--|----> pos
-6 -5.6 -5.5 -5.0
</pre>

Originally posted by payal bansal:
Thanks for replying. But do tell me onething------
when float f4 = -5.6f;
Math.round(f4)-------->-6 Why??? Why don't it roundsup to -5?


 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic