Course contentsShow
← JavaScript
Lesson 2203 of 2,72346. InternationalizationFree lesson

Percent and Unit Formatting

Display percentages and physical units (e.g., kilometers, gigabytes) with proper localization.

Percent and Unit Formatting

You'll learn how to display percentages and physical units (like kilometers or gigabytes) with proper localization using Intl.NumberFormat.

Why This Matters

When building international applications, you need to format data like "85% completion" or "120 kilometers" in a way that respects each locale's conventions. Some cultures use different symbols, spacing, or even alternate measurement preferences. Instead of hardcoding these formats, JavaScript's Intl.NumberFormat handles them automatically.

Formatting Percentages

When you set style: 'percent', the formatter multiplies your value by 100 and appends the locale-appropriate percent symbol:

const percentFormatter = new Intl.NumberFormat('en-US', {
  style: 'percent'
});

console.log(percentFormatter.format(0.85)); 
// "85%"

const percentFormatterDE = new Intl.NumberFormat('de-DE', {
  style: 'percent',
  minimumFractionDigits: 1
});

console.log(percentFormatterDE.format(0.856)); 
// "85,6 %" (note the space before % in German)

Notice that 0.85 becomes "85%" — the formatter does the multiplication for you.

Formatting Units

When you set style: 'unit', you must also specify a unit option (like 'kilometer', 'gigabyte', 'celsius'):

const distanceFormatter = new Intl.NumberFormat('en-US', {
  style: 'unit',
  unit: 'kilometer'
});

console.log(distanceFormatter.format(120));
// "120 km"

const storageFormatter = new Intl.NumberFormat('fr-FR', {
  style: 'unit',
  unit: 'gigabyte',
  unitDisplay: 'long'
});

console.log(storageFormatter.format(500));
// "500 gigaoctets" (French for gigabytes)

The unitDisplay option controls whether you see abbreviations ('short'), full names ('long'), or narrow symbols ('narrow').

Key Takeaway: Use style: 'percent' for percentages (which auto-multiplies by 100) and style: 'unit' with a specific unit for measurements — both automatically adapt to your user's locale.