Developer Guide
Currency Utilities
currency/currency-helpers.ts is a small, dependency-free ISO-4217 helper
set. Use it whenever a UI or an export must display money correctly for a
given currency code — never hardcode €/$ or assume 2 decimals.
API
| Function | Signature | Returns |
|---|---|---|
getCurrencyInfo(code) | (code: string) | CurrencyInfo | null — {code, symbol, symbolNative, decimals, name} |
currencySymbol(code) | (code: string) | string — "EUR"→"€"; falls back to the code itself |
currencyDecimals(code) | (code: string) | number — "JPY"→0, "KWD"→3; falls back to 2 |
getAllCurrencies() | () | CurrencyInfo[] — the full supported table |
defaultCurrencyForLang(lang) | (lang: string) | string — uses the region of the lang ("en-US"→"USD", "it-IT"→"EUR"); bare "en" or unknown → "EUR" |
formatMoney(amount, lang, currency) | (number | null | undefined, string, string) | localized string via Intl.NumberFormat |
Examples
Code
What it does NOT do
- No conversion — it formats amounts, it doesn't exchange them. A
formatMoney(100, ..., "USD")of a €100 amount is just "100" in USD clothing; conversion rates are a service concern, not a formatter's. - No rounding logic of your own — always pass the raw amount; the formatter applies the currency's native precision.
Alternatives for other cases
- Need a bare number (CSV export, API payload)? Emit the raw numeric value — format only at the presentation edge.
- Need arbitrary decimal precision unrelated to the currency's standard?
Use
Intl.NumberFormatdirectly —formatMoneyintentionally pinsmin/maxFractionDigitsto the currency's real decimals.
Last modified on