Reading history

JavaScript Intl: Format Numbers, Currency and Dates

5 min read 1 views

Format numbers, BDT and USD currency, percentages, dates, time zones, relative times, lists and plurals with the JavaScript Intl API, including Bangla.

The number 1234567.89 is written “1,234,567.89” in the United States, “12,34,567.89” in India and Bangladesh, and “1.234.567,89” in Germany. Dates, currencies and plurals vary just as much. Instead of writing formatting code by hand, JavaScript provides the Intl object, which knows the rules of hundreds of languages and regions. This lesson shows how to format numbers, money, percentages, dates, relative times and lists correctly for any audience.

In this lesson you will learn

  • Formatting numbers for different locales, including Bangla digits
  • Currency, percent, compact and unit formats
  • Dates and times in any language and time zone
  • Relative times such as “yesterday” and “in 3 days”
  • Lists, plurals and natural sorting with Intl

You should know numbers and dates.

Numbers and Currency

"use strict";
// Numbers: grouping, decimals and percentages depend on the locale
const amount = 1234567.891;
console.log(new Intl.NumberFormat("en-US").format(amount));
console.log(new Intl.NumberFormat("en-IN").format(amount));      // lakh and crore grouping
console.log(new Intl.NumberFormat("de-DE").format(amount));
console.log(new Intl.NumberFormat("bn-BD").format(amount));      // Bangla digits

// Currency
const bdt = new Intl.NumberFormat("en-BD", { style: "currency", currency: "BDT" });
const usd = new Intl.NumberFormat("en-US", { style: "currency", currency: "USD" });
console.log(bdt.format(1500), usd.format(1500), usd.format(-12.5));

// Percent, compact and unit styles
console.log(new Intl.NumberFormat("en", { style: "percent", maximumFractionDigits: 1 }).format(0.8765));
console.log(new Intl.NumberFormat("en", { notation: "compact" }).format(2_500_000));
console.log(new Intl.NumberFormat("en", { style: "unit", unit: "kilometer-per-hour" }).format(80));
console.log(new Intl.NumberFormat("en", { minimumFractionDigits: 2, maximumFractionDigits: 2 }).format(3.5));

Output

1,234,567.891
12,34,567.891
1.234.567,891
১২,৩৪,৫৬৭.৮৯১
BDT 1,500.00 $1,500.00 -$12.50
87.7%
2.5M
80 km/h
3.50
  • new Intl.NumberFormat(locale, options) creates a formatter; its format() method returns a string. The locale is a language tag such as "en-US", "en-IN" or "bn-BD".
  • The same number gets different separators and grouping: en-IN groups in lakhs and crores, and bn-BD also switches to Bangla digits.
  • style: "currency" needs a three-letter currency code such as "BDT" or "USD", and adds the right symbol and two decimals.
  • style: "percent" multiplies by 100, notation: "compact" gives short forms like “2.5M”, and style: "unit" adds units such as km/h.
  • minimumFractionDigits and maximumFractionDigits control decimals, so 3.5 shows as “3.50” for prices.
  • Create a formatter once and reuse it: building one is slower than calling format().

Dates, Relative Times, Lists and Plurals

"use strict";
const exam = new Date(Date.UTC(2026, 2, 26, 4, 30));   // 26 March 2026, 04:30 UTC

// Dates in different styles, languages and time zones
const opts = { dateStyle: "full", timeStyle: "short", timeZone: "Asia/Dhaka" };
console.log(new Intl.DateTimeFormat("en-GB", opts).format(exam));
console.log(new Intl.DateTimeFormat("bn-BD", opts).format(exam));
console.log(new Intl.DateTimeFormat("en-US", { dateStyle: "medium", timeZone: "America/Los_Angeles" }).format(exam));
console.log(new Intl.DateTimeFormat("en", { weekday: "short", day: "numeric", month: "short", timeZone: "UTC" }).format(exam));

// Relative times: "in 3 days", "2 hours ago"
const rtf = new Intl.RelativeTimeFormat("en", { numeric: "auto" });
console.log(rtf.format(3, "day"), "|", rtf.format(-1, "day"), "|", rtf.format(-2, "hour"));

