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

Math.round() ? please explain ?

mark stone
Ranch Hand

Joined: Dec 18, 2001
Posts: 417
can someone explain what is the difference between these two ?
System.out.println(Math.round(3.5)); //4
System.out.println(Math.round(-3.5)); //-3
why what is the logic behind this ?
Tarik Makota
Greenhorn

Joined: Jan 29, 2002
Posts: 23
Basically:
Java Math.round() which by the way returns long always leans towards higher number in case of those in the midle like : 2.5, 3.5, -2.5, -3.5.
so if you have -5.5 first highest integer/non-decimal is -5.
if you have 6.5 it takes first highest integer which is 7.
Tarik
sylvia weller
Greenhorn

Joined: Jan 07, 2002
Posts: 29
...Java Math.round() which by the way returns long...

This is not always the case. There are two 'round' methods:
int <- Math.round(float)
long <- Math.round(double)
Sylvia
Maulin Vasavada
Ranch Hand

Joined: Nov 04, 2001
Posts: 1871
hi mark,
i read in Val's note that,
Math.round(n) = Math.floor(n+0.5);
check with ur example. its right.
regards
maulin
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Math.round() ? please explain ?