Skip to content
Digisys Alpha
Menu

PHP · payments · Laravel

Money in code: why integers beat decimals and floats

Store money as integers in the smallest unit of the currency (cents), keep the currency code beside it, and convert only for display. Floats cannot represent most decimal amounts exactly, and some currencies have no minor unit at all.

By Ahmed Memon · Published 2026-11-27 · Updated 2026-11-27 · 5 min read

Rounding bugs in payment code are rarely loud. They show up as a one-cent gap in a monthly report, or a refund that is slightly too large.

Why floats fail

Binary floating point cannot store values like 0.1 exactly, so adding amounts can drift. Use integers for stored amounts.

var_dump(0.1 + 0.2 === 0.3); // bool(false)

The rules we follow

  • Store amounts as integers in minor units, with a currency code in the same row.
  • Know your currencies: some, such as the Chilean peso, have no minor unit, so 1,000 means 1,000 pesos, not 10.00.
  • Do percentage and tax maths in one place, with a clear rounding rule, and test it.
  • Send the provider the amount format it expects. Many APIs take the smallest unit, others take decimals.
  • Convert to a display string only at the edge, in the view or API response.

Splitting amounts

When you split a payout or invoice, the parts must add back to the total. Give the leftover cent to a defined recipient instead of rounding each part independently.

Tests worth writing

  • Totals with many line items.
  • Refunds that equal the original amount.
  • Zero-decimal and two-decimal currencies side by side.

Questions

Is the decimal type in MySQL good enough?

It stores exact decimals, but integer minor units avoid conversion mistakes between database, code and provider APIs.

What about currency conversion?

Keep the original amount and currency, store the rate used, and never overwrite the original value.

Need this built or reviewed?

Book a 30-minute discovery call or message us on WhatsApp. We reply within one business day.