Sub topic 8
Formatting numbers, currencies, dates and lists
This chapter assumes you have react-i18next set up with a language switcher, as in the earlier chapters. It also assumes you are comfortable with React hooks and useMemo. Here you will replace hand-built number, date and list strings with the browser's Intl APIs, so the output follows the active locale.
Why not put formats in the catalog?
You could write "price": "{{amount}} €" in every catalog. That fails quickly, because formats differ by more than the symbol:
- German writes
1.234,50 €, with a period for thousands, a comma for decimals and the symbol after the number. - US English writes
€1,234.50. - Swiss German uses an apostrophe as the thousands separator.
- Some locales use different digits or a different calendar.
The browser already ships this data through the Intl namespace (ECMAScript Internationalization API). You give it a locale tag and options, and it applies the right rules. Translators then handle words, and Intl handles formats.
flowchart TDThe block above is not a diagram. The real diagram follows.
Numbers and currencies
Intl.NumberFormat takes a locale and an options object, then returns a formatter with a format method.
const locale = 'de-DE';
const price = new Intl.NumberFormat(locale, { style: 'currency', currency: 'EUR' }).format(1234.5);
const date = new Intl.DateTimeFormat(locale, { dateStyle: 'long' }).format(new Date(2026, 11, 2));
const list = new Intl.ListFormat(locale, { style: 'long', type: 'conjunction' }).format(['Äpfel', 'Birnen', 'Kirschen']);
console.log(price); // 1.234,50 €
console.log(date); // 2. Dezember 2026
console.log(list); // Äpfel, Birnen und Kirschen
const rules = new Intl.PluralRules('en');
console.log(rules.select(1)); // 'one'
console.log(rules.select(2)); // 'other'The space before € is a non-breaking space (U+00A0), so the number and symbol never wrap onto separate lines. Do not compare formatted output with a plain space in tests.
Common NumberFormat options:
| Option | Example | Effect |
|---|---|---|
| `style: 'percent'` | `0.256` | `26%` in `en`, `26 %` in `de` |
| `style: 'unit'`, `unit: 'kilometer-per-hour'` | `90` | `90 km/h` |
| `notation: 'compact'` | `1250000` | `1.3M` in `en`, `1,3 Mio.` in `de` |
| `minimumFractionDigits` / `maximumFractionDigits` | `3.14159` | Fixes the number of decimals |
| `signDisplay: 'always'` | `5` | `+5` |
Currency is not locale
The locale decides how a number looks. The currency code decides which money it is. A German user can view prices in US dollars, and you should then pass locale = 'de-DE' and currency = 'USD'. Never infer the currency from the language. Take it from the product, order or account data.
Each currency also has its own number of decimals. JPY has none, so 1234.5 formats as ¥1,235 in en-US. Let Intl choose the digits. Set minimumFractionDigits only when the design needs it.
Floating-point money
Intl formats whatever number you give it. It does not fix rounding errors. Keep money as integer minor units (cents) in your data and convert at the edge. Be careful, because the divisor depends on the currency (100 for EUR, 1 for JPY). The conversion is safest on the server.
Dates and times
Intl.DateTimeFormat has two ways to describe the output. Use dateStyle and timeStyle (full, long, medium, short) for a locale-appropriate result. Use individual fields such as month, weekday and hour when you need specific parts.
const when = new Date(Date.UTC(2026, 11, 2, 15, 30));
const opts = { dateStyle: 'medium', timeStyle: 'short', timeZone: 'UTC' } as const;
console.log(new Intl.DateTimeFormat('en-US', opts).format(when)); // Dec 2, 2026, 3:30 PM
console.log(new Intl.DateTimeFormat('de-DE', opts).format(when)); // 02.12.2026, 15:30
console.log(new Intl.DateTimeFormat('ja-JP', opts).format(when)); // 2026/12/02 15:30Three details cause most date bugs:
- Months are zero-based in the
Dateconstructor.new Date(2026, 11, 2)is 2 December, not 2 November. - Time zones. Without
timeZone, the formatter uses the viewer's zone. That is right for "when does my meeting start", but wrong for "the sale ends on 2 December" if the server stores a UTC date. SettimeZoneexplicitly when the date must not shift. - Date-only strings are parsed as UTC.
new Date('2026-12-02')is midnight UTC, which displays as 1 December in zones west of UTC. Format it withtimeZone: 'UTC'.
An invalid Date makes format throw a RangeError. Validate input with Number.isNaN(date.getTime()) before formatting data from an API.
Relative time
Intl.RelativeTimeFormat produces phrases such as "in 3 days". With numeric: 'auto' it uses words such as "tomorrow" where the language has them.
const rtf = new Intl.RelativeTimeFormat('de', { numeric: 'auto' });
console.log(rtf.format(-1, 'day')); // gestern
console.log(rtf.format(3, 'day')); // in 3 TagenYou choose the unit and the number. Intl does not compute the difference between two dates.
Lists
Joining with ', ' and ' and ' is another form of string concatenation. Languages differ on the final conjunction, on the serial comma and on punctuation. Intl.ListFormat handles this, with type set to conjunction ("and"), disjunction ("or") or unit (no word between items), and style set to long, short or narrow.
const items = ['red', 'green', 'blue'];
console.log(new Intl.ListFormat('en-US', { type: 'conjunction' }).format(items)); // red, green, and blue
console.log(new Intl.ListFormat('en-GB', { type: 'conjunction' }).format(items)); // red, green and blue
console.log(new Intl.ListFormat('en', { type: 'disjunction' }).format(items)); // red, green, or blueUsing the active locale in React
Pass the active language to every formatter. Reading it from i18next means formatted values change when the user switches language. Formatter construction is relatively expensive, so build the formatters once per locale with useMemo.
import { useMemo } from 'react';
import { useTranslation } from 'react-i18next';
export function useFormatters() {
const { i18n } = useTranslation();
const locale = i18n.resolvedLanguage ?? 'en';
return useMemo(() => {
const date = new Intl.DateTimeFormat(locale, { dateStyle: 'long' });
const list = new Intl.ListFormat(locale, { style: 'long', type: 'conjunction' });
return {
formatDate: (value: Date) => date.format(value),
formatList: (values: string[]) => list.format(values),
formatCurrency: (value: number, currency: string) =>
new Intl.NumberFormat(locale, { style: 'currency', currency }).format(value),
};
}, [locale]);
}
export function OrderSummary(props: {
total: number;
currency: string;
shippedOn: Date;
products: string[];
}) {
const { formatCurrency, formatDate, formatList } = useFormatters();
return (
<ul>
<li>{formatCurrency(props.total, props.currency)}</li>
<li>{formatDate(props.shippedOn)}</li>
<li>{formatList(props.products)}</li>
</ul>
);
}resolvedLanguage is the language i18next actually found a catalog for. i18n.language can hold a value such as de-CH that has no catalog. For formatting, either works, since Intl accepts de-CH directly. Pick one and use it everywhere.
i18next also has built-in formatting inside messages, such as {{price, currency(EUR)}}. It uses the same Intl classes. Calling Intl in code is easier to type-check and test, so this chapter uses that approach.
Edge cases to know
- Unsupported locale tags.
Intlfalls back to its default locale for tags it does not know. A malformed tag such asen_US(underscore) throws aRangeError. Use hyphens. - Fallback differences. Your catalog fallback (
fallbackLng: 'en') and theIntlfallback are separate. A user onfrwith no French catalog sees English text but, if you passfr, French number formats. PassresolvedLanguageif you want these to agree. - Environment differences. Output can differ slightly between browsers and Node versions as Unicode CLDR data updates. Tests that assert exact strings should pin the Node version or compare with
formatToParts. - Parsing.
Intlonly formats. There is no built-in parser for1.234,50. Keep numeric state in numbers and format them for display.
Practice
Exercise
A user whose language is de-DE opens a page that shows a product priced in US dollars, stored as 4999 cents. Write the code that displays it, and say which two values you take from where. Why is ` $${(cents / 100).toFixed(2)} ` wrong?
Show answer
Use new Intl.NumberFormat('de-DE', { style: 'currency', currency: 'USD' }).format(4999 / 100), which gives 49,99 $. The locale comes from the active language (i18n.resolvedLanguage). The currency code USD comes from the product data, not from the language. The template string is wrong because it hard-codes the symbol, its position, the decimal separator and the decimal count. German users expect a comma and a trailing symbol. It also assumes every currency has two decimals.