Why does -0.5 when being passed on the Math.round results to 0? and 0.5 when being passed also results to 1? shouldn't it be that when you pass -0.5 to Math.round() should also produce -1 as result? I obtained the -1 result when the number was -0.6.
3 Answers
Math.round(double)
is documented as:
Returns the closest long to the argument, with ties rounding to positive infinity.
So -0.5 is rounding up (towards positive infinite) instead of down towards negative infinity. It's behaving exactly as documented.

- 1,421,763
- 867
- 9,128
- 9,194
The working of round( x ) is implemented as floor( x + 0.5 ) until Java6 atleast. So, by that logic, floor(-0.5 + 0.5) gives you 0 and floor(0.5+0.5) gives you 1
You can refer this link for more details
If you read the docs, it says that round()
"returns the closest long to the argument, with ties rounding up."
"Up" is towards positive infinity, not away from 0.
ETA: I see from Jon Skeet's answer that Java 8 docs improved the clarity on this. This answer quotes Java 7, which is bit more confusing than what 8 says.

- 7,186
- 1
- 26
- 44