JavaScript Math.round() Method

Definition
Math.round(x) rounds a number to the nearest integer. Numbers with a decimal part of .5 or more round up, and anything less rounds down.
Read the syntax below, then try the three examples to see the method in action.
Syntax
Call the method on Math. Start with this line.
| Call | What it does |
|---|
Math.round(x) | Returns x rounded to the nearest integer. |
Built into JavaScript. Call Math.round(...) on the Math object — no import required. Math is not a constructor, so never write new Math().
Library declarations
Parameters
| Parameter | Description |
|---|
| x | Required. The number to round. Other values are converted to a number first. |
Return Value
| Return Value | Description |
|---|
| Number | The nearest integer to x. Returns NaN if x is not a number. |
JavaScript Examples
Example 1 — Round to the Nearest Integer
Below .5 goes down. From .5 upward goes up.
console.log(Math.round(4.2)); // 4
console.log(Math.round(4.5)); // 5
console.log(Math.round(4.7)); // 5
How It Works
- Round down.
4.2 is closer to 4, so the result is 4. - Half goes up.
4.5 is exactly in the middle and rounds up to 5. - Round up.
4.7 is closer to 5. - Whole numbers stay.
Math.round(5) is just 5.
Example 2 — Negative Numbers and Halves
Negative halves round toward zero, not away from it. This surprises many beginners.
console.log(Math.round(2.5)); // 3
console.log(Math.round(-4.5)); // -4
console.log(Math.round(-4.6)); // -5
console.log(Math.round(-0.4)); // -0
How It Works
- Positive half.
2.5 rounds up to 3. - Negative half.
-4.5 rounds toward the larger number, which is -4. - Not a half.
-4.6 is closer to -5. - Minus zero.
-0.4 rounds to -0. It behaves like 0 in most code.
Example 3 — Round to 2 Decimal Places
Multiply, round, then divide. Compare it with toFixed(), which returns a string.
const price = 19.456;
console.log(Math.round(price * 100) / 100); // 19.46
console.log(price.toFixed(2)); // 19.46
console.log(Math.round("abc")); // NaN
How It Works
- Shift the decimal.
19.456 * 100 is about 1945.6. - Round.
Math.round(1945.6) is 1946. - Shift back. Dividing by
100 gives 19.46 as a number. - toFixed().
toFixed(2) also gives "19.46" but as text (a string). - Bad input.
Math.round("abc") is NaN.
Compatibility
Browser & Runtime Support
Math.round is part of the first JavaScript standard (ES1) and is Baseline Widely available in every browser and in Node.js.
✓ Baseline · ES1
Math.round
Supported in Chrome, Firefox, Safari, Edge, Internet Explorer and Node.js for many years. No polyfill needed.
99% Modern browser support
Google Chrome Supported · Desktop & Mobile
Full support
Mozilla Firefox Supported · Desktop & Mobile
Full support
Apple Safari Supported · macOS & iOS
Full support
Microsoft Edge Supported · Chromium
Full support
Internet Explorer No native support · Use a polyfill
Polyfill
Opera Supported · Modern versions
Full support
Samsung Internet Supported · Android
Full support
Bun Supported · JavaScript runtime
Supported
Deno Supported · JavaScript runtime
Supported
Node.js Supported · Server runtime
Supported
Android WebView Supported · Modern WebView
Full supportMath.round Excellent
Bottom line: Use Math.round anywhere. It works in all browsers.
Frequently Asked Questions
It rounds a number to the nearest integer. Math.round(4.6) is 5 and Math.round(4.4) is 4.
A .5 rounds up toward positive infinity. Math.round(2.5) is 3 and Math.round(-2.5) is -2.
Use Math.round(x * 100) / 100. You can also use x.toFixed(2), but that returns a string.
round goes to the nearest integer, floor always goes down, ceil always goes up, and trunc cuts off the decimal part.
It returns NaN because the text cannot be converted to a number.
All of them. It is part of the first JavaScript standard and works in every browser and in Node.js.
Next: sign()
Learn how to find if a number is positive, negative or zero.
sign() tutorial →About the author
Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
9 people found this page helpful