Splitting Money Fairly, to the Cent
Divide $10.00 three ways and you get $3.33 x 3 = $9.99 - someone eats a cent. Splitting money so it always reconciles, and settling who-owes-whom with the fewest payments, are real, solvable problems.
Splitting money is where naive code loses (or invents) pennies. Ten dollars three ways is $3.33 each, but three times that is $9.99 - a cent has vanished. Real systems can't afford vanishing cents: a payout, a revenue split, a group bill must add back to exactly the original total. This session covers two related problems: allocating an amount so the pieces always reconcile, and settling a web of debts with the fewest payments.
The largest-remainder method
The fix for the vanishing cent is to give the leftover units to someone, deterministically, rather than dropping them.
split $10.00 (1000 cents) three ways:
base = 1000 // 3 = 333 each -> 333, 333, 333 (sums to 999)
leftover = 1000 - 999 = 1 cent
give the 1 leftover cent to the largest remainder -> 334, 333, 333
total = 1000 exactlyallocate(), and larzsplit builds itemized bills on top of it - each line shared by different people, with tax and tip allocated proportionally, all reconciling to the cent.Settling a web of debts
A related problem: after a trip, everyone owes everyone. Paying every debt directly is wasteful - many payments cancel out.
| Approach | What it does | Payments |
|---|---|---|
| Pay every debt directly | Ann->Bo, Bo->Cara, Cara->Ann... | Many, wasteful |
| Net each person first | Reduce to one balance each (+ owed / - owes) | Fewer |
| Settle the balances | Biggest debtor pays biggest creditor, repeat | <= n-1 |
| Optimise | Find groups that already cancel out | Provably minimum |
First <b>net</b> everyone to a single balance (if Ann owes Bo $10 and Bo owes Ann $4, that's just Ann owes Bo $6). Then settle: repeatedly have the biggest debtor pay the biggest creditor. That clears everyone in at most n-1 payments. The stack's <a href='/stack/larznetting'>larznetting</a> does exactly this, and can find the provably minimum number of payments by spotting groups that cancel among themselves - the 'simplify debts' feature done correctly.
Money that always adds up
- Never drop the leftover cent - allocate it deterministically (largest remainder).
- Splits must reconcile: the pieces always sum back to the exact total.
- Net before you settle debts, then pay biggest-debtor-to-biggest-creditor.
- Study the tools: larzmoney.allocate, larzsplit, larznetting.
๐งฉ Do it with LarzOS
Turn this lesson into action with the exact free tools and AI workflows for the job:
๐ฏ Key takeaways
- Dividing money naively loses cents ($3.33 x 3 = $9.99); allocate the leftover deterministically instead.
- The largest-remainder method hands leftover units to the largest fractional shares, so splits always reconcile.
- To settle group debts, net everyone to one balance, then pay biggest-debtor-to-biggest-creditor (<= n-1 payments).
- larzmoney.allocate, larzsplit and larznetting implement these correctly - study them.
Frequently asked
Who should get the leftover cent - isn't giving it to one person unfair?
Giving the leftover unit to specific shares is the fairest option available, because the alternative - dropping it - is worse (the total no longer reconciles, and money is lost), and the leftover is genuinely just one indivisible smallest unit that has to go somewhere. When a total doesn't divide evenly into the smallest currency unit, there is by definition a remainder of one or a few smallest units (cents) that cannot be split further, so some shares must be one cent larger than others - there's no way to make them perfectly equal, because you can't give someone a third of a cent. The question is only which shares get the extra cent(s), and the largest-remainder method answers it in the fairest, most principled way: it gives the leftover units to the shares whose exact (unrounded) value had the largest fractional part - that is, the shares that were 'closest' to deserving the next whole cent. This minimises the total rounding error and is deterministic and transparent, so it's defensible and reproducible. The difference is one cent, which over a single split is negligible, and because the method is systematic rather than arbitrary, no one is being singled out unfairly - the cent goes to whoever was mathematically closest to earning it. For contexts where even that appearance matters, you can rotate who gets the leftover across many splits, or disclose the rule, but the core point stands: the leftover cent must go to someone (dropping it breaks reconciliation and loses money), and largest-remainder allocation assigns it in the fairest, most justifiable way. Perfect equality is impossible when a total doesn't divide evenly; correct-and-reconciling with a principled tiebreak is the right goal, and that's what allocation achieves.
Finding the 'minimum number of payments' to settle debts - is that always easy?
Netting everyone to a single balance and then settling greedily (biggest debtor pays biggest creditor) is easy and fast, and it always clears everyone in at most n-1 payments, which is good enough for almost all real uses. Finding the truly minimum number of payments, however, is a genuinely hard problem in the general case - it's related to partitioning the balances into the maximum number of subgroups that each sum to zero (each such group can be settled internally, and more groups means fewer total payments), and that partitioning problem is NP-hard, meaning there's no known fast algorithm that always finds the optimum for large inputs. In practice this rarely matters, for two reasons. First, the greedy n-1 solution is already quite good and often optimal or near-optimal, so most 'split the bill' apps just use it and are fine. Second, real groups are small - a trip with 5 or 10 people - and for small inputs you can afford the exponential search to find the exact minimum, because the numbers involved are tiny. So the practical approach, which the stack's larznetting takes, is: net first (always), settle greedily by default (fast, <= n-1, correct), and offer an 'optimise' mode that finds the provable minimum for small groups by searching for the cancelling subgroups, with a guard so it doesn't attempt the expensive search on very large inputs. That gives you a correct, fast settlement for everyone and the genuinely minimal one when the group is small enough to compute it. The honest picture: settling debts correctly is easy; settling them in the absolute fewest payments is theoretically hard but practically manageable because real groups are small. Knowing the distinction - that greedy is good and cheap, optimal is nice but expensive - is part of handling this well, and it's a nice example of a real problem where 'good enough, fast' and 'provably optimal, slow' both have their place.
Related lessons
Money Is Not a Float
The first rule of money in software: never store it as a floating-point number. $0.10 + $0โฆ
Money & Payments in SoftwareKeeping the Books: Double-Entry
Once money moves, you need to record it so nothing is lost. Double-entry bookkeeping - eveโฆ
Make Money Online7 Passive Income Ideas, Ranked by Real Effort vs Reward
'Passive' is a myth for the first year. Here's what each idea actually takes to build โ anโฆ