Emmanuel Akinfulubi

JavaScriptMath

Implement banker's rounding by hand in JavaScript

Intermediate · 4 min read

Every time you round a halfway value up, you add half a unit that wasn't there. Do it once and nobody notices. Do it across thousands of invoice lines and the total drifts. Banks and accountants solved this long ago with banker's rounding, also called round half to even.

The rule: when a number is exactly halfway, round to whichever neighbor is even. So 0.5 becomes 0, 1.5 becomes 2, 2.5 becomes 2 and 3.5 becomes 4. Half the ties go down and half go up, so the extra halves cancel out.

In this tutorial you will write roundHalfEven(n) by hand, then prove it keeps a total that Math.round inflates. You need to know Math.floor, % and if.

Why "even" fixes the drift

Look at four ties: 0.5, 1.5, 2.5 and 3.5. They add up to exactly 8.

  • Math.round sends all four up: 1, 2, 3, 4. The total is 10, two too many.
  • Half to even gives 0, 2, 2, 4. The total is 8, which is right.

Even neighbors take turns: one tie goes down, the next goes up. Over a long list the gains and losses balance.

The plan

For any number n:

  1. Find the whole number below it: low = Math.floor(n).
  2. Find what is left over: fraction = n - low. This is between 0 and 1.
  3. If the fraction is more than 0.5, round up. If less, round down.
  4. Only when it is exactly 0.5, pick the even one of low and low + 1.

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

10 Banker's rounding, by hand

fair

Write round-half-to-even: a tie goes to whichever neighbour is even, so 0.5 -> 0, 1.5 -> 2, 2.5 -> 2, 3.5 -> 4. Then show it preserves a total that Math.round inflates.

Stuck? Three hints, smallest first
  1. 1. Take Math.floor(n) and look at the fractional part left over.
  2. 2. Only the exact 0.5 case needs the even rule; everything else rounds normally.
  3. 3. Sum [0.5, 1.5, 2.5, 3.5] two ways and compare against the exact total of 8.

Step 1: split the number into whole and fraction

Step 1

const n = 2.5;
const low = Math.floor(n);
const fraction = n - low;
console.log(low, fraction); // 2 0.5

Math.floor always goes down, so fraction is never negative, even for negative numbers. For -1.5, low is -2 and fraction is 0.5.

Step 2: the ordinary cases

Step 2

function roundHalfEven(n) {
  const low = Math.floor(n);
  const fraction = n - low;
  if (fraction > 0.5) return low + 1;
  if (fraction < 0.5) return low;
  return low; // not finished yet
}
console.log(roundHalfEven(2.4)); // 2
console.log(roundHalfEven(2.6)); // 3

Anything not halfway rounds to the nearest, as usual. Only the last line, the tie, is left to fix.

Step 3: the tie goes to the even side

A whole number is even when dividing it by 2 leaves nothing over: low % 2 === 0.

Step 3

function roundHalfEven(n) {
  const low = Math.floor(n);
  const fraction = n - low;
  if (fraction > 0.5) return low + 1;
  if (fraction < 0.5) return low;
  return low % 2 === 0 ? low : low + 1;
}
console.log([0.5, 1.5, 2.5, 3.5].map(roundHalfEven)); // [ 0, 2, 2, 4 ]

For 2.5, low is 2, which is even, so the answer is 2. For 1.5, low is 1, which is odd, so the answer is low + 1, which is 2. Notice 2.5 goes down to 2, where Math.round would give 3.

Step 4: negative numbers

Step 4

function roundHalfEven(n) {
  const low = Math.floor(n);
  const fraction = n - low;
  if (fraction > 0.5) return low + 1;
  if (fraction < 0.5) return low;
  return low % 2 === 0 ? low : low + 1;
}
console.log(roundHalfEven(-1.5)); // -2
console.log(roundHalfEven(-2.5)); // -2

For -1.5, low is -2, which is even, so the answer is -2. For -2.5, low is -3. -3 % 2 is -1, not 0, so it counts as odd and the answer is -3 + 1, which is -2. The rule works on both sides of zero without a special case.

Step 5: prove the totals

Step 5

