Learning center

Convert prices between cents and decimals without losing a cent

When one release stores prices as integer cents and the next as a decimal amount, the code that converts between them runs on every dual write. Converting through a floating-point number and truncating loses a cent on 65,624 of the million prices from 0.00 to 9,999.99. Round instead of truncating, or convert in the database with numeric, and reconcile the two columns before trusting either.

The truncation#

Release 2026.11.3 writes price_cents integer. Release 2026.11.4 adds price numeric(12,2) and writes both, converting in application code. In Python 3.11:

>>> 19.99 * 100
1998.9999999999998
>>> int(19.99 * 100)
1998
>>> round(19.99 * 100)
1999

19.99 has no exact binary representation, so the product lands just below 1999, and int() cuts it off. JavaScript does the same with Math.trunc or | 0, and Math.floor does it for positive prices. We counted every price from 0.00 to 9,999.99:

ConversionPrices that lose a cent
int(price * 100) in Python65,624 of 1,000,000
Math.floor(price * 100) in Node 2465,624 of 1,000,000
round(price * 100) in Python0

The first affected prices are 0.29, 0.57, 0.58, 1.13, 1.14 and 1.15. Nothing fails: every converted value is a valid integer, one cent short.

Convert in the database#

PostgreSQL's numeric type is exact, so a conversion there has no rounding step to get wrong. We filled a million rows with every cent value and compared, on PostgreSQL 16.15:

SELECT count(*) FILTER (WHERE price * 100 <> price_cents) AS numeric_mismatches,
       count(*) FILTER (WHERE trunc((price_cents / 100.0)::float8 * 100) <> price_cents) AS float_trunc,
       count(*) FILTER (WHERE round((price_cents / 100.0)::float8 * 100) <> price_cents) AS float_round
FROM line_items;
Exact numericFloat, truncatedFloat, rounded
065,6240

A cast from float8 to integer in PostgreSQL rounds: (0.29::float8 * 100)::int is 29, while trunc(...) gives 28. A trigger that fills price_cents from price as (NEW.price * 100)::integer on a numeric column is exact, and it covers the old release's writes too, as use a trigger when the old release must keep filling the new column shows for another column.

Do not reinterpret the old column#

Changing the meaning of price_cents in place, for example to store whole currency units for a new currency, cannot work while both releases run. The old release has no way to tell which rows use which unit. Add a new column with a new name, and keep the old one in its old unit until no release reads it.

The reconciliation#

SELECT count(*) FROM line_items WHERE price * 100 IS DISTINCT FROM price_cents;

Run it during the dual-write period and after any backfill. Pass when it returns zero for rows written by both releases. A count of a few percent that clusters on prices like 0.29 points at a truncating conversion in some writer. Find every backfill mismatch with IS DISTINCT FROM explains why the comparison uses IS DISTINCT FROM.

One month of free code review.

New organizations get one calendar month of hosted code review for private and public GitHub repositories. No card or cloud connection required. Eligibility and fair-use terms.

Start free with GitHub

Related reading