To round a number in JavaScript, use Math.round() for the nearest integer: Math.round(4.6) returns 5. To round decimal places, multiply by a power of 10, round, then divide by the same factor. The method you choose matters for negative values, midpoint ties, and non-finite inputs.
JavaScript rounding methods return numeric values. They do not change the original variable unless you assign the result back to it.
How does rounding numbers in JavaScript work?
Math.round(value) selects the nearest integer. When a value is exactly halfway between integers, JavaScript rounds toward positive infinity. Therefore, Math.round(3.5) returns 4, while Math.round(-3.5) returns -3, not -4.
This comparison uses the same positive, negative, and midpoint inputs for each method. The values appear in this order: 3.7, -3.7, 3.5, -3.5.
- Math.round(): 4, -4, 4, -3
- Math.floor(): 3, -4, 3, -4
- Math.ceil(): 4, -3, 4, -3
- Math.trunc(): 3, -3, 3, -3
How do you round a JavaScript number to an integer?
To round a JavaScript number to an integer, choose the function that matches the desired direction:
- Math.round(value): nearest integer, with midpoint ties toward positive infinity.
- Math.floor(value): the next integer toward negative infinity. For example, Math.floor(-2.1) is -3.
- Math.ceil(value): the next integer toward positive infinity. For example, Math.ceil(-2.1) is -2.
- Math.trunc(value): removes the fractional portion toward zero. For example, Math.trunc(-2.9) is -2.
Floor and truncation are not equivalent for negative values: Math.floor(-2.9) returns -3, whereas Math.trunc(-2.9) returns -2.
How do you round decimal places in JavaScript?
For a simple decimal-place calculation, scale the number before rounding:
function roundTo(value, places) { const factor = 10 ** places; return Math.round(value * factor) / factor; }
roundTo(12.346, 2) returns 12.35. With two decimal places, the factor is 100: JavaScript rounds 12.346 × 100 to 1235, then divides by 100. Replace Math.round() with Math.floor(), Math.ceil(), or Math.trunc() when directional rounding is required.
Scaling also follows the selected method’s negative-number behavior. For example, scaling -12.346 by 100 and applying Math.round() produces -12.35; an exact negative midpoint follows Math.round’s tie rule.
Why do negative values and floating-point numbers surprise you?
Negative values expose the difference between direction and distance. Math.floor(-3.2) moves farther from zero to -4, while Math.trunc(-3.2) removes only the fraction and returns -3. Math.ceil(-3.2) also returns -3.
Decimal scaling has a floating-point limitation. JavaScript stores ordinary numbers in binary floating-point, so many decimal fractions are represented slightly above or below their written value. For example, Math.round(1.005 * 100) / 100 can return 1 because the scaled value may be just below 100.5.
For strict decimal rules, use integer units such as cents or decimal-aware arithmetic instead of assuming multiplication guarantees exact precision. Non-finite values pass through these methods: Math.round(NaN) returns NaN, Math.floor(Infinity) returns Infinity, Math.ceil(-Infinity) returns -Infinity, and Math.trunc(NaN) returns NaN.
