Store Money as Integers: Three Bugs I Keep Seeing in Billing Code

●2
calendar_today ago • schedule1 min read

Most billing bugs I have debugged were not in the payment provider. They were in how the app stored and calculated amounts. Three patterns come up again and again.

1. Floats for amounts

0.1 + 0.2 === 0.3 // false

A float cannot represent most decimal fractions exactly, so totals drift by a cent here and there. Store amounts as integers in the smallest unit of the currency (cents for USD) and only format them for display.

const priceCents = 1999;            // $19.99
const totalCents = priceCents * 3;  // 5997, exact

2. Assuming every currency has two decimals

JPY has zero decimal places, KWD has three. If your code always divides by 100, a ¥1,000 charge shows up as ¥10. Keep the exponent next to the currency:

const MINOR_UNITS = { USD: 2, EUR: 2, JPY: 0, KWD: 3 };
const toDisplay = (amount, currency) =>
  (amount / 10 ** MINOR_UNITS[currency]).toFixed(MINOR_UNITS[currency]);

3. Rounding at the wrong step

If you apply a 10% discount to each line item and round each one, the order total can differ from rounding once at the end. Pick one rule, write it down, and use the same function everywhere, including refunds. A partial refund that rounds differently from the original charge is how you end up refunding one cent more than you collected.

A small checklist

  • Amount columns are integers (BIGINT), never FLOAT or DOUBLE.
  • Every amount is stored with its currency code.
  • One shared function does rounding; tests cover JPY and a three-decimal currency.

What is the strangest money bug you have shipped?

Want more #webdev & #programming deep dives?

Join 6,047+ developers sharing real benchmarks, code breakdowns, and projects. Free forever.

More Posts

TypeScript Complexity Has Finally Reached the Point of Total Absurdity

Karol Modelski - Apr 23

I’m a Senior Dev and I’ve Forgotten How to Think Without a Prompt

Karol Modelski - Mar 19

Delivering Database Changes

Steve Fentonverified - Jul 22

How I Built a React Portfolio in 7 Days That Landed ₹1.2L in Freelance Work

Dharanidharan - Feb 9

Three Design-to-Code Rules Most Developers Ignore (That Will Make You a Better Engineer)

Joemetry - Sep 26
chevron_left
121 Points • 2 Badges
1Posts
0Comments
Working on Waffo Pancake, a merchant of record for developers selling software and digital products ... Show more

Related Jobs

View all jobs →

Commenters (This Week)

1 comment
1 comment

Contribute meaningful comments to climb the leaderboard and earn badges!