Math.round
Rounds a number to the nearest integer.
Available since: ES1
Syntax
Math.round(x)Parameters
| Parameter | Type | Description |
|---|---|---|
| x | number | The number to round. |
Return value
numberThe nearest integer.
Examples
Rounding to an integer:
console.log(Math.round(4.4));
console.log(Math.round(4.6));Output:
4
5A half is rounded up, including for negative numbers:
console.log(Math.round(2.5));
console.log(Math.round(-2.5));Output:
3
-2Rounding to two decimal places:
console.log(Math.round(3.14159 * 100) / 100);Output:
3.14Notes
The method has no second argument for decimal places. To get a string with a fixed number of digits, use toFixed().
See also
- Math.floor — Rounds a number down to an integer.
- Number.prototype.toFixed — Formats a number with a given number of decimal places.
- Math.ceil — Rounds a number up to an integer.
- Number.prototype.toPrecision — Formats a number with a given number of significant digits.
Related recipes
In the tutorial
- Numbers — Numbers in JavaScript — integers and fractions, rounding, the Math object, toFixed and the special values Infinity and NaN.