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)); // -5Math.round(number)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)); // 12When 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)); // -21A 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.26const value = 5.42;
console.log(Math.round(value)); // 5
console.log(Math.floor(value)); // 5
console.log(Math.ceil(value)); // 6For dedicated rounding behavior, see Math.ceil() and Math.floor().
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 expectTry the Math.round() demo. You can also read the PHP round() tutorial.
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.