Sub topic 8
Formatting numbers, currencies, dates and lists
This chapter assumes you can write React function components and hooks, have react-i18next set up as in the earlier chapters, and know what a BCP 47 language tag such as de-DE is.
Why not format by hand
Writing '$' + price.toFixed(2) bakes in one region's conventions. Germans write 1.234,50 €, with the symbol last, a dot for thousands and a comma for decimals. Dates and lists vary as much. The browser's built-in Intl objects know these rules for every supported locale, so you pass them a locale and a value and they return the right string.
| API | Formats | Typical option |
|---|---|---|
| `Intl.NumberFormat` | numbers, currencies, percentages | `style: 'currency'` |
| `Intl.DateTimeFormat` | dates and times | `dateStyle: 'long'` |
| `Intl.ListFormat` | lists of strings | `type: 'conjunction'` |
| `Intl.PluralRules` | plural category of a number | `select(n)` |
The basic calls
Each formatter takes a locale tag, then an options object.
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 KirschenNote that the currency is a separate choice from the locale. The locale decides how the amount looks. The currency option decides which money it is. A German user can still see prices in US dollars, written with German separators.
Using the active locale in React
Hard-coding 'de-DE' defeats the purpose. Read the active language from i18next and rebuild the formatter when it changes.
import { useMemo } from 'react';
import { useTranslation } from 'react-i18next';
export function Price({ amount, currency }: { amount: number; currency: string }) {
const { i18n } = useTranslation();
const locale = i18n.resolvedLanguage ?? 'en';
const formatter = useMemo(
() => new Intl.NumberFormat(locale, { style: 'currency', currency }),
[locale, currency],
);
return <span>{formatter.format(amount)}</span>;
}useTranslation re-renders the component when the language changes, so the memo key locale is enough to refresh the output. Memoising matters because constructing an Intl formatter is slower than calling format. Create one per locale and options pair, not one per render.
Common errors
- Formatting before translating. Pass the raw number or
Datetoformat. Do not convert it to a string first, or the formatter has nothing to work with. - Ignoring time zones.
new Date(2026, 11, 2)uses the local time zone. For a timestamp from a server, passtimeZonein the options so every user sees the same day, or deliberately use theirs. - Using the wrong currency code. The code must be an ISO 4217 value such as
EUR. An invalid code throws aRangeError. - Splicing formatted values into sentences. Pass the formatted string as a variable to
t, as int('total', { amount: formatted }), so translators can move it within the sentence. - Assuming identical output everywhere. Exact strings can differ slightly between browsers and Node versions. Do not compare them in tests without pinning the environment.
Exercise
A component shows new Intl.NumberFormat('en-US', { style: 'currency', currency: 'EUR' }).format(5) to every user. A French user sees "€5.00" instead of the French style. What is wrong, and how do you fix it?
Show answer
The locale is hard-coded to en-US, so the number follows US conventions even though the currency is euros. Take the locale from i18n.resolvedLanguage (for example fr), and keep currency: 'EUR'. The output then follows French conventions: "5,00 €".