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

Math.round() for negative numbers

Angela lewis
Ranch Hand

Joined: Mar 01, 2004
Posts: 100
Can someone please explain how Math.round() works for negative numbers.
anandkumar gangoni

Joined: May 30, 2004
Posts: 16
well... Math.round method comes in 2 flavours where one form returns int on taking float as an argument and the other return long on taking double as argument.

whether the argument is negative or positive wat it does is........ it adds 0.5 to the argument and returns the floor value in int or long type depending on the argument provied.

shall be waiting to here from you.
till then this is Anand saying....
anandkumar gangoni

Joined: May 30, 2004
Posts: 16
an addition to wat i said

If NaN is the argument passed to Math.round, then the return value is zero.
Dirk Schreckmann

Joined: Dec 10, 2001
Posts: 7023
If you're wondering how anandkumar gangoni could possibly know such a thing, I'll give you a hint to his wisdom. Take a look at the J2SE API documentation.

[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
I agree. Here's the link:
subject: Math.round() for negative numbers
It's not a secret anymore!