aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Math.round( -.5)? 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( -.5)?" Watch "Math.round( -.5)?" New topic
Author

Math.round( -.5)?

Tom Adams
Ranch Hand

Joined: Feb 07, 2003
Posts: 56
Does it seem odd that java rounds -.5 to zero?
Is that right?


Tom
Jessica Sant
Sheriff

Joined: Oct 17, 2001
Posts: 4313

check out the API for Math.round(double)
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. In other words, the result is equal to the value of the expression:
(long)Math.floor(a + 0.5d)

So... it behaves just like it's supposed to.
Tom Adams
Ranch Hand

Joined: Feb 07, 2003
Posts: 56
Yea...I read that also. Is it just me or does this seem strange? Just as a check excel and MS SQL Server both feel that -.5 should be rounded to -1. :roll:
Jessica Sant
Sheriff

Joined: Oct 17, 2001
Posts: 4313

lol yes -- it does seem a little strange... maybe you can write your own method called
long roundTheWayIExpectItToBehave(double d)
and use that??
 
 
subject: Math.round( -.5)?