• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Bear Bibeault
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Jj Roberts
  • Carey Brown
Bartenders:
  • salvin francis
  • Frits Walraven
  • Piet Souris

Math.round() method

 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the output of following



Answer is :

Round f4 is -5
Round f5 is 6
Round f6 is -5
Round f7 is 5

How r the value of f6 and f7 getting calculated??
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Math.round(double d)...

"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."

http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Math.html
 
Ranch Hand
Posts: 225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am not sure, but this could be the way you do it.
First add 0.5 to the argument and than perform a Math.floor() on it.

Here f6=-5.49
-5.49 + 0.5 = -4.99
Now, we perform a Math.floor() on -4.99 giving -5, because -5 is less than -4.

Similarly f7=5.49
5.49 + 0.5 = 5.99
Now, we perform a Math.floor() on 5.99 giving 5, because 5 is less than 6.

Math.round() returns integer closest to the argument in the case above because the arguments are of type float.
[ June 17, 2005: Message edited by: Abdulla Mamuwala ]
 
Ranch Hand
Posts: 961
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For the SCJP exam I think it is important to recognize the next things

  • double Math.floor(double a): receives a double and returns a double
  • double Math.ceil(double a) : receives a double and returns a double
  • int Math.round(float a): receives a float returns a integer
  • long Math.round(double a): receives a double returns a long


  • It is important to recognize that in the next output



    Now, rouund might behave different for positive and negative numbers.



    However:



    Be careful with that, because that is a probable question in the exam.
     
    Ranch Hand
    Posts: 531
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    float f6 = -5.49f;
    float f7 = 5.49f;

    Rounds off to the nearest integer: -5 and 5 respectively.

    Given -5.5 and 5.5 you need a rounding rule since there are two "nearest" integers. They decided to round up: -5, 6 respectively. In other words if smack dab in the middle add 0.5.
     
    shetal bansal
    Ranch Hand
    Posts: 63
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thanxs a lot for the wonderful replies!!
     
    Don't get me started about those stupid light bulbs.
    reply
      Bookmark Topic Watch Topic
    • New Topic