← Back to JavaScript, page 4

javascript-0075 · Medium mid

How do you format numbers, dates, plurals, and lists for different locales with the Intl API?

The Intl object gives built-in, locale-aware formatting: NumberFormat for numbers and currencies, DateTimeFormat for dates with an explicit time zone, PluralRules for singular and plural categories, RelativeTimeFormat for phrases like "2 days ago", and ListFormat for lists. These replace hand-written string code and hard-coded separators, which break for other locales.

Full answer

People write numbers, dates, and lists differently around the world. One thousand two hundred and thirty-four and a half is 1,234.5 in the US, 1.234,5 in Germany, and 1,234.5 in India with grouping of 12,34,567 for larger values. Building these strings by hand is error-prone, so JavaScript has Intl.

Intl.NumberFormat(locale, options) formats plain numbers, currencies (style: 'currency', currency: 'INR'), percentages, units, and compact forms like 1.2K. The currency code is separate from the locale, since a German reader may view prices in Indian rupees.

Intl.DateTimeFormat formats dates. Always pass timeZone, otherwise the server or user machine zone is used. Use dateStyle and timeStyle for common patterns, or detailed options for exact parts.

Intl.PluralRules returns a category such as one, other, few, or many for a number. English has two forms, but Polish or Arabic have more, so code like count === 1 ? 'item' : 'items' is wrong in many languages. Plural categories map to translated strings.

Intl.RelativeTimeFormat produces "in 3 days" or "yesterday" (with numeric: 'auto'). Intl.ListFormat joins items as "A, B, and C" with the right conjunction and punctuation. Intl.Collator sorts text correctly, Intl.DisplayNames gives names for languages and regions, and Intl.Segmenter splits text by grapheme or word.

Creating a formatter is more costly than using one, so create it once and reuse it in loops. Output can differ slightly between engines and versions, so tests should not compare exact strings from different environments.

javascript
const amount = 1234567.891;
console.log(new Intl.NumberFormat('en-US').format(amount));
console.log(new Intl.NumberFormat('de-DE').format(amount));
console.log(new Intl.NumberFormat('en-IN').format(amount));

const rupees = new Intl.NumberFormat('en-IN', { style: 'currency', currency: 'INR', maximumFractionDigits: 0 });
console.log(rupees.format(1499.5));
console.log(new Intl.NumberFormat('en-US', { style: 'currency', currency: 'USD' }).format(1499.5));
console.log(new Intl.NumberFormat('en', { notation: 'compact' }).format(1250000));
console.log(new Intl.NumberFormat('en', { style: 'percent' }).format(0.256));
console.log(new Intl.NumberFormat('en', { style: 'unit', unit: 'kilometer-per-hour' }).format(60));

const moment = new Date('2026-08-15T09:05:00Z');
const parts = { timeZone: 'Asia/Kolkata', dateStyle: 'long', timeStyle: 'short', hourCycle: 'h23' };
console.log(new Intl.DateTimeFormat('en-GB', parts).format(moment));
console.log(new Intl.DateTimeFormat('en-US', { timeZone: 'UTC', year: 'numeric', month: 'long', day: 'numeric' }).format(moment));
console.log(new Intl.DateTimeFormat('de-DE', { timeZone: 'UTC', weekday: 'long', month: 'long', day: 'numeric' }).format(moment));

const english = new Intl.PluralRules('en');
const polish = new Intl.PluralRules('pl');
console.log([0, 1, 2].map((n) => english.select(n)), [1, 2, 5, 22].map((n) => polish.select(n)));
const ordinals = new Intl.PluralRules('en', { type: 'ordinal' });
const suffix = { one: 'st', two: 'nd', few: 'rd', other: 'th' };
console.log([1, 2, 3, 4, 11, 21].map((n) => n + suffix[ordinals.select(n)]).join(' '));

const relative = new Intl.RelativeTimeFormat('en', { numeric: 'auto' });
console.log(relative.format(-1, 'day'), '|', relative.format(3, 'week'), '|', relative.format(0, 'day'));

const list = new Intl.ListFormat('en', { style: 'long', type: 'conjunction' });
console.log(list.format(['HTML', 'CSS', 'JavaScript']), '|', new Intl.ListFormat('en', { type: 'disjunction' }).format(['a', 'b']));
console.log(new Intl.DisplayNames('en', { type: 'region' }).of('IN'), new Intl.DisplayNames('en', { type: 'language' }).of('hi'));

Run with Node.js v24.22.1 as js-75.js; two runs gave identical output. Output: 1,234,567.891 1.234.567,891 12,34,567.891 ₹1,500 $1,499.50 1.3M 26% 60 km/h 15 August 2026 at 14:35 August 15, 2026 Samstag, 15. August [ 'other', 'one', 'other' ] [ 'one', 'few', 'many', 'few' ] 1st 2nd 3rd 4th 11th 21st yesterday | in 3 weeks | today HTML, CSS, and JavaScript | a or b India Hindi

Key points

  • Use Intl instead of hand-written separators and plural rules.
  • NumberFormat handles currency, percent, unit, and compact styles; currency is independent of locale.
  • Always set an explicit timeZone in DateTimeFormat.
  • PluralRules gives categories because many languages have more than two forms.
  • Reuse formatter objects; avoid exact-string tests across environments.

Common mistakes

  • Hard-coding thousands separators, currency symbols, or count === 1 plural logic.
  • Formatting dates without a timeZone and getting different output on each machine.
  • Creating a new formatter object for every item in a long list.
  • Comparing formatted strings in tests that run in different environments.

Follow-ups

  • How would you build a message like "3 files" or "1 file" for many languages?
  • What is the difference between a locale and a currency in NumberFormat?
  • How does Intl.Collator handle accents and numbers in sorting?

Tips

  • Show PluralRules with a language that has more than two forms; it proves the point quickly.
  • Say "explicit timeZone" every time dates come up.
  • Mention reuse of formatter objects as a performance point.