In a recent development, last Friday three of us split a $87.44 dinner with an 18% tip. Instead of doing mental math at the table, I typed a ten-line Python script into my laptop. It said everyone owes $34.39. We collected that times three: $103.17. The bill was $103.18. We were a penny short, and the waiter watched me debug arithmetic in public.
It bugged me enough to spend part of the weekend on tip math properly: how tip percentages, totals and per-person splits interact, why two languages can round the same number differently, and when to just use a free online tip calculator instead of shipping code for it. Everything below was run for real on Python 3.11.1 and Node 22.13.1. The outputs are pasted, not invented.
A tip is two multiplications and an addition:.
All trivial, except money needs whole cents at the end, and rounding is where the pennies leak. Run the naive version and look at what floats do in between:.
Nothing is wrong with your machine. Binary floating point just can't represent 23.1246 exactly. Every method below has to decide what to do about that.
That last line is the dinner-table bug in miniature. Rounding each share down to a valid cent amount can drop money. With three people it's a penny; with a 12-person team lunch it can be several, and someone quietly covers the difference.
The second trap is subtler. round() in Python uses banker's rounding (round half to even) on the stored binary value, which is not always what the decimal on your screen claims:.
If you want money-grade rounding, use Decimal with an explicit rule. This is the version I keep now:.
Sources indicate that floor-splitting in integer cents makes the leftover explicit instead of silent. The official decimal docs cover the rounding modes if you want the full list.
It has been revealed that fine so far. But try the half-way case that Python rounded down:.
Python mentioned 2.67. JavaScript claims 2.68. Same input, two runtimes, two answers, and the reason is worth knowing. The double closest to 2.675 is actually 2.674999999999999822β¦, so Python's round() sees something below the half and stays at 2.67. But 2.675 * 100 comes out as exactly 267.5, and Math.round rounds halves up, so JavaScript lands on 268. Neither is "wrong"; they're rounding different binary values. If your backend is Python and your frontend is JS, the client can compute a different total than the server for the identical bill. I have seen this exact mismatch in a group-expense app's pull request review, and it took a while to convince people it wasn't "just floating point noise" but a real divergence.
Industry observers note that for display, toFixed(2) is fine; see the MDN notes on toFixed for its own rounding quirks. For storage, do what the Python version does: work in integer cents and format only at the edges.
Most tip situations are not engineering problems. You're standing in a restaurant, the check is on paper, and opening an editor to avoid mental arithmetic is its own kind of sad. For those moments I use this browser-based tip and bill-splitting tool: enter the bill, pick the tip rate, set the number of people, and it demonstrates the tip, the total and the per-person amount at once. Everything runs client-side, no signup and nothing uploaded, which is also why I don't mind using it for someone else's expense report where the amounts are none of my server's business.
The split view also settles the "do we round before or after dividing" argument faster than my Decimal script does.
1. Rounding each share independently loses money. Shown above: $103.18 split three ways rounded per person collects $103.17. Fix: floor-split in cents and assign the remainder to one person (or the last card). My earlier floor test printed remainder 0.019999999999996 when I forgot to work in cents first β the fix is total * 100 as an integer before dividing.
2. Half-way values round differently across languages. round(2.675, 2) is 2.67 in Python 3.11.1 and Math.round(2.675 * 100) / 100 is 2.68 in Node 22.13.1. Neither is "wrong"; they round different binary approximations. If two services must agree, agree on Decimal/integer-cents in both, not on each language's default.
3. Locale-formatted amounts parse silently into garbage. A check printed as 1.234,50 (European format) is deadly depending on runtime:.
It has been revealed that node accepts the string and computes a tip on β¬1.23 instead of β¬1,234.50. Python at least throws. Paste amounts into the tip calculator and you'll notice immediately because the output is visibly absurd; paste them into a parseFloat chain and you might not.
Industry observers note that the whole bug class comes from doing money math in binary floats and rounding wherever convenient. Pick one rounding rule, keep it in cents, and the penny stays on the bill instead of on your conscience.
More free browser-based utilities at utilvy.com.
Recent findings suggest that nice write-up! The penny-rounding issue bites a lot of split-bill scripts, so this is a handy reminder.
