Format currency and numbers with Intl
Locale-aware money, percent and compact number formatting using the built-in Intl.NumberFormat.
- JavaScript
- GreenSnippets
- MIT
- Executed with Node.js 22
function formatMoney(amountMinor, currency = "USD", locale = "en-US") {
return new Intl.NumberFormat(locale, { style: "currency", currency }).format(amountMinor / 100);
}
const formatPercent = (ratio, locale = "en-US") =>
new Intl.NumberFormat(locale, { style: "percent", maximumFractionDigits: 1 }).format(ratio);
const formatCompact = (n, locale = "en-US") =>
new Intl.NumberFormat(locale, { notation: "compact" }).format(n);Example / expected output
formatMoney(123456) // "$1,234.56"
formatMoney(123456, "EUR", "de-DE") // "1.234,56 €"
formatPercent(0.256) // "25.6%"
formatCompact(1_250_000) // "1.3M"How it works
Never build currency strings by hand: symbols, separators and placement differ per locale. Store money as integer minor units (cents) and divide only for display. In hot paths, create the formatter once and reuse it.
Related snippets
- Python Tested
Format bytes as a human-readable size
Convert a byte count into KB, MB, GB and so on with a configurable number of decimals.
- PHP Tested
Format bytes as a readable size in PHP
Convert a number of bytes to KB, MB or GB with a chosen precision.
- JavaScript Tested
Format bytes as KB, MB, GB
Convert a byte count into a short human-readable size such as 1.5 MB.
- JavaScript Tested
Remove duplicates from an array
Deduplicate primitives with a Set, or objects by a key or computed value, keeping the first occurrence.

