MINOR UNITS AND CURRENCIES

Not every currency has two decimals

L214 MINMONEY
PREDICT

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.

▶ SIMULATION
amount typeAmount: a number with 2 decimals (×100 / ÷100 at every boundary)
ISO 4217 exponents seen—
✓ an amount means the same value on both sides of every boundary✓ a stored amount keeps every decimal its currency has
SCHEDULE
01[OWOLEAF]OWOLEAF: deploy: Amount = number with 2 decimals; minor = amount × 100
02[CARD]CARD PROCESSOR: Owoleaf card purchase in Lagos: { amount: 455075, currency: NGN }
03[CARD]CARD PROCESSOR: refund to an Owoleaf card from a Tokyo shop: { amount: 1000, currency: JPY }
04[PARTNER]REMITTANCE PARTNER: customer sends ¥1,000 to Japan; Owoleaf posts it to the partner
05[OWOLEAF]OWOLEAF: save a KWD 12.345 quote to the amount column
Start of assume 2 decimals scenario. 5 steps.
// 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

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.

BREAK IT NOT YET

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

FIX IT NOT YET

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.

Mental model:

An amount is two things, never one: an integer of minor units and the currency that says what one unit is worth. The number 100 belongs to the naira and the dollar, not to money. Keep the pair together in every column, message and function signature, and use the exponent only where a human reads or types the amount.

IN PRODUCTION

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.

IN INTERVIEWS

“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.

RECALL CARDS Tap to flip. They join your review deck when you mark the lesson complete.

USED IN

Chapters that build on this lesson.

NEXT: RESIDUAL CENT →