Emmanuel Akinfulubi

JavaScriptMath

Round half away from zero in JavaScript

Intermediate · 3 min read

Ask most people to round 2.5 and they say 3. Ask them to round -2.5 and they say -3. That rule is called round half away from zero: a value exactly halfway always moves away from zero, on either side. It is how most people round in their heads, and it keeps a charge and its refund the same size.

Math.round doesn't do this. It sends -2.5 to -2. In this tutorial you will write roundHalfAway(n) so that 2.5 becomes 3 and -2.5 becomes -3. You need to know Math.round, Math.abs and Math.sign.

What a "tie" is

A tie is a number exactly halfway between two whole numbers: 0.5, 1.5, 2.5, -2.5. Every rounding rule agrees on numbers that aren't ties: 2.4 goes to 2, 2.6 goes to 3. The rules only differ on what to do with a tie.

Math.round breaks every tie towards plus infinity. On positive numbers that means up, which looks right. On negative numbers it means towards zero, which looks wrong:

JavaScript

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

The plan

A positive tie already rounds the way we want. So:

  1. Take the size of the number, without its sign. That is Math.abs(n), which is never negative.
  2. Round the size. Because it's positive, a tie goes up.
  3. Put the sign back with Math.sign(n), which is 1 for positives and -1 for negatives.

Here is the exercise. Try it yourself first if you like, then read on and we will build it step by step.

9 Round half away from zero

fair

Math.round sends -2.5 to -2. Write roundHalfAway so that a tie always moves away from zero, making 2.5 -> 3 and -2.5 -> -3.

Stuck? Three hints, smallest first
  1. 1. Round the magnitude, then put the sign back.
  2. 2. Math.sign(n) * Math.round(Math.abs(n)) is the whole function.
  3. 3. Check -0.5: it should give -1, where Math.round gives -0.

Step 1: round the size

Step 1

console.log(Math.abs(-2.5));             // 2.5
console.log(Math.round(Math.abs(-2.5))); // 3

The size of -2.5 is 2.5, and rounding a positive tie gives 3. Now we have the right size, but we've lost the minus sign.

Step 2: put the sign back

Step 2

const roundHalfAway = (n) => Math.sign(n) * Math.round(Math.abs(n));
console.log(roundHalfAway(2.5));  // 3
console.log(roundHalfAway(-2.5)); // -3

Math.sign(-2.5) is -1, and -1 * 3 is -3. For 2.5 the sign is 1, so nothing changes. That one line is the whole function.

Step 3: check -0.5

-0.5 is the tie closest to zero, so it's the one most likely to go wrong.

Step 3

const roundHalfAway = (n) => Math.sign(n) * Math.round(Math.abs(n));
console.log(roundHalfAway(-0.5)); // -1
console.log(Math.round(-0.5));    // -0

Our function gives -1, as the rule says. Math.round gives -0, a "minus zero" that JavaScript keeps when a negative number rounds to zero.

Step 4: numbers that aren't ties

A new rounding function must not change anything except ties:

Step 4

const roundHalfAway = (n) => Math.sign(n) * Math.round(Math.abs(n));
console.log(roundHalfAway(-1.4)); // -1
console.log(roundHalfAway(-1.6)); // -2
console.log(roundHalfAway(0));    // 0

-1.4 is nearer -1, and -1.6 is nearer -2. Zero stays zero, because Math.sign(0) is 0 and anything times 0 is 0.

Step 5: why it matters for money

Say a customer is charged 150.5 kobo, and later refunded the same amount, written as -150.5:

Step 5

const roundHalfAway = (n) => Math.sign(n) * Math.round(Math.abs(n));
console.log([Math.round(150.5), Math.round(-150.5)]);       // [ 151, -150 ]
console.log([roundHalfAway(150.5), roundHalfAway(-150.5)]); // [ 151, -151 ]

With Math.round, the customer pays 151 and gets back only 150. The half kobo disappears every time. With roundHalfAway, the charge and the refund mirror each other.

Step 6: toFixed already does this

There is one built-in that rounds ties away from zero: toFixed. It returns a string, though.

JavaScript

console.log((2.5).toFixed(0));  // 3
console.log((-2.5).toFixed(0)); // -3

That's fine for display. When you need a number to keep calculating with, use roundHalfAway.

The full solution

The answer panel has the one-line roundHalfAway, prints it next to Math.round for 2.5, -2.5 and -0.5, and repeats the charge-and-refund example. The checks underneath confirm each value. Press Run to see the output.

The answer

const roundHalfAway = (n) => Math.sign(n) * Math.round(Math.abs(n));

console.log('2.5  ->', roundHalfAway(2.5), ' Math.round says', Math.round(2.5));
console.log('-2.5 ->', roundHalfAway(-2.5), 'Math.round says', Math.round(-2.5));
console.log('-0.5 ->', roundHalfAway(-0.5), 'Math.round says', Math.round(-0.5));
console.log('-1.4 ->', roundHalfAway(-1.4), 'and -1.6 ->', roundHalfAway(-1.6));
console.log('a refund of 150.5 kobo either way:', [roundHalfAway(150.5), roundHalfAway(-150.5)]);
console.log('toFixed(0) rounds the same way:', (2.5).toFixed(0), (-2.5).toFixed(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('a positive tie still goes up', roundHalfAway(2.5), 3);
test('a negative tie now goes down to -3', roundHalfAway(-2.5), -3);
test('where Math.round gives -2', Math.round(-2.5), -2);
test('-0.5 becomes -1', roundHalfAway(-0.5), -1);
test('non-ties are unaffected', [roundHalfAway(-1.4), roundHalfAway(-1.6)], [-1, -2]);
test('zero stays zero', roundHalfAway(0), 0);
test('a charge and its refund now round to the same magnitude', [roundHalfAway(150.5), roundHalfAway(-150.5)], [151, -151]);
test('which Math.round does not manage', [Math.round(150.5), Math.round(-150.5)], [151, -150]);
test('and toFixed(0) already behaved this way', [(2.5).toFixed(0), (-2.5).toFixed(0)], ['3', '-3']);

This is the rounding most people mean by "round to the nearest": ties move away from zero, so the magnitude of a value and its negation round identically. That symmetry is what you want when a refund must mirror a charge, and it is what Math.round denies you — 150.5 and -150.5 become 151 and 150, and the half-kobo has quietly gone to the house.

Math.sign(n) * ... also handles -0 correctly by accident: Math.sign(-0) is -0, and -0 * 0 is -0, so roundHalfAway(-0) stays -0 rather than flipping sign.

Common mistakes

  • Writing Math.round(n) * Math.sign(n). That rounds the signed number first, so the tie has already gone the wrong way. Round Math.abs(n).
  • Forgetting the sign entirely. Math.round(Math.abs(n)) turns -2.5 into 3, a positive number.
  • Testing only positive numbers. Every rounding rule agrees on 2.5. Always test -2.5 and -0.5 too.

Try it yourself

  1. Extend it to roundHalfAway(n, places) that rounds to decimal places, so roundHalfAway(-1.25, 1) gives -1.3.
  2. Run your function on [-1.5, -0.5, 0.5, 1.5]. Do the four answers add up to the same total as the four inputs?
  3. Write roundHalfTowardZero(n), where a tie moves towards zero instead: 2.5 becomes 2 and -2.5 becomes -2.

Keep practising

Newsletter

New writing, when it lands.

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