# Math.round() method

shetal bansal

Ranch Hand

Posts: 63

posted 11 years ago

Math.round(double d)...

"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."

http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Math.html

"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."

http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Math.html

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." *~Joe Strummer*

sscce.org

Abdulla Mamuwala

Ranch Hand

Posts: 225

posted 11 years ago

I am not sure, but this could be the way you do it.

First add 0.5 to the argument and than perform a Math.floor() on it.

Here f6=-5.49

-5.49 + 0.5 = -4.99

Now, we perform a Math.floor() on -4.99 giving -5, because -5 is less than -4.

Similarly f7=5.49

5.49 + 0.5 = 5.99

Now, we perform a Math.floor() on 5.99 giving 5, because 5 is less than 6.

Math.round() returns integer closest to the argument in the case above because the arguments are of type float.

[ June 17, 2005: Message edited by: Abdulla Mamuwala ]

First add 0.5 to the argument and than perform a Math.floor() on it.

Here f6=-5.49

-5.49 + 0.5 = -4.99

Now, we perform a Math.floor() on -4.99 giving -5, because -5 is less than -4.

Similarly f7=5.49

5.49 + 0.5 = 5.99

Now, we perform a Math.floor() on 5.99 giving 5, because 5 is less than 6.

Math.round() returns integer closest to the argument in the case above because the arguments are of type float.

[ June 17, 2005: Message edited by: Abdulla Mamuwala ]

Edwin Dalorzo

Ranch Hand

Posts: 961

posted 11 years ago

For the SCJP exam I think it is important to recognize the next things

double Math.floor(double a): receives a double and returns a double double Math.ceil(double a) : receives a double and returns a double int Math.round(float a): receives a float returns a integer long Math.round(double a): receives a double returns a long

It is important to recognize that in the next output

Now, rouund might behave different for positive and negative numbers.

However:

Be careful with that, because that is a probable question in the exam.

It is important to recognize that in the next output

Now, rouund might behave different for positive and negative numbers.

However:

Be careful with that, because that is a probable question in the exam.

Rick O'Shay

Ranch Hand

Posts: 531