How do you store money correctly and what bugs are there?
Money is stored in NUMERIC(p, s) or as an integer in minimal units (cents), not in float with its binary error. A test checks the invariant: the parts sum to the whole and the remainder has an owner, because 100.00 / 3 three times gives 99.99. It also checks whether the API rejects extra digits or rounds silently, how it returns amounts in JSON, and whether currencies with other decimal counts are handled.
SQL · QA practice
A payment of 100.00 was split into three parts and the total came to 99.99. Where did the cent go?
A fee or a payment is divided into three equal parts: 100.00 / 3 = 33.33, three times. The total is 99.99, one cent has gone. In monthly reports such "micro-remainders" add up to an amount accounting cannot explain. Besides rounding, there is also the column type: in FLOAT money accumulates an error that a test with one amount does not show. This lesson is about how to store money in PostgreSQL, where the vanishing cents come from and what to test.
How it works and why
The types real and double precision (float8) store a number in binary. The decimal 0.1 has no exact binary form, just as 1/3 has no finite decimal one, so the closest approximation is stored. One operation is almost invisible, thousands accumulate a difference. NUMERIC(p, s) stores decimal digits exactly: p is the total number of significant digits, s the digits after the point. The other reliable approach is an integer in minimal units (cents) plus a separate currency column. PostgreSQL also has the money type, but its format depends on the server's lc_monetary setting, so it is not recommended for accounting.
SELECT SUM(0.1::float8) FROM generate_series(1, 10);
-- 0.9999999999999999
SELECT SUM(0.1::numeric) FROM generate_series(1, 10);
-- 1.0
SELECT 0.1::float8 * 3 = 0.3::float8; -- false
SELECT 0.1::numeric * 3 = 0.3::numeric; -- true
Ten times 0.1
double precision
The sum is 0.9999999999999999, and the "equals 1" check does not fire.
numeric
The sum is 1.0: decimal digits are stored exactly.
Example: splitting an amount
Even with NUMERIC rounding comes from nowhere: when an amount is divided, the remainder has to go somewhere.
SELECT round(100.00 / 3, 2) AS part, -- 33.33
round(100.00 / 3, 2) * 3 AS total; -- 99.99
-- the remainder goes to the first part
SELECT 100.00 - 2 * round(100.00 / 3, 2) AS first_part;
-- 33.34
The sum of the parts and the whole
| part | amount |
|---|---|
| 1 | 33.33 |
| 2 | 33.33 |
| 3 | 33.33 |
| total | 99.99 |
| part | amount |
|---|---|
| 1 | 33.34 |
| 2 | 33.33 |
| 3 | 33.33 |
| total | 100.00 |
A test for such a rule checks not a single part but the invariant: the sum of the parts equals the whole. A few more column type traps: writing 10.005 into NUMERIC(14,2) is silently rounded to 10.01 (PostgreSQL rounds "away from zero") rather than rejected; a value with more integer digits than the type allows gives an overflow error. So test separately what the API does with extra digits: rejects or silently rounds, and whether it matches the requirement. For float8 rounding on a half depends on the platform (most often to the nearest even), while for numeric it is away from zero: round(2.5::numeric) gives 3. Other systems may round differently, so take the rounding mode from the requirements.
Limits and alternatives
Currencies have different numbers of decimal places: JPY has none, and some currencies have three (per ISO 4217), so one NUMERIC(14,2) for all does not fit. An integer in minimal units avoids fractions but requires knowing each currency's scale and not forgetting to divide when displaying. In JSON a number becomes a double in many clients, so an API often returns the amount as a string ("10.05"): that is worth checking too. Currency conversion and percentages are tested separately, with an explicitly written rounding rule.
Common mistakes
- Testing only round amounts: 10, 100. Errors and rounding show on 0.01, 0.10, 99.99, 100.005.
- Comparing floating-point amounts with
=. - Rounding each part separately and not checking that the sum of the parts equals the whole.
- Assuming a currency always has two decimal places.
- Not finding out what the API does with extra digits: silent rounding hides the client's mistake.
Check yourself
SELECT 0.1::float8 * 3 = 0.3::float8 returns false. Why, and what does the same expression return with numeric?
Answer
0.1 in binary is approximate, so the product differs slightly from the approximate 0.3. In numeric the digits are decimal and exact, and the expression gives true.
A system divides 100.00 among three recipients. Which checks are needed?
Answer
The sum of the parts is 100.00, not 99.99; it is clear who gets the remainder; the result is the same on a retry; behaviour with amounts that do not divide evenly and with currencies that have another number of decimals.