(Note · · 5 min read)

The payroll total is off by two cents

Somebody reconciles a report against the bank and finds a discrepancy of a few cents. Nothing is broken in a way that throws an error, which is what makes it expensive: the sum has been slightly wrong for months, and every statement produced in that time is slightly wrong too.

By Md. Habibur Rahman Shohel, full-stack & AI product engineer in Dhaka

The cause is representation, not arithmetic

A binary floating point number cannot hold most decimal fractions exactly, in the same way that a decimal cannot hold a third exactly. Nothing has gone wrong when a tenth plus two tenths is not exactly three tenths — that is what the format is. Add a few thousand such numbers and the residue becomes visible in a total.

Payroll is where it shows first, because an amount is multiplied by a rate, apportioned, taxed and summed across hundreds of people, and every step carries the error forward into the next.

Store integers or a decimal type

Two storage choices are correct. Keep amounts as integers in the currency's smallest unit — cents, paisa — and do the arithmetic in whole numbers. Or use the database's exact decimal type with a declared precision and scale, and a matching arbitrary-precision library in the application.

What is never correct is a floating point column, or passing money through a language's default number type on the way in or out. A decimal column read into a float in the application has already lost the guarantee the column was there to provide.

Decide where rounding happens, once

Rounding is not a detail to be left to whichever function happens to run last. Round at defined points — at the end of a calculation, not in the middle of one — and write the rule down, including which direction halves go.

Rounding at every intermediate step and rounding once at the end give different answers, and both are defensible. What is indefensible is not knowing which the system does.

Apportioning must add up

Splitting an amount across people or periods rarely divides evenly, and rounding each share independently produces a set of shares that does not sum to the original. The remainder has to go somewhere explicitly.

Distribute the remainder rather than losing it
total = 10000            # cents
shares = 3
base = total // shares   # 3333 each
rest = total % shares    # 1 cent left over

# give the remainder out, one unit at a time, by a rule
# you can state: 3334, 3333, 3333  ->  sums to 10000

Which share receives the extra unit should follow a stated rule, so the same input always produces the same split and a recalculation does not quietly move a cent between two people.

Keep the currency with the amount

An amount without its currency is a number waiting to be added to the wrong thing. Store the currency alongside and refuse to add across currencies without an explicit conversion, with the rate used recorded on the record rather than looked up again later.

A conversion that is recomputed at read time means a historical document changes its figures when a rate moves. Record the rate at the moment the amount was fixed.

Test with the figures that break it

Write the test around an amount that divides badly and a rate with more decimal places than the result — that is where the discrepancy appears. On the payroll platform I maintain, this is covered by part of a suite of roughly 4,900 automatic backend tests, because the arithmetic is the product.

Common questions

Why is my money total off by a few cents?

Because binary floating point cannot represent most decimal fractions exactly. Each amount is slightly off, and summing hundreds of them makes the residue visible. The arithmetic is not failing — the storage format cannot hold the values.

How should money be stored in a database?

Either as integers in the currency's smallest unit with whole-number arithmetic, or in the database's exact decimal type with declared precision and a matching arbitrary-precision library in the application. Never a floating point column, and never read into a float on the way out.

How do I split an amount so the shares still add up?

Compute the base share by integer division, then distribute the remainder one unit at a time by a stated rule. Rounding each share independently produces shares that do not sum to the original, and the rule must be deterministic so a recalculation does not move a unit between people.

Should currency conversion be recalculated at read time?

No. Record the rate used on the record at the moment the amount was fixed. Recomputing at read time means a historical invoice or payslip changes its figures whenever the rate moves.

Need something like this built?