function roundHalfEven(n) {
  const low = Math.floor(n);
  const fraction = n - low;
  if (fraction > 0.5) return low + 1;
  if (fraction < 0.5) return low;
  return low % 2 === 0 ? low : low + 1;
}
const halves = [0.5, 1.5, 2.5, 3.5];
const exact = halves.reduce((s, n) => s + n, 0);
const evenTotal = halves.reduce((s, n) => s + roundHalfEven(n), 0);
const roundTotal = halves.reduce((s, n) => s + Math.round(n), 0);
console.log(exact, evenTotal, roundTotal); // 8 8 10

reduce walks the array and keeps a running sum s, starting at 0. The exact total is 8. Half to even keeps it at 8. Math.round pushes it to 10.

Step 6: what counts as a tie

The fraction check only finds ties that are stored exactly. 0.5, 1.5 and 2.5 are stored exactly, because halves are easy in binary. A value like 1.005 is not: it's stored a hair below, so scaled by 100 it becomes 100.49999999999999. Its fraction is not exactly 0.5, and it rounds down by size instead. That is correct behavior, not a gap: the stored number really isn't a tie.

The full solution

The answer panel has roundHalfEven, compares it with Math.round on the four halves and on negatives, and adds up the totals. The checks underneath confirm each rounding and that the total stays at 8. Press Run to see the output.

The answer

function roundHalfEven(n) {
  const low = Math.floor(n);
  const fraction = n - low;
  if (fraction > 0.5) return low + 1;
  if (fraction < 0.5) return low;
  return low % 2 === 0 ? low : low + 1;      // an exact tie: take the even side
}

const halves = [0.5, 1.5, 2.5, 3.5];
const exact = halves.reduce((s, n) => s + n, 0);
const evenTotal = halves.reduce((s, n) => s + roundHalfEven(n), 0);
const roundTotal = halves.reduce((s, n) => s + Math.round(n), 0);

console.log('half to even :', halves.map(roundHalfEven));
console.log('Math.round   :', halves.map((n) => Math.round(n)));
console.log('exact total  :', exact);
console.log('even total   :', evenTotal, '<- unchanged');
console.log('round total  :', roundTotal, '<- two too many');
console.log('negatives    :', [-1.5, -2.5].map(roundHalfEven));

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('0.5 goes down to the even 0', roundHalfEven(0.5), 0);
test('1.5 goes up to the even 2', roundHalfEven(1.5), 2);
test('2.5 stays at the even 2, where Math.round gives 3', [roundHalfEven(2.5), Math.round(2.5)], [2, 3]);
test('3.5 goes up to the even 4', roundHalfEven(3.5), 4);
test('non-ties round to the nearest as usual', [roundHalfEven(2.4), roundHalfEven(2.6)], [2, 3]);
test('negative ties take the even neighbour too', [roundHalfEven(-1.5), roundHalfEven(-2.5)], [-2, -2]);
test('the four halves really total 8', exact, 8);
test('half-to-even preserves that total', evenTotal, 8);
test('while Math.round inflates it to 10', roundTotal, 10);

Half-up has a bias: over a long column of ties it gains half a unit every time, and the assertions show four values gaining 2. Half-to-even splits the ties evenly between up and down, so the bias cancels. That is why it is the default in IEEE-754 arithmetic, in Python's round, and in most accounting rules — hence the name.

The implementation detail worth noting is fraction === 0.5 only fires on an exactly representable half, which is the only case where it can. 0.5, 1.5, 2.5 are all exact doubles; 1.005 is not, so it never reaches the tie branch and rounds by magnitude instead. That is correct, not a gap.

Common mistakes

  • Using Math.trunc instead of Math.floor. For negatives, trunc goes the wrong way, so the fraction comes out negative and the checks break.
  • Checking low % 2 === 1 for odd. For negative numbers, % gives -1, so -3 wouldn't count as odd. Test === 0 for even instead.
  • Applying the even rule to every number. Only exact ties use it. 2.6 still rounds to 3.

Try it yourself

  1. Round [4.5, 5.5, 6.5, 7.5] both ways and compare the totals with the exact sum.
  2. Write roundHalfEven(n, places) for decimal places, using the scale-round-scale-back trick, and try roundHalfEven(0.125, 2).
  3. Find out what roundHalfEven(-0.5) returns, and check it with Object.is.

Keep practising

Newsletter

New writing, when it lands.

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