Encodera

Thanks for reading!

Donate to the site — we work hard for you.

JavaScript reference

Math.round

Rounds a number to the nearest integer.

Available since: ES1

Syntax

Math.round(x)

Parameters

ParameterTypeDescription
xnumberThe 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
5

A half is rounded up, including for negative numbers:

console.log(Math.round(2.5));
console.log(Math.round(-2.5));

Output:

3
-2

Rounding to two decimal places:

console.log(Math.round(3.14159 * 100) / 100);

Output:

3.14

Notes

The method has no second argument for decimal places. To get a string with a fixed number of digits, use toFixed().

See also

Related recipes

In the tutorial

  • Numbers — Numbers in JavaScript — integers and fractions, rounding, the Math object, toFixed and the special values Infinity and NaN.

Thanks for reading!

Donate to the site — we work hard for you.