QA Interview Hub · Knowledge base · Quizzes · Українська

SQL › SQL in QA work

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

Wrong

double precision

The sum is 0.9999999999999999, and the "equals 1" check does not fire.

Right

numeric

The sum is 1.0: decimal digits are stored exactly.

Figure 1. A float error is not visible in a test with one amount, it appears in sums and comparisons.

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

Each part rounded
partamount
133.33
233.33
333.33
total99.99
Remainder in the first part
partamount
133.34
233.33
333.33
total100.00
Figure 2. Each part rounded separately: a cent is lost. The remainder in one part: exactly 100.00 in total.

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.

In an interview. Junior: Money is not stored in FLOAT because it has a binary error: 0.1 plus 0.2 does not equal exactly 0.3. The right choice is NUMERIC or an integer in minimal units. I test amounts with cents and fee rounding. Middle/Senior: I check invariants: the sum of the parts equals the whole, the remainder has an owner. I look at what the API does with extra digits, how it returns amounts in JSON (string or number) and whether currencies with another number of decimals are covered. I take the rounding mode from the requirements, not from the behaviour of the type.

More articles in this chapter