// Lists joined the way each language does it
console.log(new Intl.ListFormat("en", { type: "conjunction" }).format(["HTML", "CSS", "JavaScript"]));

// Plural rules pick the right word form
const pr = new Intl.PluralRules("en");
for (const n of [0, 1, 2]) {
  console.log(`${n} ${pr.select(n) === "one" ? "lesson" : "lessons"}`);
}

// Sorting with a Collator: numbers inside strings sort naturally
const files = ["lesson10", "lesson2", "Lesson1"];
console.log(files.toSorted(new Intl.Collator("en", { numeric: true, sensitivity: "base" }).compare));

Output

Thursday, 26 March 2026 at 10:30
বৃহস্পতিবার, ২৬ মার্চ, ২০২৬ এ ১০:৩০ AM
Mar 25, 2026
Thu, Mar 26
in 3 days | yesterday | 2 hours ago
HTML, CSS, and JavaScript
0 lessons
1 lesson
2 lessons
[ 'Lesson1', 'lesson2', 'lesson10' ]
  • Intl.DateTimeFormat formats a Date. dateStyle and timeStyle ("full", "long", "medium", "short") give ready-made styles, or you can pick parts like weekday and month.
  • timeZone matters: the exam at 04:30 UTC is 10:30 in Dhaka and the evening before in Los Angeles, so even the date changes.
  • Intl.RelativeTimeFormat writes “in 3 days” or “2 hours ago”. With numeric: "auto", -1 day becomes “yesterday”.
  • Intl.ListFormat joins items with commas and “and”, following each language’s rules.
  • Intl.PluralRules tells you which word form to use. English only has “one” and “other”, but some languages have several plural forms, which is why hand-written n === 1 checks do not translate.
  • Intl.Collator with numeric: true sorts “lesson2” before “lesson10”, and sensitivity: "base" ignores case.

Exact output can differ slightly between browsers and versions, because each uses its own copy of the locale data. Never parse formatted strings back into numbers; keep the original values for calculations.

Intl Tools at a Glance

ConstructorFormatsExample result
Intl.NumberFormatNumbers, currency, percent, units12,34,567
Intl.DateTimeFormatDates and timesThursday, 26 March 2026
Intl.RelativeTimeFormatRelative timesin 3 days
Intl.ListFormatListsHTML, CSS, and JavaScript
Intl.PluralRulesPlural category“one” or “other”
Intl.CollatorLanguage-aware sortinglesson2 before lesson10

Common Mistakes

  1. Building currency strings by hand, such as "৳" + amount.toFixed(2), which ignores grouping and locale rules.
  2. Forgetting timeZone, so users in other countries see a different date than intended.
  3. Creating a new formatter inside a loop over thousands of rows.
  4. Doing money maths with formatted strings. Format only when displaying.

Practice Exercises

Exercise 1. Show 2500000 views as “2.5M views” and 0.054 as “5.4%”.

Show answer
const compact = new Intl.NumberFormat("en", { notation: "compact" });
const pct = new Intl.NumberFormat("en", { style: "percent", maximumFractionDigits: 1 });
console.log(compact.format(2500000) + " views", pct.format(0.054));

Exercise 2. Show today’s date in Bangla with the full weekday and month name.

Show answer
console.log(new Intl.DateTimeFormat("bn-BD", { dateStyle: "full" }).format(new Date()));

Frequently Asked Questions

What is the difference between toLocaleString and Intl.NumberFormat?

(1234.5).toLocaleString("en-IN") uses the same rules as Intl.NumberFormat and accepts the same options. It is handy for one-off values; a reusable Intl formatter is better when formatting many values.

How do I use the visitor’s own locale?

Pass undefined as the locale, as in new Intl.NumberFormat(undefined), and the browser uses the user’s language settings. navigator.language also tells you the preferred language.

Summary

  • Intl.NumberFormat handles grouping, digits, currency, percent and units.
  • Intl.DateTimeFormat formats dates by language and time zone.
  • Relative times, lists, plurals and sorting all have Intl helpers.
  • Format only for display, and reuse formatters.

Next: IntersectionObserver: Lazy Loading and Infinite Scroll.

Lessons are also marked complete when you click Next.

Leave a Reply

Your email address will not be published. Required fields are marked *