jQuery in Action, 3rd edition
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Difference between Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Difference between "rint" and "round" methods in Math class?" Watch "Difference between "rint" and "round" methods in Math class?" New topic

Difference between "rint" and "round" methods in Math class?

Mallik Hiremath
Ranch Hand

Joined: Oct 20, 2002
Posts: 46
The Math class API states
-public static double rint(double d)
this method returns a double value that is closest in value to the argument and is equal to a mathematical integer.
-public static long (or int) round(double d)
this method returns closest long (or int) to the argument.
Could any one tell me what exactly the difference between these two methods apart from their return types?
Mallik HM
[ November 02, 2002: Message edited by: Mallik HM ]
Ron Newman
Ranch Hand

Joined: Jun 06, 2002
Posts: 1056
Be sure to read the long descriptions, not just the short ones. The functions produce different results when the argument is exactly halfway between two integers. round() rounds up, but rint() rounds to an even integer.

% java RoundTest 3.5
round(3.5) = 4
rint(3.5) = 4.0
% java RoundTest 4.5
round(4.5) = 5
rint(4.5) = 4.0

Ron Newman - SCJP 1.2 (100%, 7 August 2002)
Mallik Hiremath
Ranch Hand

Joined: Oct 20, 2002
Posts: 46
Ron ,
Thank you very much.
Kathy Sierra
Cowgirl and Author

Joined: Oct 10, 2002
Posts: 1589
Howdy all -- just wanted to point out that the exam objectives require *only* the following methods:
abs, ceil, floor, max, min, random, round, sin, cos, tan, sqrt, toDegrees, toRadians
And that's it!
Don't know about you, but I'm much too lazy to study more than I need to , so for all of you who're overwhelmed as it is, the list above is all that's required of the Math class in the exam.
of course, feel free to study more

Co-Author of Head First Design Patterns
Just a Jini girl living in a J2EE world.
I agree. Here's the link: http://aspose.com/file-tools
subject: Difference between "rint" and "round" methods in Math class?
It's not a secret anymore!