Sub topic 15
Numbers, Units, and Currencies with Intl.NumberFormat
Dates are seldom shown alone. A booking page shows a price next to the check-in date, a fitness app shows "3 hours" next to a timestamp, and a dashboard shows "1.2M events this week." Intl.NumberFormat handles these values with the same locale rules that Intl.DateTimeFormat uses for dates.
This chapter assumes you know the Intl basics from earlier chapters: BCP 47 locale tags, the options object, and resolvedOptions(). It also assumes you can run code in Node.js 20+ or a current browser.
The Working Model
An Intl.NumberFormat instance is built from a locale and an options object. The central option is style. It decides what kind of value you are formatting and which other options are required.
| style | Required option | Example input | en-US output |
|---|---|---|---|
| `"decimal"` (default) | none | `1234.5` | `1,234.5` |
| `"percent"` | none | `0.256` | `26%` |
| `"currency"` | `currency` | `1234.5` | `$1,234.50` |
| `"unit"` | `unit` | `3` | `3 hr` |
Once style is chosen, the remaining options control digits and rounding, grouping, notation, and how the sign and the symbol or unit appear.
Decimal Numbers and Locale Separators
Locales differ in the grouping separator, the decimal separator, and sometimes the digits themselves.
const n = 1234567.891;
for (const locale of ["en-US", "de-DE", "fr-FR", "en-IN", "ar-EG"]) {
console.log(locale, new Intl.NumberFormat(locale).format(n));
}
// en-US 1,234,567.891
// de-DE 1.234.567,891
// fr-FR 1 234 567,891 (narrow no-break space as the group separator)
// en-IN 12,34,567.891 (lakh grouping)
// ar-EG ١٬٢٣٤٬٥٦٧٫٨٩١ (Arabic-Indic digits)By default, decimal style keeps at most 3 fraction digits. You can change rounding with these options:
minimumFractionDigits/maximumFractionDigits: pad or round the decimals.minimumIntegerDigits: pad with leading zeros, which is handy for clock-like output.maximumSignificantDigits: round to a number of significant digits instead.useGrouping: set tofalseto drop group separators, for example for IDs or years.
const twoDp = new Intl.NumberFormat("en-US", {
minimumFractionDigits: 2,
maximumFractionDigits: 2,
});
console.log(twoDp.format(3)); // "3.00"
console.log(twoDp.format(2.005)); // "2.01"
const pad = new Intl.NumberFormat("en-US", { minimumIntegerDigits: 2 });
console.log(pad.format(7)); // "07"
const sig = new Intl.NumberFormat("en-US", { maximumSignificantDigits: 3 });
console.log(sig.format(123456)); // "123,000"twoDp.format(2.005) gives "2.01", but (2.005).toFixed(2) gives "2.00". The Intl formatter rounds the decimal value as written, while toFixed works on the binary approximation of 2.005, which is slightly less than 2.005. This difference matters when you display money.
Percentages
Percent style multiplies the input by 100. Pass the fraction, not the percentage.
const pct = new Intl.NumberFormat("en-US", { style: "percent" });
console.log(pct.format(0.256)); // "26%" (default: 0 fraction digits)
console.log(pct.format(25.6)); // "2,560%" (common mistake)
const pct1 = new Intl.NumberFormat("en-US", {
style: "percent",
maximumFractionDigits: 1,
});
console.log(pct1.format(0.256)); // "25.6%"Currencies
Currency style requires an ISO 4217 code such as "USD", "EUR", or "JPY". The locale controls the layout. The currency code controls the default number of decimals.
const amount = 1234.5;
console.log(new Intl.NumberFormat("en-US", { style: "currency", currency: "USD" }).format(amount));
// "$1,234.50"
console.log(new Intl.NumberFormat("de-DE", { style: "currency", currency: "EUR" }).format(amount));
// "1.234,50 €"
console.log(new Intl.NumberFormat("en-US", { style: "currency", currency: "JPY" }).format(amount));
// "¥1,235" (yen has no minor unit, so it rounds to 0 decimals)
const usd = (opts) =>
new Intl.NumberFormat("en-US", { style: "currency", currency: "USD", ...opts });
console.log(usd({ currencyDisplay: "code" }).format(10)); // "USD 10.00"
console.log(usd({ currencyDisplay: "name" }).format(10)); // "10.00 US dollars"
console.log(usd({ currencySign: "accounting" }).format(-5)); // "($5.00)"The locale and the currency are independent of each other. A German user who views a price in US dollars should get de-DE with currency: "USD", which produces "1.234,50 $". Never derive the currency from the user's locale. The currency belongs to the data, and the locale belongs to the reader.
Units
Unit style uses identifiers from a fixed, sanctioned list: "hour", "kilometer", "megabyte", "celsius", and others. You can combine two of them with -per-. The unitDisplay option can be "short" (default), "long", or "narrow".
const speed = new Intl.NumberFormat("en", { style: "unit", unit: "kilometer-per-hour" });
console.log(speed.format(50)); // "50 km/h"
const hoursLong = new Intl.NumberFormat("en", { style: "unit", unit: "hour", unitDisplay: "long" });
console.log(hoursLong.format(1)); // "1 hour"
console.log(hoursLong.format(3)); // "3 hours" (plural handled for you)
const size = new Intl.NumberFormat("en", { style: "unit", unit: "megabyte", maximumFractionDigits: 1 });
console.log(size.format(1.46)); // "1.5 MB"
console.log(Intl.supportedValuesOf("unit").includes("hour")); // trueThis connects directly to dates. A Date difference gives milliseconds, and a unit formatter turns that into readable text:
const start = new Date("2024-07-04T09:00:00Z");
const end = new Date("2024-07-04T11:45:00Z");
const hours = (end - start) / 3_600_000;
const dur = new Intl.NumberFormat("en", {
style: "unit",
unit: "hour",
unitDisplay: "long",
maximumFractionDigits: 2,
});
console.log(dur.format(hours)); // "2.75 hours"For output like "2 hours, 45 minutes," check typeof Intl.DurationFormat === "function" before using it. It is a newer API and is not available in every runtime.
Notation and Sign Display
notation: "compact"gives short forms such as "1.2M". UsecompactDisplay: "long"to get "1.2 million".notation: "scientific"and"engineering"give exponent forms.signDisplaycan be"auto","always","exceptZero","negative", or"never". It is useful for deltas.
const compact = new Intl.NumberFormat("en-US", { notation: "compact" });
console.log(compact.format(1234567)); // "1.2M"
console.log(compact.format(980)); // "980"
const compactDe = new Intl.NumberFormat("de-DE", { notation: "compact", compactDisplay: "long" });
console.log(compactDe.format(1234567)); // "1,2 Millionen"
const delta = new Intl.NumberFormat("en-US", { signDisplay: "exceptZero" });
console.log([5, 0, -3].map((v) => delta.format(v))); // ["+5", "0", "-3"]formatToParts and formatRange
Number formatters have the same two methods you saw for dates. formatToParts returns typed tokens such as currency, integer, group, decimal, and fraction, which you can style one by one. formatRange formats a span and merges shared symbols where the locale allows it.
const eur = new Intl.NumberFormat("de-DE", { style: "currency", currency: "EUR" });
console.log(eur.formatToParts(1234.5).map((p) => `${p.type}:${p.value}`));
// ["integer:1", "group:.", "integer:234", "decimal:,", "fraction:50", "literal: ", "currency:€"]
const range = new Intl.NumberFormat("en-US", {
style: "currency",
currency: "USD",
maximumFractionDigits: 0,
});
console.log(range.formatRange(3, 5)); // e.g. "$3 – $5"Common Errors
| Symptom | Cause | Fix |
|---|---|---|
| `TypeError: Currency code is required` | `style: "currency"` without `currency` | Always pass an ISO 4217 code |
| `RangeError` on construction | Unknown unit, invalid currency code, or `maximumFractionDigits` below the minimum | Check the value against `Intl.supportedValuesOf` and keep min ≤ max |
| `"2,560%"` | Passed 25.6 to percent style | Pass the fraction, 0.256 |
| Test fails although the output looks identical | Output contains U+00A0 or U+202F instead of a normal space | Normalize spaces in assertions, or compare against `formatToParts` |
| Output differs between Node and browser | The engines ship different CLDR data versions | Don't hard-code exact strings in production logic |
const out = new Intl.NumberFormat("de-DE", { style: "currency", currency: "EUR" }).format(1234.5);
console.log(out === "1.234,50 €"); // false
console.log(out.replace(/[\u00a0\u202f]/g, " ") === "1.234,50 €"); // trueExercise
A Japanese user views an invoice of 4999.99 in euros. Write the formatter call, and explain why the output has two decimals even though yen prices usually show none.
Show answer
Use new Intl.NumberFormat("ja-JP", { style: "currency", currency: "EUR" }).format(4999.99), which gives something like "€4,999.99". The currency code (EUR) sets the default fraction digits. EUR has two minor-unit digits, so two decimals appear. The ja-JP locale controls only the layout and separators.
Exercise
Write a function formatDelta(oldValue, newValue) that returns the percentage change with an explicit sign and one decimal, so that 200 to 230 gives "+15.0%" and an unchanged value gives "0.0%".
Show answer
const f = new Intl.NumberFormat("en-US", { style: "percent", minimumFractionDigits: 1, maximumFractionDigits: 1, signDisplay: "exceptZero" }); const formatDelta = (a, b) => f.format((b - a) / a); The function passes the fraction (0.15), not 15, because percent style multiplies by 100. signDisplay "exceptZero" adds "+" to positive values and leaves zero unsigned. The formatter is created once, outside the function.
Exercise
Why does new Intl.NumberFormat("en", { style: "unit", unit: "hours" }) throw, and how can your code check a unit before using it?
Show answer
Unit identifiers are singular, sanctioned names, so the correct one is "hour". The plural word comes from the locale data at format time. To check a unit, call Intl.supportedValuesOf("unit").includes(name), or wrap the construction in try/catch and handle the RangeError.