Math.round()
BaselineWidely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.
TheMath.round()
static method returns the value of a number rounded to the nearest integer.
Try it
console.log(Math.round(0.9));// Expected output: 1console.log(Math.round(5.95), Math.round(5.5), Math.round(5.05));// Expected output: 6 6 5console.log(Math.round(-5.05), Math.round(-5.5), Math.round(-5.95));// Expected output: -5 -5 -6
Syntax
Math.round(x)
Parameters
x
A number.
Return value
The value ofx
rounded to the nearest integer.
Description
If the fractional portion of the argument is greater than 0.5, the argument is rounded to the integer with the next higher absolute value. If it is less than 0.5, the argument is rounded to the integer with the lower absolute value. If the fractional portion is exactly 0.5, the argument is rounded to the next integer in the direction of +∞.
Note:This differs from many languages'round()
functions, which often round half-incrementsaway from zero, giving a different result in the case of negative numbers with a fractional part of exactly 0.5.
Math.round(x)
is not exactly the same asMath.floor(x + 0.5)
. Whenx
is -0, or -0.5 ≤ x < 0,Math.round(x)
returns -0, whileMath.floor(x + 0.5)
returns 0. However, neglecting that difference and potential precision errors,Math.round(x)
andMath.floor(x + 0.5)
are generally equivalent.
Becauseround()
is a static method ofMath
, you always use it asMath.round()
, rather than as a method of aMath
object you created (Math
has no constructor).
Examples
Using round
Math.round(-Infinity); // -InfinityMath.round(-20.51); // -21Math.round(-20.5); // -20Math.round(-0.1); // -0Math.round(0); // 0Math.round(20.49); // 20Math.round(20.5); // 21Math.round(42); // 42Math.round(Infinity); // Infinity
Specifications
Specification |
---|
ECMAScript® 2026 Language Specification # sec-math.round |