Originally posted by John Lincoln:
...I don't cleary understand Math.round is equal to Math.floor + 0.5. Can you please explain...
First, we add 0.5 to the value, then take the
floor of the result. As the API says, Math.floor(double a)...
Returns the largest (closest to positive infinity) double value that is less than or equal to the argument and is equal to a mathematical integer.
First, note that we are looking for a value "less than or equal to the argument." So if you imagine the argument on the number line, our range of possible results includes the argument
and everything to the left. Now, within this range, we find the integer that is "closest to positive infinity" -- or in other words, the
right-most integer.
So for example, if we have -3.2, we first add 0.5 to get -2.7. Now, our range of possible results is everything to the left of (and including) -2.7. Next we look for the right-most integer
within that range, which is -3.0. Casting to type long, this is -3.
(Note that round is overridden to take a float or a double. The float implementation returns an int, and the double implementation returns a long.)
[ March 07, 2007: Message edited by: marc weber ]