Emmanuel Akinfulubi

JavaScriptMath

Rounding negative numbers in JavaScript

Intermediate · 4 min read

In the last exercise, Math.floor, Math.ceil, Math.round and Math.trunc behaved nicely on positive numbers. Negative numbers are where they split apart. Temperatures below zero, refunds, debts and positions to the left of a chart's origin are all negative, so this matters in real code.

In this tutorial you will round -1.7, -1.5 and -0.5 with all four functions, see which answers change, and find the one result that comes out as minus zero. You need to know what the four functions do on positive numbers.

"Down" means towards minus infinity

On a positive number, "down" and "towards zero" are the same direction. On a negative number they are opposites.

Picture the number line around -1.7: -2 is on its left and -1 is on its right. "Down" means left, towards minus infinity, so down from -1.7 is -2. "Towards zero" means right, so towards zero from -1.7 is -1.

  • Math.floor always goes left (down).
  • Math.ceil always goes right (up).
  • Math.trunc always goes towards zero.
  • Math.round goes to the nearest, and breaks a tie by going right.

Here is the exercise. Try it yourself first if you like, then read on and we will go through it together.

6 The same four functions on a negative number

fair

Now round -1.7, -1.5 and -0.5. Three of the four give a different answer from the positive case. Find the one result that is -0.

Stuck? Three hints, smallest first
  1. 1. "Down" means towards minus infinity, so Math.floor(-1.7) is not -1.
  2. 2. Math.round always breaks a tie towards plus infinity, which on a negative means towards zero.
  3. 3. Math.round(-0.5) is -0. Compare it with Object.is, since -0 === 0.

Step 1: round -1.7 four ways

Use the same helper as last time:

Step 1

const show = (n) => ({
  floor: Math.floor(n),
  ceil: Math.ceil(n),
  round: Math.round(n),
  trunc: Math.trunc(n),
});
console.log(show(-1.7)); // { floor: -2, ceil: -1, round: -2, trunc: -1 }

A natural guess is that Math.floor(-1.7) is -1, because you "drop the .7". That is what trunc does, not floor. Floor goes down, and down from -1.7 is -2.

Compare with 1.7, where floor gave 1 and ceil gave 2. On the negative side, floor's answer has a bigger size and ceil's has a smaller one.

Step 2: trunc now agrees with ceil

On positives, trunc and floor always agreed. On negatives, trunc and ceil agree instead, because both move towards zero:

Step 2

console.log(Math.trunc(-0.1) === Math.floor(-0.1)); // false
console.log([-0.1, -1.5, -9.9].every((n) => Math.trunc(n) === Math.ceil(n))); // true

So "trunc is the same as floor" is only half true. Pick trunc when you mean "drop the fraction", and floor when you mean "go down".

Step 3: the halfway cases

Now -1.5 and -2.5, which sit exactly between two whole numbers:

Step 3

console.log(Math.round(-1.5)); // -1
console.log(Math.round(-2.5)); // -2
console.log(Math.round(2.5));  // 3

Math.round always breaks a tie towards plus infinity, to the right. On a negative number, right means towards zero, so -1.5 becomes -1 and -2.5 becomes -2.

That makes rounding lopsided. 2.5 becomes 3 but -2.5 becomes -2: the same size in, different sizes out. If you round a charge of 2.5 and its refund of -2.5, they no longer cancel. Exercises 9 and 10 fix this.

A simple way to remember the rule: Math.round(x) behaves like Math.floor(x + 0.5). For -1.5 that is Math.floor(-1), which is -1.

Step 4: find the minus zero

JavaScript has two zeros, 0 and -0. They are equal with ===, but they are different values. Round -0.5:

Step 4

console.log(Math.round(-0.5)); // -0
console.log(Object.is(Math.round(-0.5), -0)); // true
console.log(Math.round(-0.5) === 0);          // true

The nearest whole numbers are -1 and 0, the tie goes right, so the answer is zero. But it keeps the minus sign from the input, so it is -0. Object.is is a strict comparison that can see the difference; === can't.

