MINOR UNITS AND CURRENCIES
Not every currency has two decimals
Owoleaf's Amount type holds a number with two decimals, and converts from the card processor's integer amount by dividing by 100. A Tokyo shop refunds a purchase made on an Owoleaf card, and the processor sends { amount: 1000, currency: 'JPY' }. What does Owoleaf credit the customer?
Payment APIs send money as an integer count of the currency’s smallest unit: kobo for the naira, cents for the dollar. The power of ten that makes one major unit is the currency’s exponent, published in ISO 4217. Most currencies have 2. The yen and the won have 0. The Kuwaiti and Bahraini dinars have 3 (1,000 fils). Chile’s unit of account, CLF, has 4.
Code written for one currency hard-codes the 100: divide on the way in, multiply on the way out. Every naira test passes. Then a yen card refund is booked at a hundredth of its value, a yen transfer to the remittance partner goes out a hundred times too big, and a two-decimal column rounds away a dinar’s third digit.
// One constant, baked into every boundary.
const toMinor = (amount) => Math.round(amount * 100);
const fromMinor = (minor) => minor / 100;
// amount NUMERIC(14,2), no currency column
credit(account, fromMinor(record.amount)); // JPY 1000 -> 10.00// ISO 4217: NGN 2, USD 2, JPY 0, KRW 0, KWD 3, BHD 3, CLF 4 ...
type Money = { minor: bigint; currency: string };
// The wire already speaks minor units: keep them.
const m: Money = { minor: BigInt(record.amount), currency: record.currency };
// Only text and display need the exponent.
const e = EXPONENT[m.currency];
// minor BIGINT, currency CHAR(3), stored together Watch the two checks. In ASSUME 2 DECIMALS the boundary check turns red on the yen refund (¥1,000 booked as ¥10.00) and stays red through the payout (¥1,000 sent as ¥100,000); the precision check turns red when KWD 12.345 lands in a NUMERIC(14,2) column as 12.35. THE TEST SUITE runs the same code over naira and dollars and stays green throughout. The fix carries the integer and the currency together and looks up the exponent: all four amounts arrive unchanged.
This is the test suite: the two-decimal Amount type against naira and dollar records, and every check is green. Inject one record so that an amount changes value.
GOAL: an amount changes value at a boundary
Rewrite toMinor(text, currency, exponentOf). It turns a decimal string such as "12.345" into an integer number of minor units (return a number or a BigInt), using exponentOf(currency) (the ISO 4217 exponent). A string with more decimals than the currency has is an error: return null rather than drop or round a digit. The grader tries NGN, JPY, KWD, BHD and CLF.
Two more things the exponent table does not settle. A provider’s exponent can differ from ISO’s: Stripe’s currency guide notes that ISK and UGX are zero-decimal currencies but still have to be sent as two-decimal values with 00 at the end, so the table belongs to each API you talk to, not to the world. And the exponent tells you how to write an amount, not how to split one: dividing ¥1,000 three ways or converting it to naira still leaves a remainder, which is the residual cent lesson. Once the integers are right, the payment ledger keeps them balanced.
Any product that takes a second currency: card issuing and acquiring, cross-border payouts and remittance, marketplaces paying sellers abroad, travel and airline pricing, app-store and game prices. Look for * 100, / 100, toFixed(2) and NUMERIC(…,2) near anything called amount.
“Design the amount type for a payments API that will go multi-currency.” Say integer minor units plus an ISO 4217 code, stored and passed together; the exponent from a table (JPY 0, NGN 2, KWD 3), and a per-provider override for the ones that differ. Reject input with more decimals than the currency has instead of rounding it, and add a yen and a dinar to the test fixtures.
USED IN
Chapters that build on this lesson.
- Metric definitions and a single source of truthData and ML
- Cross-border payments: correspondent banks, FX, remittances, PAPSSPayments
- Double-entry bookkeeping for engineersPayments
- Money in software: minor units, rounding, currenciesPayments
- Project: a double-entry ledger for a walletPayments
- Stablecoins for cross-border paymentsPayments
- Product analytics: funnels, cohorts and instrumentationProduct Manager