Sub topic 15
Numbers, Units, and Currencies with Intl.NumberFormat
This chapter assumes you know how to create Intl formatters with a locale tag and an options object, and how to read resolvedOptions(). Those topics were covered in the Intl basics chapter. Dates often appear next to numbers, such as prices, durations, distances, and percentages. Intl.NumberFormat formats those numbers using the conventions of each locale.
The working model
A NumberFormat instance pairs a locale with a style. The locale decides grouping separators, the decimal mark, digit shapes, and where symbols go. The style decides what kind of number you are formatting.
| style | Required extra option | Example output (en-US) |
|---|---|---|
| `"decimal"` (default) | none | `1,234.5` |
| `"currency"` | `currency` (ISO 4217 code) | `$1,234.50` |
| `"percent"` | none | `26%` |
| `"unit"` | `unit` (a supported unit identifier) | `3 hours` |
const n = 1234567.891;
console.log(new Intl.NumberFormat("en-US").format(n)); // "1,234,567.891"
console.log(new Intl.NumberFormat("de-DE").format(n)); // "1.234.567,891"
console.log(new Intl.NumberFormat("en-IN").format(n)); // "12,34,567.891" (Indian grouping)
const eur = new Intl.NumberFormat("de-DE", { style: "currency", currency: "EUR" });
console.log(eur.format(1234.5)); // e.g. "1.234,50 €"
const yen = new Intl.NumberFormat("en-US", { style: "currency", currency: "JPY" });
console.log(yen.format(1234.5)); // "¥1,235" (yen has no minor unit)
const pct = new Intl.NumberFormat("en-US", { style: "percent" });
console.log(pct.format(0.256)); // "26%"
const hours = new Intl.NumberFormat("en", { style: "unit", unit: "hour", unitDisplay: "long" });
console.log(hours.format(3)); // "3 hours"
const speed = new Intl.NumberFormat("en", { style: "unit", unit: "kilometer-per-hour" });
console.log(speed.format(50)); // "50 km/h"
const compact = new Intl.NumberFormat("en-US", { notation: "compact" });
console.log(compact.format(1234567)); // "1.2M"The currency's own rules set the default number of fraction digits, which is why the euro shows two and the yen shows none. You can override them with minimumFractionDigits and maximumFractionDigits, or with maximumSignificantDigits.
Common errors
- Expecting conversion.
currency: "EUR"only changes the symbol and the rounding. It never converts the amount from dollars to euros. - Omitting the required option.
{ style: "currency" }withoutcurrencythrows aTypeError. The same goes forstyle: "unit"withoutunit. - Passing a percentage instead of a fraction.
style: "percent"multiplies by 100, so passing26prints2,600%. - Using an unsupported unit. Only a fixed list of identifiers is allowed, such as
"hour","kilobyte", and"celsius", plus compoundX-per-Yforms built from them. An unknown name like"hours"throws aRangeError. - Formatting before rounding money. Floating-point sums like
0.1 + 0.2format fine as$0.30, but storing that rounded display text and adding it up later gives drift. Keep money as integer minor units (cents) and divide only at display time.
Like DateTimeFormat, NumberFormat has formatToParts(). It returns typed pieces such as integer, group, decimal, fraction, and currency, so you can style the symbol separately without parsing a string.
Exercise
A duration of 90 minutes should display as "1.5 hours" in English and use the German decimal comma in "de-DE". Write a function formatHours(minutes, locale) using Intl.NumberFormat that shows at most one fractional digit.
Show answer
function formatHours(minutes, locale) { return new Intl.NumberFormat(locale, { style: "unit", unit: "hour", unitDisplay: "long", maximumFractionDigits: 1 }).format(minutes / 60); } Calling formatHours(90, "en") returns "1.5 hours", and formatHours(90, "de-DE") returns "1,5 Stunden". In real code, cache one formatter per locale instead of building a new one on every call.
Exercise
Why does new Intl.NumberFormat("en-US", { style: "currency", currency: "USD" }).format(19.999) print "$20.00", and how would you make it print three decimals?
Show answer
USD defaults to two fraction digits, so the value is rounded to 20.00. Pass minimumFractionDigits: 3 and maximumFractionDigits: 3 to print "$19.999".