Math.floor vs ceil vs round vs trunc in JavaScript
Beginner · 4 min read
JavaScript has four ways to turn a decimal into a whole number: Math.floor, Math.ceil, Math.round and Math.trunc. For 4.7 they look like they come in two pairs. Give them -4.7 and the pairs split up. That is where real bugs live: a refund, a temperature below zero, a debit on a bank statement.
In this tutorial you will run all four on a positive and a negative number, see which ones agree and which don't, and learn a picture that makes the rules easy to remember. You need to know what a number and a function are.
Picture a number line
Draw a line with the negative numbers on the left, zero in the middle and the positive numbers on the right:
-5 -4.7 -4 ... 0 ... 4 4.7 5
Each function moves a decimal to a whole number in a fixed direction:
flooralways moves left, down the line, like dropping to the floor.ceil(short for ceiling) always moves right, up the line.roundmoves to the nearest whole number.trunc(short for truncate, "cut off") drops the decimal part, so it always moves towards zero.
Moving towards zero is left for a positive number and right for a negative one. That single fact explains everything below.
Here is the exercise. Try it yourself first if you like, or read on and we will build it together.
52 floor, ceil, round and trunc
easyApply all four rounding functions to 4.7 and to -4.7, and work out which pairs agree and which do not.
Stuck? Three hints, smallest first
- 1.
floorgoes down the number line,ceilgoes up. Neither cares about zero. - 2.
truncdrops the fraction, so it moves TOWARDS zero, which is downward for a positive and upward for a negative. - 3.
Math.roundbreaks a tie upwards, which for-2.5means-2.
Step 1: all four at once
Return an object with one answer from each function, so you can compare them side by side:
const ways = (n) => ({
floor: Math.floor(n),
ceil: Math.ceil(n),
round: Math.round(n),
trunc: Math.trunc(n),
});
const a = ways(4.7);
console.log(a.floor, a.ceil, a.round, a.trunc); // 4 5 5 4
The brackets around the curly braces, ({ ... }), tell JavaScript that the arrow function returns an object. Without them, the braces would be read as the body of the function.
For 4.7, floor and trunc agree on 4, and ceil and round agree on 5.
Step 2: try a negative number
const ways = (n) => ({
floor: Math.floor(n),
ceil: Math.ceil(n),
round: Math.round(n),
trunc: Math.trunc(n),
});
const b = ways(-4.7);
console.log(b.floor, b.ceil, b.round, b.trunc); // -5 -4 -5 -4
Now floor and trunc disagree. floor moves left to -5. trunc moves towards zero, which is right, to -4.
This is the natural wrong turn. Plenty of code uses Math.floor to "remove the decimals". It works for every positive number, so the bug stays hidden until the first negative number arrives.
Step 3: ties and Math.round
A tie is a number exactly halfway, like 2.5. Math.round breaks a tie by moving up the number line, to the right:
console.log(Math.round(2.5)); // 3
console.log(Math.round(-2.5)); // -2
Many people expect -3 for the second one, thinking "ties go away from zero". The rule is "ties go up", and up from -2.5 is -2.
Step 4: minus zero
Round -0.5 and you get a value JavaScript calls minus zero, written -0:
console.log(Object.is(Math.round(-0.5), -0)); // true
console.log(Math.round(-0.5) === 0); // true
console.log(JSON.stringify(Math.round(-0.5))); // 0
-0 counts as equal to 0 with ===, and turning it into text loses the sign. Object.is is the one comparison that can tell them apart. You will rarely need to care, but it explains why a console sometimes shows -0.
Step 5: the bitwise shortcut
You may see x | 0 used to drop decimals. | is a bitwise operator: it works on the number as 32 binary digits, and turning it into 32 bits drops the fraction. That limit causes trouble with big numbers:
console.log(4.7 | 0); // 4
console.log(-4.7 | 0); // -4
console.log((2 ** 31) | 0); // -2147483648
It acts like trunc for small numbers, but 2 to the power 31 (2,147,483,648) doesn't fit in 32 bits with a sign, so it wraps round to a large negative number. Use Math.trunc instead.
The full solution
The answer panel has ways and prints it for 4.7 and -4.7, followed by the two ties and the minus zero case. The checks cover each pair agreeing or not, and the bitwise wrap. Press Run to see the output.
const ways = (n) => ({
floor: Math.floor(n), // down, always
ceil: Math.ceil(n), // up, always
round: Math.round(n), // to the nearest, ties go up
trunc: Math.trunc(n), // fraction dropped, so towards zero
});
console.log('4.7 ->', ways(4.7));
console.log('-4.7 ->', ways(-4.7), '<- floor and trunc disagree');
console.log('ties: 2.5 ->', Math.round(2.5), ' -2.5 ->', Math.round(-2.5));
console.log('Math.round(-0.5) is', Math.round(-0.5), 'and Object.is(-0, 0) is', Object.is(Math.round(-0.5), 0));
Output
9 checks that prove this answer
You do not have to read these. They are part of the answer panel above, so pressing
Run there executes all 9 of them and prints ok for each.
That output is the proof, and the build fails if a single check fails.
test('a positive number', ways(4.7), { floor: 4, ceil: 5, round: 5, trunc: 4 });
test('a negative number is where they part', ways(-4.7), { floor: -5, ceil: -4, round: -5, trunc: -4 });
test('a tie rounds up', Math.round(2.5), 3);
test('so a negative tie rounds towards zero', Math.round(-2.5), -2);
test('Math.round(-0.5) is minus zero', Object.is(Math.round(-0.5), -0), true);
test('which JSON cannot tell from zero', JSON.stringify(Math.round(-0.5)), '0');
test('floor and trunc are the same for positives', Math.floor(4.7) === Math.trunc(4.7), true);
test('and different for negatives', Math.floor(-0.9) === Math.trunc(-0.9), false);
test('the bitwise trick truncates, but only within 32 bits', (2 ** 31) | 0, -2147483648);
The whole difference lives in the negatives. Math.floor(-4.7) is -5 and Math.trunc(-4.7) is -4, so code that used floor to "remove the decimals" quietly changes answer as soon as a negative arrives — a refund, a temperature, a debit.
Math.round(-2.5) being -2 surprises people who expect symmetry: the rule is "ties go up the number line", not "ties go away from zero". And x | 0 is a fast truncate that silently wraps past 2³¹, which is why the last assertion answers a large negative rather than the two billion you passed in.
Common mistakes
- Using
floorto remove decimals. It changes the answer for negative numbers. UseMath.truncwhen you mean "drop the decimal part". - Expecting
Math.round(-2.5)to be -3. Ties go up the number line, so it is -2. - Using
x | 0on big numbers. It only works within 32 bits and silently wraps beyond that.
Try it yourself
- Show
ways(-0.2)and work out each answer on the number line before running it. - Write
roundAwayFromZero(n)so that 2.5 gives 3 and -2.5 gives -3. (Hint:Math.signandMath.abs.) - A delivery van holds 12 boxes. Use the right function to work out how many vans 50 boxes need.