File APIs for Java Developers Manipulate DOC, XLS, PPT, PDF and many others from your application. http://aspose.com/file-tools
Big Moose Saloon
 Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies Register / Login

# Math.round() question

Jonathan Oblea
Greenhorn

Joined: Jul 29, 2002
Posts: 20
Hello All,
I have read the round() method from K&B book and i'm a little confused
on the statement below:
"If the number after the decimal point is less than 0.5 then
Math.round() is equal to Math.floor(). If the number after the
decimal point is greater than equal 0.5 then Math.round() is
equal to Math.ceil()".
Maybe I misunderstood the statement above. Can someone please give me
guidance on this topic?
Example:
1.) Math.round(-5.4) returns -5
Math.floor(-5.4) returns -6
Note: The number after the decimal is less than 0.5 which means
Math.round() should be equal to Math.floor() but it the
result doesn't match?

2.) Math.round(-5.6) returns -6
Math.ceil (-5.6) returns -5
Note: The number after the decimal is greater than equal to 0.5
which means Math.round() should be equal to Math.ceil() but
the result doesn't match?

Jonathan
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
Jonathan O.,
Welcome back, to JavaRanch!
We ain't got many rules 'round these parts, but we do got one. Please change your display name to comply with The JavaRanch Naming Policy. We'd like it if your last name contained more than just one letter.
Thanks Pardner! Hope to see you 'round the Ranch!

Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
It looks like Kathy and Bert weren't thinking about negative numbers for that explanation of how Math.round() works.
I prefer how the Math class documentation describes the behavior of Math.round().
Returns the closest int 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 int. In other words, the result is equal to the value of the expression:
(int)Math.floor(a + 0.5f)
If it's not already reported, and it doesn't look as if it has been, you might do us all a favor and email Kathy and/or Bert about this error.
[ January 07, 2004: Message edited by: Dirk Schreckmann ]
Dmitry Melnik
Ranch Hand

Joined: Dec 18, 2003
Posts: 328
public static long round(double a)
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)
Which means that the statement from the book you have cited is correct for positive numbers only (and it's not correct for negatives)
Sriram Chintapalli
Ranch Hand

Joined: Dec 16, 2003
Posts: 59
Hi Jonathan,
Kathy and Bates did not include negative floats or doubles for explanation of the Math.round() method I had to try and figure it out, perhaps they thought it was too obvious? but they did point out the all interesting -0.5 behaviour:
int i=Math.round(-10.5f); //gives -10
int j=Math.round(10.5f); //gives 11
both are ceil()'ed.
This is my first post so coudnt resist pointing out the 0.5 behavior
Dan Gun
Greenhorn

Joined: Aug 06, 2003
Posts: 26
Yeah, I got confused too after reading that particular bit. At the end I just use Math.floor(x + 0.5).

SCJP 1.4
Jonathan Oblea
Greenhorn

Joined: Jul 29, 2002
Posts: 20
Hello All, Thank you for your replies.
Can I assume for Math.round() the following?
For positive numbers the following can be applied:
1. less than 0.5 = floor()
2. greater than equal to 0.5 = ceil()
For negative numbers the following can be applied:
1. greater than 0.5 = floor()
2. less than equal to 0.5 = ceil()
Thanks again,
Jonathan
Sudhir Meduri
Greenhorn

Joined: May 28, 2003
Posts: 22
I think after looking the following result
Math.round(5.4) :5
Math.round(5.5) :6
Math.round(5.9) :6
Math.round(-5.4) :-5
Math.round(-5.5) :-5
Math.round(-5.9) :-6
I will simply follow the thumb rule
Add +0.5 (no matter pos or neg ) and floor it.
Pls correct me if I am wrong.

Thanks,<br />Sudhir <br />SCJP1.4
dhana rangu
Ranch Hand

Joined: Oct 01, 2003
Posts: 65
sudhir
does
round(-5.5) gives -5
Sudhir Meduri
Greenhorn

Joined: May 28, 2003
Posts: 22
yes it did gave to me
System.out.println ("Math.round(-5.5) :"+Math.round(-5.5)); = -5
i'm using jdk 1.4.2 version (in case..)
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11952

20

For negative numbers the following can be applied:
1. greater than 0.5 = floor()
2. less than equal to 0.5 = ceil()

This makes the former math teacher in me cringe. because, since we're talking about negative numbers, "greater than 0.5" is confusing. -0.4 is greater than -0.5.
I think what you mean is "larger magnitude" than -0.5
This is why it's probably always better to use the "add 0.5 and take the floor" rule. it's less confusing.

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors

I agree. Here's the link: http://aspose.com/file-tools

subject: Math.round() question