Math.round()

Math.round(x)
Returns: number · Added in vES1 · Updated March 13, 2026 · Math
math round rounding integer numbers

Math.round() rounds a number to the nearest integer, with .5 values rounding away from zero. This is the standard “arithmetic rounding” behavior you learned in school.

Syntax

Math.round(x)

Parameters

ParameterTypeDefaultDescription
xnumberThe number to round

Return Value

The integer nearest to x. When exactly halfway between two integers (.5 cases), it rounds away from zero.

Examples

Basic usage

Math.round(4.7);
// 5

Math.round(4.4);
// 4

Math.round(4.5);
// 5

Math.round(-4.5);
// -4 (rounds UP toward zero)

Rounding to decimal places

A common need is rounding to specific decimal places. Use multiplication and division to scale, round, then scale back:

// Round to 2 decimal places
function roundToFixed(number, decimals) {
  const factor = 10 ** decimals;
  return Math.round(number * factor) / factor;
}

roundToFixed(3.14159, 2);
// 3.14

roundToFixed(2.71828, 3);
// 2.718

roundToFixed(99.456, 1);
// 99.5

Negative numbers: the .5 behavior

The behavior with negative numbers is the most counterintuitive aspect of Math.round():

// Positive: .5 rounds up (away from zero)
Math.round(2.5);   // 3
Math.round(2.51);  // 3
Math.round(2.49); // 2

// Negative: .5 rounds UP (toward zero!)
Math.round(-2.5);  // -2
Math.round(-2.51); // -3
Math.round(-2.49); // -2

// This differs from Math.floor() and Math.ceil()
Math.floor(-2.5);  // -3 (rounds down, away from zero)
Math.ceil(-2.5);  // -2 (rounds up, toward zero)

This “round half away from zero” behavior matches most rounding conventions but can surprise developers expecting “round half to even” (banker’s rounding).

Common formatting pattern: currency

A practical use case is formatting currency values:

function formatPrice(amount) {
  // Round to 2 decimal places for cents
  const cents = Math.round(amount * 100) / 100;
  return cents.toFixed(2);
}

formatPrice(19.999);
// "20.00"

formatPrice(19.991);
// "19.99"

formatPrice(19.995);
// "20.00"

round vs floor vs ceil vs trunc

Expression3.7-3.73.5-3.5
Math.round(x)4-44-4
Math.floor(x)3-43-4
Math.ceil(x)4-34-3
Math.trunc(x)3-33-3

Key differences:

  • round: Nearest integer (.5 rounds away from zero)
  • floor: Always rounds toward negative infinity
  • ceil: Always rounds toward positive infinity
  • trunc: Simply removes the fractional part, toward zero

Edge Cases

Math.round(Infinity);   // Infinity
Math.round(-Infinity); // -Infinity
Math.round(NaN);       // NaN
Math.round(0);         // 0
Math.round(-0);        // -0
Math.round(0.5);       // 1
Math.round(-0.5);      // -0 (rounds toward zero)

Performance Note

Math.round() is fast on all modern engines. For maximum performance in tight loops, the bitwise trick ~~x or x + 0.5 | 0 can work, but readability suffers. Prefer Math.round().

See Also