Sub topic 11
Rounding: round, floor, ceil, and trunc
This chapter assumes you know how JavaScript stores numbers as 64-bit floats, why values like 1.005 are not stored exactly, and how the Math object groups its functions. Four Math functions turn a fractional number into an integer. They agree on positive numbers more often than on negative ones, and that difference causes most rounding bugs.
The Four Functions
Each function answers a different question:
Math.round(x)returns the nearest integer. Ties (.5) go toward positive infinity.Math.floor(x)returns the largest integer less than or equal tox, so it always moves down the number line.Math.ceil(x)returns the smallest integer greater than or equal tox, so it always moves up the number line.Math.trunc(x)removes the fractional part, so it always moves toward zero.
console.log(Math.round(2.5)); // 3
console.log(Math.round(-2.5)); // -2
console.log(Math.floor(-2.5)); // -3
console.log(Math.ceil(-2.5)); // -2
console.log(Math.trunc(-2.5)); // -2
console.log((1.005).toFixed(2)); // "1.00" (1.005 is stored slightly below 1.005)
console.log(typeof (3.14159).toFixed(2)); // "string"| Input | round | floor | ceil | trunc |
|---|---|---|---|---|
| 2.7 | 3 | 2 | 3 | 2 |
| 2.5 | 3 | 2 | 3 | 2 |
| -2.5 | -2 | -3 | -2 | -2 |
| -2.7 | -3 | -3 | -2 | -2 |
For positive inputs, floor and trunc give the same result. For negative inputs, trunc gives the same result as ceil. If your code only ever sees positive numbers in testing, this difference stays hidden until production.
Common Errors
Expecting round-half-away-from-zero
Many people learned in school that -2.5 rounds to -3. Math.round gives -2 instead, because it always breaks ties upward. If you need symmetric rounding, round the magnitude and put the sign back:
function roundHalfAwayFromZero(x) {
return Math.sign(x) * Math.round(Math.abs(x));
}
console.log(roundHalfAwayFromZero(-2.5)); // -3
console.log(roundHalfAwayFromZero(2.5)); // 3Rounding to decimal places
None of these functions accepts a number of digits. The usual workaround is to scale, round, and scale back. Floating-point storage can still trip it up:
console.log(Math.round(1.005 * 100) / 100); // 1 (1.005 * 100 is 100.49999999999999)
console.log(Math.round((1.005 + Number.EPSILON) * 100) / 100); // 1.01Adding Number.EPSILON fixes values near 1, but it is too small to matter for large magnitudes. Use this approach for display. Money calculations should use integer cents instead. toFixed returns a string and has the same stored-value problem.
Using floor to split negative values
For pagination or bucketing with negative offsets, Math.floor(-7 / 2) gives -4, while Math.trunc(-7 / 2) gives -3. Decide which one your algorithm needs. Don't pick whichever happened to pass your tests.
All four functions return NaN for NaN, return infinities unchanged, and coerce their argument with Number() first, so Math.floor("3.9") is 3.
Exercise
What do Math.floor(-0.5), Math.ceil(-0.5), Math.trunc(-0.5), and Math.round(-0.5) return?
Show answer
Math.floor(-0.5) is -1. Math.ceil(-0.5), Math.trunc(-0.5), and Math.round(-0.5) all return -0, which prints as -0 in Node and compares === to 0.
Exercise
You need the number of full hours in a duration that can be negative (for example -90 minutes should give -1, not -2). Which function do you use on minutes / 60?
Show answer
Math.trunc, because it moves toward zero: Math.trunc(-1.5) is -1, while Math.floor(-1.5) would give -2.