The Math.round() function returns the number rounded to the nearest integer. That is, 3.87 is rounded to 4 and 3.45 is rounded to 3.
Example
Math.round() Syntax
The syntax of the Math.round() function is:
Math.round(x)
round(), being a static method, is called using the Math class name.
Math.round() Parameters
The Math.round() function takes in:
- x - A number
Return value from Math.round()
Math.round() returns the value of the number rounded to the nearest integer as follows:
- If the fractional portion > 0.5, x is rounded to integer with higher absolute value.
- If the fractional portion < 0.5, x is rounded to integer with lower absolute value.
- If the fractional portion = 0.5, x is rounded to the next integer in the direction of +∞.
Example: Using Math.round()
Output
2 10 5 -4 0 NaN
Note: Math.round() returns 0 for null rather than NaN.
Recommended readings: