Home / Blog / Cost and billing
Why every price in our billing is an integer of micro-dollars
A cent is ten thousand micro-dollars. A month's invoice sums thousands of values each smaller than a cent — and floating point loses that argument in front of a customer.
A micro-dollar is a millionth of a dollar, which makes a cent 10,000 of them. Every amount in this billing system is an integer number of micro-dollars — a run-minute costs some whole number of them, well under a cent — and nothing is ever expressed as a decimal, anywhere, at any point.
- Every amount in our billing is an integer number of micro-dollars, and a cent is 10,000 of them.
- A run-minute costs a whole number of micro-dollars, well under a cent, so cents cannot be the working unit.
- It is bigint in TypeScript, BIGINT in Postgres and billing.Micros in Go: never a float.
- Micro-dollars become cents exactly once, per invoice, and the remainder is kept.
- A CHECK constraint refuses an invoice whose parts do not reconcile, and a property test holds the sum.
That reads like fastidiousness until you look at the shape of the arithmetic. A month’s invoice is the sum of thousands of values each individually smaller than a cent. Cents cannot be the working unit — you would round every line to zero. And a double accumulates error you do not discover in a test; you discover it when a customer’s invoice disagrees with the ledger by one cent that neither side can explain.
Integers all the way down
So: bigint in TypeScript, BIGINT in Postgres, billing.Micros in Go. Never a float, never a JavaScript number. The arithmetic lives in one module, and the pricing page’s estimator imports that module rather than re-deriving the sums — because the alternative is two implementations of the same formula, and one of them being wrong is a matter of time.
The rounding has to happen exactly once
Somewhere a total of micro-dollars becomes a charge in cents, because that is what a card network accepts. Doing it per line and summing gives a different answer from summing and doing it once, and the difference is real money in the aggregate. So the split is explicit and the remainder is kept:
total_micros = charged_cents * 10000 + remainder_micros
That is not a comment. It is a CHECK constraint on the invoices table. The database refuses to store an invoice whose parts do not reconcile, and the function that rebuilds a draft invoice re-adds the lines and refuses a set that does not sum. Two independent implementations of the same arithmetic — one in the application, one in the schema — have to agree before a row exists.
Why a property test rather than examples
The failure mode here is not “this case is wrong”, it is “the allocated cents do not add up to the total”, which example-based tests find only if you happened to pick the example. So the test asserts the property directly, over generated inputs: however the amount is split across lines, the cents allocated always sum to the invoice total. It has caught exactly the thing it was written for.
None of this is clever. It is the boring version, chosen because the interesting version fails in front of a customer. The prices themselves are on the GitHub Actions runner pricing page, read from the rows that do the billing.
The same arithmetic is what you see on an invoice: how you are billed follows a minute of usage to a line on the bill, and invoices and payments covers what happens when it is charged, and spending alerts and the spending cap say what you can set to bound a month before it reaches one. To put your own numbers through it, the calculator estimates what a month of your builds would cost beside a GitHub-hosted baseline.
Sources
- Numeric types (PostgreSQL documentation) (postgresql.org)
- BigInt (MDN) (developer.mozilla.org)
Frequently asked questions
Why not store prices as floating-point numbers?
A month's invoice sums thousands of values each smaller than a cent, and a double accumulates error you find only when an invoice disagrees with the ledger by a cent. Integers of micro-dollars add exactly.
Why micro-dollars rather than cents?
A run-minute costs well under a cent, so with cents as the working unit every line would round to zero. A micro-dollar is a millionth of a dollar, which makes a cent 10,000 of them.
When does the rounding to cents happen?
Exactly once, when a total of micro-dollars becomes a charge in cents. Rounding each line and summing gives a different answer from summing and rounding once, so the split is explicit and the remainder is kept.
How is the billing arithmetic tested?
With a property test over generated inputs: however an amount is split across lines, the allocated cents always sum to the invoice total. The database also refuses, with a CHECK constraint, an invoice whose parts do not reconcile.