2.4 Rounding Floating-Point Numbers
NN 2, IE 3
var roundedVal = Math.round(floatingPointValue);
The operation does not disturb the original value. Capture the rounded result in a variable.
The algorithm that the Math.round( ) method uses is that any floating-point value that is greater than or equal to x.5 is rounded up to x+1; otherwise, the returned value is x.
Anytime a floating-point number evaluates to a number equal to an integer value, the decimal and digits to the right of the decimal go away. A variable can hold a floating-point number in one statement and be modified to an integer in the next. This drives some programmers crazy because they were indoctrinated by other languages to treat each type of number as a different data type.
2.4.4 See Also
Section 2.0.2 in the introduction of this chapter.