Math.round()
Math.round(x)
Rounds to the nearest integer. An exact half (x.5) is rounded up – towards plus infinity.
Parameters
- x – a number.
Return value
An integer.
Examples
console.log(Math.round(2.4), Math.round(2.5), Math.round(-2.5), Math.round(-2.6)); const price = 14.7943; console.log(Math.round(price * 100) / 100); console.log(Math.round(1.005 * 100) / 100, Math.round((1.005 + Number.EPSILON) * 100) / 100);
Output:
2 3 -2 -3 14.79 1 1.01
Things to watch out for
- A half of a negative number goes towards zero:
Math.round(-2.5)is-2(toFixed() gives-3). - Rounding to decimal places with
* 100 / 100suffers from the inexactness of decimal numbers (1.005 → 1). For money calculate in cents. - To output a number with a given number of decimal places toFixed() or toLocaleString() is more suitable.
Browser support
Baseline – widely available: works in all major browsers and has for a long time (since 2015 at the latest). Since version: Chrome 1, Firefox 1, Safari 1; Node.js 0.10.0. As of October 2026 (MDN browser-compat-data).
Related:
Math.floor()
Math.ceil()
Number.prototype.toFixed()
Number.EPSILON