Math.trunc makes one too:

JavaScript

console.log(Object.is(Math.trunc(-0.1), -0)); // true

Step 5: getting rid of minus zero

Minus zero behaves like zero in sums, and String(-0) is '0', so it usually stays hidden. If you need a plain zero, check for zero and replace it:

Step 5

const plainZero = (n) => (n === 0 ? 0 : n);
console.log(Object.is(plainZero(Math.round(-0.5)), 0)); // true
console.log(plainZero(-3)); // -3

n === 0 is true for both zeros, so both become a plain 0, and every other number passes through.

The full solution

The answer panel runs the show helper on -1.7, -1.5, -2.5 and -0.5, and checks for minus zero with Object.is. The checks underneath confirm each rule, including that trunc agrees with ceil on every negative fraction. Press Run to see the output.

The answer

const show = (n) => ({
  floor: Math.floor(n),
  ceil: Math.ceil(n),
  round: Math.round(n),
  trunc: Math.trunc(n),
});

console.log('-1.7 ->', show(-1.7));
console.log('-1.5 ->', show(-1.5), '<- the tie went towards zero');
console.log('-2.5 ->', show(-2.5));
console.log('-0.5 ->', show(-0.5), '<- round gave -0');
console.log('round(-0.5) is -0?', Object.is(Math.round(-0.5), -0));
console.log('and trunc(-0.1) is -0 as well:', Object.is(Math.trunc(-0.1), -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.

9 checks

test('floor always goes down, so -1.7 becomes -2', Math.floor(-1.7), -2);
test('ceil always goes up, so -1.7 becomes -1', Math.ceil(-1.7), -1);
test('trunc drops the fraction, giving -1', Math.trunc(-1.7), -1);
test('round takes the nearest, which really is -2', Math.round(-1.7), -2);
test('a negative tie rounds towards zero: -1.5 becomes -1', Math.round(-1.5), -1);
test('and -2.5 becomes -2, so rounding is not symmetric', [Math.round(2.5), Math.round(-2.5)], [3, -2]);
test('round(-0.5) is -0, not 0 and not -1', Object.is(Math.round(-0.5), -0), true);
test('trunc and floor disagree on every negative fraction', Math.trunc(-0.1) === Math.floor(-0.1), false);
test('while trunc and ceil agree on all of them', [-0.1, -1.5, -9.9].every((n) => Math.trunc(n) === Math.ceil(n)), true);

Math.round is specified as Math.floor(x + 0.5), and that single definition explains everything odd here: -1.5 + 0.5 is -1, so the tie moves towards plus infinity, which on the negative side is towards zero. Rounding 2.5 and -2.5 therefore gives 3 and -2, magnitudes 3 and 2. On a column of signed figures that is a real bias, which is what exercises 9 and 10 fix.

-0 leaks out of Math.round(-0.5) and Math.trunc(-0.1). It behaves as zero in arithmetic and in ===, but String(-0) is "0" while (-0).toFixed(1) is "-0.0", so a report can end up printing a negative zero. If that matters, normalise with n + 0... which does not help — use n === 0 ? 0 : n.

Common mistakes

  • Thinking Math.floor(-1.7) is -1. Floor goes down, towards minus infinity, so it is -2. Use Math.trunc to drop the fraction.
  • Expecting Math.round to be symmetric. Math.round(2.5) is 3 and Math.round(-2.5) is -2.
  • Testing for minus zero with ===. -0 === 0 is true. Use Object.is(n, -0).

Try it yourself

  1. Run show(-2.5) and show(-0.4). Predict each value before you look.
  2. Write roundTowardZero(n) without using Math.trunc. (Hint: pick floor or ceil depending on the sign.)
  3. Find another value that Math.ceil turns into -0.

Keep practising

Newsletter

New writing, when it lands.

Occasional notes on shipping software with AI agents. No schedule, no filler, unsubscribe in one click.