JavaScript:
Rounding numbers

How to:

Here’s how you round numbers in JavaScript using Math.round(), Math.ceil(), and Math.floor():

let originalNumber = 2.567;

let roundedDown = Math.floor(originalNumber); // 2
let roundedUp = Math.ceil(originalNumber);    // 3
let rounded = Math.round(originalNumber);     // 3 (since .567 is more than .5)

console.log(roundedDown); // Prints: 2
console.log(roundedUp);   // Prints: 3
console.log(rounded);     // Prints: 3

To fix to a certain number of decimal places, use toFixed():

let twoDecimals = originalNumber.toFixed(2); // "2.57" (returns a string)

console.log(twoDecimals); // Prints: "2.57"

Convert the string back to a number with a unary plus or Number():

let numberAgain = +twoDecimals; // 2.57

console.log(numberAgain); // Prints: 2.57

Deep Dive

Rounding numbers isn’t new; it’s as old as numbers. In JavaScript, Math.round() uses “round half up” tie-breaking: if the fractional part is 0.5, it rounds to the nearest even number.

For more control, toFixed() might be your go-to, but remember, it returns a string. Converting back to a number might be an extra step but ensures you keep working with numeric types.

Alternatives? Libraries like lodash offer _.round(number, [precision=0]) for more nuanced control. Or, the newer Intl.NumberFormat gives you high-precision formatting beyond just rounding.

Speaking of precision, beware of floating-point quirks in JavaScript. 0.1 + 0.2 doesn’t exactly equal 0.3 due to how numbers are stored. Sometimes, rounding becomes necessary to correct such floating-point errors.

See Also