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

# math class

Greenhorn
Posts: 1
• Number of slices to send:
Optional 'thank-you' note:

hi,
here is one mock exam question.please see am i right or wrong?
Double a = 90.7;
Double b = method(a);
System.out.println(b);
What could be the method(b) to printout 90.0
1. abs()
2. round()
3. floor()

4. ceil()
i choose 2,3 ,but the answer is only 3 .How 2 is not correct,
my feeling is round(90.7) will return integer value 90, later casting in to double which becomes 90.0
am i wrong?
--thanks
shivaram

Ranch Hand
Posts: 18944
• Number of slices to send:
Optional 'thank-you' note:
Hi Sivram.
Only floor() will give you 90.0 . round() WILL NOT give 90.0 because floor() will return the CLOSEST LONG to the argument.There is a simple rule to evaluating this(also explained in Sun's docs):
2)take floor() of the result.This will give you 91.
3)now cast result to long.

------------------
Come on in !! Drinks are on the house in the Big Moose Saloon !!

Ranch Hand
Posts: 30
• Number of slices to send:
Optional 'thank-you' note:
class RoundTry {

public static void main(String args[]){

System.out.println("round(90.7) = " + Math.round(90.7));
}
}
it prints out 91 (far from 90.0, not even 91.0)
because java.Math.round() return long, not float or double, so why not check it up in API and play with it.
rong chen
[This message has been edited by Rong Chen (edited October 06, 2000).]

Rong Chen
Ranch Hand
Posts: 30
• Number of slices to send:
Optional 'thank-you' note:
by the way, round() can't take Double argument, it only takes flout or double. following lines cause compiler error:
Double a = 90.7;
round(a);

 Did you see how Paul cut 87% off of his electric heat bill with 82 watts of micro heaters?