Course contentsShow
← JavaScript
Lesson 1646 of 2,72335. Standard Library and Built-insFree lesson

Unit Formatting: Percentages and Measurements

Format percentages, kilometers, megabytes, and other units with locale-appropriate symbols.

Unit Formatting: Percentages and Measurements

You'll learn how to format numbers as percentages, distances, file sizes, and other units with the correct symbols for any locale.

What Are Units in Intl.NumberFormat?

When you display "75%" or "120 km" or "500 MB", you're combining a number with a unit. Different countries format these differently—some use spaces before units, different decimal separators, or entirely different measurement systems. Intl.NumberFormat can handle all of this automatically.

Formatting Percentages

Use style: 'percent' to format decimal numbers as percentages:

const percent = new Intl.NumberFormat('en-US', { style: 'percent' });
console.log(percent.format(0.75));  // "75%"

const germanPercent = new Intl.NumberFormat('de-DE', { style: 'percent' });
console.log(germanPercent.format(0.75));  // "75 %" (note the space)

Important: Pass 0.75 for 75%, not 75. The formatter multiplies by 100 for you.

Formatting Measurements

Use style: 'unit' with a unit option for measurements:

const km = new Intl.NumberFormat('en-US', { 
  style: 'unit', 
  unit: 'kilometer' 
});
console.log(km.format(120));  // "120 km"

const mb = new Intl.NumberFormat('en-US', { 
  style: 'unit', 
  unit: 'megabyte' 
});
console.log(mb.format(500));  // "500 MB"

Common units include kilometer, meter, mile, celsius, fahrenheit, megabyte, gigabyte, liter, and many more.

Why This Matters

Instead of manually concatenating strings like value + '%' or size + ' MB', you get locale-aware formatting that respects spacing, symbol placement, and even plural forms in some languages—all handled automatically.

Key Takeaway: Use style: 'percent' for percentages (passing decimal values) and style: 'unit' with a unit option for measurements, letting Intl format them correctly for any locale.