Accounting for DevelopersModule 4: Production financial systemsAdvanced4 min read
Multi-Currency Ledgers and Foreign Exchange in Code
Build a multi-currency ledger in SQL and TypeScript: transaction vs functional currency, minor-unit FX conversion, realised and unrealised FX gain or loss.
Course outline (16 lessons)
Before this lesson:Journal and General Ledger Database DesignBuilding a Double-Entry Accounting Engine in Code
In this lesson · 6 sections
A US company bills a client in euros, pays a supplier in yen, and files financial statements in US dollars. You can never add 100 EUR + 100 USD = 200—each currency is a separate unit of measure. A multi-currency ledger solves this by tracking every line in two currencies at once: the currency of the transaction and the company’s reporting currency.
Transaction currency vs functional currency
- Transaction currency: the currency on the invoice, bank transfer or receipt (for example,
EURorJPY). Customer statements and bank reconciliations happen here. - Functional currency: the primary currency in which the legal entity operates and prepares its trial balance (for example,
USD).
Every journal entry must balance to zero in functional currency (SUM(functional_amount_minor) = 0), and every single-currency entry must also balance in its transaction currency.
functional_minor = round_half_up(transaction_minor × 10^(f_exp − t_exp) × rate_micros ÷ 1,000,000)
- transaction_minor
- signed integer amount in transaction minor units
- f_exp, t_exp
- ISO 4217 minor-unit exponents (USD = 2, EUR = 2, JPY = 0, KWD = 3)
- rate_micros
- exchange rate scaled by 1,000,000 (e.g., 1.080000 USD/EUR → 1080000)
Extending the postings schema
Extend the postings table from journal and general ledger database design so each line permanently snapshots both currencies and the exact rate applied:
CREATE TABLE fx_rates (
base_currency char(3) NOT NULL,
quote_currency char(3) NOT NULL,
rate_date date NOT NULL,
rate_micros bigint NOT NULL CHECK (rate_micros > 0), -- quote per 1 base * 1e6
source text NOT NULL, -- e.g. 'ECB', 'OANDA'
PRIMARY KEY (base_currency, quote_currency, rate_date, source)
);
ALTER TABLE postings
ADD COLUMN functional_currency char(3) NOT NULL,
ADD COLUMN functional_amount_minor bigint NOT NULL CHECK (functional_amount_minor <> 0),
ADD COLUMN fx_rate_micros bigint NOT NULL CHECK (fx_rate_micros > 0),
ADD COLUMN fx_rate_at timestamptz NOT NULL;
Here amount_minor and currency store the transaction amount, while functional_amount_minor and functional_currency store the converted home-currency amount.
Safe FX conversion in TypeScript
Never multiply by a JavaScript number rate (10000 * 1.072), which introduces binary floating-point drift across currencies with different decimal places. Use BigInt scaled rates and ISO 4217 exponents:
const MINOR_EXP: Record<string, bigint> = { USD: 2n, EUR: 2n, GBP: 2n, JPY: 0n, KWD: 3n };
const SCALE = 1_000_000n;
function convertToFunctional(
txMinor: bigint,
txCurrency: string,
funcCurrency: string,
rateMicros: bigint, // functional units per 1 transaction unit, scaled by 1e6
): bigint {
if (txCurrency === funcCurrency) return txMinor;
const sign = txMinor < 0n ? -1n : 1n;
const absTx = txMinor < 0n ? -txMinor : txMinor;
const expDiff = MINOR_EXP[funcCurrency] - MINOR_EXP[txCurrency];
const num = expDiff >= 0n ? absTx * 10n ** expDiff * rateMicros : absTx * rateMicros;
const den = expDiff >= 0n ? SCALE : SCALE * 10n ** -expDiff;
const rounded = (num * 2n + den) / (den * 2n); // half-up
return sign * rounded;
}
When converting a multi-line entry, convert each line individually and check whether rounding created a 1-minor-unit functional imbalance; if so, post the 1-cent difference to an FX Rounding account rather than silently altering a line.
Realised vs unrealised FX gain or loss
Exchange rates move between issuing an invoice and collecting cash:
- Unrealised FX gain/loss happens at period-end when an open foreign-currency monetary balance (such as Accounts Receivable, Accounts Payable or foreign cash) is revalued at the closing spot rate. Because no cash changed hands yet, finance auto-reverses the revaluation on the first day of the next period.
- Realised FX gain/loss happens at settlement when cash actually lands at a different rate than the original invoice rate. It is permanent.
Northwind Robotics (fictional) — EUR invoice, month-end revaluation and settlement (USD books)
Northwind Robotics keeps its functional ledger in USD.
1. Issue €10,000.00 invoice (10 March, spot 1 EUR = 1.0800 USD): functional value = 10,000 × 1.0800 = $10,800.00.
| Account | Debit (Dr) | Credit (Cr) |
|---|---|---|
| Accounts Receivable (EUR) | $10,800 | |
| Product Revenue | $10,800 | |
| Total✓ Balanced | $10,800 | $10,800 |
2. Month-end revaluation (31 March, invoice unpaid, spot 1 EUR = 1.0950 USD): carrying value rises to $10,950.00 (+$150.00 unrealised gain). On 1 April, Northwind auto-reverses this entry so AR returns to $10,800.00.
| Account | Debit (Dr) | Credit (Cr) |
|---|---|---|
| Accounts Receivable (EUR) | $150 | |
| Unrealised FX Gain | $150 | |
| Total✓ Balanced | $150 | $150 |
3. Customer settles €10,000.00 (14 April, spot 1 EUR = 1.0720 USD): cash received = $10,720.00. Clearing AR at its $10,800.00 base value leaves an $80.00 realised FX loss.
| Account | Debit (Dr) | Credit (Cr) |
|---|---|---|
| Cash (USD) | $10,720 | |
| Realised FX Loss | $80 | |
| Accounts Receivable (EUR) | $10,800 | |
| Total✓ Balanced | $10,800 | $10,800 |
Every entry balances in USD, and the EUR transaction leg on Accounts Receivable nets cleanly to €10,000 − €10,000 = €0.
Real-world application
Global SaaS platforms, marketplaces and multi-entity ERPs rely on dual-currency postings to keep customer invoices exact in local currency while rolling up consolidated income statements in USD or EUR. At month-end, an automated revaluation job scans every open foreign-currency balance, compares its historical functional sum against the closing spot rate, and posts reversing revaluation entries before period close.
Recap
- Record both transaction currency and functional currency minor units plus the rate snapshot on every posting.
- Perform FX math with
BigIntscaled rates and ISO 4217 minor-unit exponents to eliminate floating-point drift. - Post unrealised FX entries at period-end (auto-reversed on day one of the next period) and realised FX entries upon cash settlement.
- Keep
SUM(functional_amount_minor) = 0on every journal entry, allocating any 1-unit conversion rounding explicitly.
Active recall
Quick check · Test yourself
Work out each answer in your head first, then reveal the explanation and mark whether you got it right.
1A USD-functional company issues a €5,000 invoice when 1 EUR = 1.10 USD ($5,500) and collects €5,000 later when 1 EUR = 1.06 USD ($5,300). What is the realised FX gain or loss?
Show worked answer
A realised FX loss of $200. Cash received is $5,300 (Dr Cash $5,300), Accounts Receivable is cleared at its original $5,500 carrying value (Cr AR $5,500), and the $200 difference is booked as Dr Realised FX Loss $200.
2Why must a multi-currency ledger store the exchange rate and converted functional amount at posting time instead of recalculating from a live FX table?
Show worked answer
Historical revenue and settlement amounts are fixed at the spot rate on the transaction date. Recalculating from today's rate would silently rewrite past financial statements and destroy the audit trail.
3How do you convert 150,000 JPY (0 decimal places) to USD (2 decimal places) at 0.006650 USD per JPY without floating-point errors?
Show worked answer
Scale for the minor-unit exponent difference (10^(2−0) = 100) and multiply using BigInt scaled by 1,000,000: (150000n * 100n * 6650n + 500000n) / 1000000n = 99750n minor units, or $997.50.
Key terms in this lesson
Lesson 9 of 16 · Accounting for Developers
Finished this lesson?
Mark it complete to track your progress through the course. Progress is saved in this browser only.
Previous lesson: Designing an Accounting API
Connected lessons
Educational content only—not accounting, tax, legal or investment advice. See our editorial policy or report an error via our corrections policy.