JavaScript - Math.round() Method
The JavaScript Math.round() method returns an integral value that is nearest to the argument value. In halfway cases, the argument is rounded in the direction of +∞.
Syntax
Math.round(x)
Parameters
x |
Specify a value to round. |
Return Value
Returns an integral value by rounding up the x to the nearest integral value.
Example:
In the example below, Math.round() method is used to round the given number.
var txt; txt = "Math.round(10.2) = " + Math.round(10.2) + "<br>"; txt = txt + "Math.round(10.5) = " + Math.round(10.5) + "<br>"; txt = txt + "Math.round(10.8) = " + Math.round(10.8) + "<br>"; txt = txt + "Math.round(-5.2) = " + Math.round(-5.2) + "<br>"; txt = txt + "Math.round(-5.5) = " + Math.round(-5.5) + "<br>"; txt = txt + "Math.round(-5.8) = " + Math.round(-5.8) + "<br>";
The output (value of txt) after running above script will be:
Math.round(10.2) = 10 Math.round(10.5) = 11 Math.round(10.8) = 11 Math.round(-5.2) = -5 Math.round(-5.5) = -5 Math.round(-5.8) = -6
❮ JavaScript - Math Object