Money in a billing system is never a float
A one-cent variance is not a rounding problem to be tolerated. It is the symptom of an arithmetic model that cannot be reconciled, and it will scale into a number somebody has to explain to an auditor.
A practice manager rings about a report. An account shows a balance of minus one cent. There is no transaction on it for one cent. Every individual number on the account looks right, and the total does not.
That cent is not a cosmetic defect. It is a message about how the system stores and divides money, and the same fault produces the twelve-dollar variance on a reconciliation report next quarter.
Store the smallest unit, as an integer
The rule is not negotiable and it is not new: money is an integer number of cents, carried as an integer all the way through storage and arithmetic, formatted into dollars only at the moment it is shown to a human or written into a file.
Binary floating point cannot represent one tenth exactly. Add ten-cent amounts enough times and the total drifts; compare two quantities that should be equal and they are not; divide and the error compounds. None of this is a library problem to be solved by more decimal places. It is a representation problem, and the fix is to stop representing money in a type that was designed for measurement.
In practice:
- Integers in the database — cents, not dollars with two decimal places in a float column. A fixed-point decimal type is acceptable where the database supports it properly; a float or a double is not.
- Integers in the code, with a money type that refuses to be added to a plain number and refuses to be added to an amount in another currency.
- Conversion at the edges only. Parsing an EDI amount, rendering a statement, writing a CSV for the accountant.
The edges are where the care is needed, because EDI amounts arrive as decimal strings and the parse is a conversion, not a cast. 1199.99 becomes 119999, and the conversion that does it must reject anything with more precision than it can carry rather than quietly truncating a third decimal place that the payer meant.
Division is the part that bites
Storing cents removes the drift. It does not remove the hard problem, which is that money has to be divided and cents cannot be split.
The shape it takes in a billing system: a payment, or an adjustment, or a patient's share, that applies to a claim and has to be distributed across its service lines. Three lines, a seventy-cent amount, and seventy does not divide by three.
An allocation routine has exactly two obligations:
- The parts sum to the whole. Always, exactly, with no residual hiding anywhere.
- The rule is deterministic. Run it twice on the same input and the same line gets the extra cent both times.
The usual implementation is the one everybody arrives at eventually: divide, floor, then distribute the remaining cents one at a time in a fixed order — largest charge first, then line number as the tie-break. Twenty-four cents on three lines of equal charge gives eight, eight, eight. Seventy gives twenty-four, twenty-three, twenty-three, and the line that got twenty-four is the same line every time.
What makes it defensible is not the elegance of the rule but that it is written down in one function, used everywhere money is split, and tested on the awkward cases: one cent across five lines, zero across three, a negative amount, a single line, an amount larger than the total charge.
Rounding has a direction, and it is a decision
Where a figure comes from a percentage — a coinsurance share, a contract that pays a proportion of a fee schedule, a withhold — the result is usually not a whole number of cents, and somebody has to decide what happens to the half.
Half-up is the conventional answer in this domain and it is almost certainly what the payer's own system does. Banker's rounding, which is the default in several languages and in more than one database, is not, and the difference shows up as a one-cent disagreement with the remittance on a percentage of claims large enough to notice.
Two things matter more than which rule you pick: that the same rule is applied in every place money is rounded, and that the rounding happens once rather than at every intermediate step. A figure rounded, stored, multiplied and rounded again drifts further than the arithmetic would suggest.
What bites once it is live
JSON has no money type. An amount that goes out as a JSON number comes back through a parser that may well have made it a double. Amounts cross service boundaries as integer cents or as strings, never as JSON numbers with decimal points.
Spreadsheet exports are floats again. Whatever you store, the moment a figure lands in a spreadsheet it is a float in somebody else's hands. Export cents or export formatted strings; expect the recipient's totals to differ from yours by a cent and have an answer for it.
Negative amounts are not an error case. Reversals, recoupments, refunds and credit balances are all genuinely negative money. A type or a column that cannot hold a negative, or a formatter that drops the sign, turns a recovery into a payment.
Percentages are not money. A contract rate of 82.5% of a fee schedule is a rate, stored with more precision than two decimal places, and multiplying an integer cent amount by it is the one place you need real care about the order of operations.
The database's default numeric type is probably wrong. Check it rather than assume it. A column that was created as a double four years ago is still a double today, and every amount that has passed through it has already been quietly approximated.
How to test it
- The allocation function, property-tested: for any amount and any number of lines, the parts sum to the amount, and the same input gives the same distribution.
- A claim where the payment, the contractual adjustment and the patient responsibility are each allocated across lines, asserting that each line's own arithmetic balances and that the claim's does.
- A round trip through the EDI parser and writer: the string from the file, parsed to cents, written back out, is the same string.
- A grep over the codebase for float arithmetic on anything named like money. Doing this once, properly, usually finds something.
- A reconciliation assertion that runs in production, not only in tests: the ledger's own totals tie out to the cent, and if they do not, that is an alert rather than a discrepancy somebody discovers in a report.
What we do
We carry money as integer cents with one allocation routine and one rounding rule used everywhere, convert only at the file and screen boundaries, and assert in production that the ledger ties out — because the first cent is cheap to find and the thousandth is a reconciliation project.
Building something that has to talk to a payer?
If any of the above is a problem you currently have, describe it and we will tell you what we would do about it.