File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Math.round() 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 "Math.round()" Watch "Math.round()" New topic
Author

Math.round()

Varsha Dighe
Ranch Hand

Joined: Oct 14, 2000
Posts: 32
How come Math.round(9.5) = 10, but Math.round(-9.5) = -9, and yet Math.round(-9.6) = -10 ?
-Thanks
Varsha
P SOLAIAPPAN
Ranch Hand

Joined: Oct 20, 2000
Posts: 68

Math.round() will always roun up to nearest high value.
For e.g
Math.round(9.4)=9 // any value <9.5 will be rounded to 9<br /> Math.round(9.5)=10 // any value >=9.5 will be rounded to 10
Math.round(9.6)=10 //
Math.round(-9.5)= -9 // Note here -9.5 > -9.6 hence all the
numbers which are >=-9.5 will be
rounded to -9 which is a bigger number
than -10.

Math.round(-9.4)= -9 // -9.4>-9.5 hence -9
Math.round(-9.6)=-10 // since -9.6<-9.5<br /> Remember that -9 >-10
I hope u r clear

solaiappan
kamesh vadarevu
Greenhorn

Joined: Oct 17, 2000
Posts: 20
Varsha,
Math.round() Rounds nearest int which is >0.5 ie if it is 9.0 to 9.5 you get 9.0 and if it is 9.6 to 9.9 you get 10.please remember sign is retained.Hope this helps.
kamesh
kamesh vadarevu
Greenhorn

Joined: Oct 17, 2000
Posts: 20
OOPs I am wrong thank you soliappan.Varsha please check soliappan 's reply.
kamesh
Udayan Naik
Ranch Hand

Joined: Oct 18, 2000
Posts: 135
Hi Varsha.The general rule about rounding is :
round() will give the closest result to the argument passed to it. So from 0.0 to 0.4 it will give 0.0(nearest lower number) and from 0.5 to 1.0 it will give 1.0(nearest higher number).When u give a middle range argument(like 0.5) it will give the next higher number(like 1.0).
For negative numbers , -9 is greater than -9.5 thus u get round(-9.5) as -9 which is nearest higher number.
For -9.6 the nearest lower number is -10.0 thus round(-9.6) is -10.


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


Udayan Naik<BR>Sun Certified Programmer for the Java 2 Platform
Varsha Dighe
Ranch Hand

Joined: Oct 14, 2000
Posts: 32
Thankyou very much to all for taking time to reply to my question. I think I understand it now
-Varsha
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Math.round()