JavaScript Math.round()

Math.round(x) returns the nearest integer. If a value is exactly halfway between two integers, JavaScript rounds toward positive infinity, which matters for negative .5 values.

console.log(Math.round(5.5));  // 6
console.log(Math.round(-5.5)); // -5

Math.round() syntax Top ↑

Math.round(number)

Basic Math.round() examples Top ↑

console.log(Math.round(2.65));  // 3
console.log(Math.round(7.05));  // 7
console.log(Math.round(-2.65)); // -3
console.log(Math.round(-8.15)); // -8
console.log(Math.round(11.65)); // 12

Why Math.round(-5.5) is -5 Top ↑

When the fraction is exactly .5, the tie is resolved in the direction of positive infinity. This differs from the “half away from zero” rule used in some systems.

console.log(Math.round(-20.5));  // -20
console.log(Math.round(-20.51)); // -21

Rounding to a chosen number of decimal places Top ↑

A common numeric technique multiplies, rounds, and divides. For display formatting, toFixed() is often clearer because it returns a string with a fixed number of decimal places.

const value = 11.257;
const rounded = Math.round(value * 100) / 100;
console.log(rounded); // 11.26

Math.round() vs Math.floor() vs Math.ceil() Top ↑

const value = 5.42;
console.log(Math.round(value)); // 5
console.log(Math.floor(value)); // 5
console.log(Math.ceil(value));  // 6

For dedicated rounding behavior, see Math.ceil() and Math.floor().

Floating-point rounding caveats Top ↑

Multiplying decimal values before rounding can expose binary floating-point representation effects. If the application requires exact decimal rules, define those rules explicitly rather than assuming a single rounding expression covers every case.

console.log(1.005 * 100); // may not be represented as the exact decimal value you expect

Interactive Math.round() demo Top ↑

Try the Math.round() demo. You can also read the PHP round() tutorial.

Math.abs()




Subscribe to our YouTube Channel here



plus2net.com










✖
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
© 2000-2026 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer