Building Multi-Currency Systems That Do Not Lie

·1 min read·

Currency arithmetic in floating point is broken. Here is how to build currency systems that are always correct.

The most common currency system bug: storing monetary amounts as DECIMAL or FLOAT, performing arithmetic with floating-point numbers, rounding at the end.

0.1 + 0.2 in IEEE 754 floating point does not equal 0.3. At 2 million transactions per year, rounding errors compound into discrepancies your reconciliation system will flag.

The correct approach: store all monetary amounts as integers in the smallest currency unit (pence for GBP, cents for USD, kobo for NGN). £14.99 is stored as 1499. All arithmetic is integer arithmetic.

Convert to human-readable format only at display time. Never in the database. Never in business logic. This is the approach used by Stripe, Paystack, and every payment gateway I have integrated with.

— Dick Bassey | DevDick | 2022

Share this article
← Back to all writing