Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

JavaScript · Function

Math.round

Math.round() returns a number rounded to the nearest integer. Exact halves round toward positive infinity, so -3.5 becomes -3 and 3.5 becomes 4.

How it works

Math.round() converts its argument to a number and returns the closest integral Number. When two integers are equally close, it chooses the one toward positive infinity, so positive and negative halves behave differently.

Syntax

Math.round(x)

Parameters

NameTypeDescription
xnumberThe numeric value to round; it is converted to a Number before rounding.

Returns

The value of x rounded to the nearest integer.

Key points

  • Returns the input rounded to the nearest integer.
  • Fractions above 0.5 round away from the current integer toward the next integer.
  • Exact halves resolve toward positive infinity.
  • Call it through Math.round(); it is not an instance method.
  • The return value is a Number containing the rounded result.

Common mistakes

  • Halfway values round toward positive infinity: 3.5 becomes 4, while -3.5 becomes -3.
  • Do not replace Math.round(x) with Math.floor(x + 0.5), because results can differ, especially for negative zero.
  • Values from -0.5 through values below zero can produce negative zero; use Object.is(result, -0) when the sign matters.

Example: Round values to integers

const values = [3.49, 3.5, -3.5, -0.1];
const rounded = values.map(Math.round);
console.log(rounded); // [3, 4, -3, -0]

Maps Math.round over several values to show ordinary rounding, ties toward positive infinity, and preservation of negative zero.

When to use it

Round numeric results

Convert measured or calculated decimal values to whole-unit quantities.

Nearest-integer decisions

Choose the nearest integer for display or threshold logic.

Signed numeric processing

Handle signed values while preserving JavaScript's negative-zero behavior when relevant.

  • Math.ceil()
  • Math.floor()
  • Math.trunc()
  • Object.is()

Source: Math.round () - JavaScript | MDN. Checked 2026-10-15.