RESIDUAL CENT
Round the whole once, then share it out
An Owoleaf split request divides a ₦100.00 bill equally between Chidi, Folake and Ifeoma. The code rounds each share half-up to the kobo, then switches to banker's rounding (half to even) to be safe. What do the three wallets pay in total?
Money is stored in whole minor units. The naira’s minor unit is the kobo, a hundredth of a naira. Most exact shares land between two kobo, so something rounds. Round every part on its own and the rounding errors do not cancel: the parts miss the total by up to half a kobo per part.
The fix is a rule about where you round. Round the whole once. Then share it out with largest remainder: floor every exact share, and give the leftover kobo one each to the shares with the biggest fractions. The parts now sum to the total by construction. Someone gets the extra kobo: decide who in writing and post it as its own entry, so the books in PAYMENT LEDGER still sum to zero.
// Round every part on its own.
share = round(bill / n) // 10000 / 3 → 3333, three times
for line in invoice:
line.ngn = round(line.usd * rate) // six roundings
charge = round(invoice.usd * rate) // a seventh, independent rounding
// sum(shares) != bill, sum(lines) != charge.
// Banker's (half-even) rounding changes only exact ties: less bias, same gap.// Round the WHOLE once, then allocate it.
function allocate(total, weights): // integers, minor units
W = sum(weights)
parts = [floor(total * w / W) for w in weights]
left = total - sum(parts) // 0 <= left < n
for i in indices by (total * w_i mod W) desc, then bill order:
if left == 0: break
parts[i] += 1; left -= 1 // the residual, one unit each
return parts // sum(parts) == total, always
shares = allocate(bill, [1, 1, 1]) // 3334, 3333, 3333: Chidi gets the kobo
charge = round(invoice.usd * rate)
lines = allocate(charge, [line.usd ...]) Watch the two gaps in the middle column. HALF-UP PER LINE loses a kobo on the split and its six converted invoice lines come to 2 kobo more than the card charge. BANKER’S PER LINE changes one line, the $12.50 that lands exactly on 0.5 kobo, so the invoice gap shrinks to 1 kobo and the split gap does not move. The fix keeps both gaps at 0, and Chidi carries the residual kobo because the three remainders tie and he is first on the bill.
At ₦100.00 split three ways, half-up rounding loses a kobo. Move the bill and the number of wallets until half-up per share does the opposite: the wallets are debited more than the bill.
GOAL: the shares add up to more than the bill
Banker’s rounding earns its place elsewhere. Over thousands of roundings, half-up pushes every tie upwards and the total drifts one way; half-even sends half the ties each way, so the drift averages out. That is a cure for bias. Vancouver’s was the extreme case, truncation pushing every result down; any round-to-nearest removes most of it, and half-even removes the tie-shaped rest. It is not a cure for parts that must sum to a whole. Notice too that in the fix the two identical $4.99 seats get different naira amounts, ₦7,640.95 and ₦7,640.94. That is the price of an exact total. Say so on the receipt rather than hiding it.
Rewrite allocate(total, weights). It gets a non-negative integer total in kobo and an array of non-negative integer weights (at least one above zero). Return integer parts that sum exactly to the total, where each part is the floor or the ceiling of its exact share total × w / W, and the extra kobo go to the parts with the largest remainders. The grader runs equal splits, invoice lines, zero weights and many small lines.
Minor units and currencies looks at the unit itself: the kobo has two decimal places, and not every currency does. The same quoted rate, ₦1,531.2516 per dollar, is the USD/NGN mid in FX arbitrage, where a stale cross rate makes a loop pay.
Split bills and group payments, instalment plans (₦100,000 over three months), tax and fee lines on an invoice, multi-currency receipts, pro-rata refunds, interest shared across accounts, and the reconciliation report that shows a few kobo nobody can explain.
“Split ₦100 between three people” or “Implement allocate(total, ratios).” Show that independent rounding loses or creates a unit, then floor and hand out the remainder by largest fraction, with a fixed tie-break. Keep money in integer minor units, say that banker's rounding reduces bias but does not close the gap, and say where the residual is booked.
USED IN
Chapters that build on this lesson.
- Money in software: minor units, rounding, currenciesPayments
- Debugging a problem you have never seenSoftware Engineer
- The first ninety days: the codebase, the team and the first changeSoftware Engineer
- Testing money: invariants, reconciliation and evidence for a regulatorSoftware Testing and Quality