Course contentsShow
← Web Development
Lesson 2773 of 2,98249. Dates, Times, and Numbers on the WebFree lesson

Unit and Percent Formatting

Formatting units like speed, length, and percentages with locale-appropriate notation.

Unit and Percent Formatting

You'll learn how to format measurements, quantities, and percentages with proper symbols and locale-specific conventions using the Intl API.

Why Unit Formatting Matters

Different regions display measurements differently. Americans write "55 mph" while Europeans prefer "90 km/h". Percentages vary too: "50%" in English becomes "50 %" (with a space) in French. Manually hardcoding these differences creates maintenance nightmares and poor user experiences.

The Intl.NumberFormat API handles this automatically when you specify a style and unit option.

Formatting Units

To format measurements like speed, length, or temperature, use the "unit" style:

// Speed formatting
const speedFormatter = new Intl.NumberFormat('en-US', {
  style: 'unit',
  unit: 'mile-per-hour'
});

console.log(speedFormatter.format(65)); // "65 mph"

// Same data, different locale
const speedFormatterDE = new Intl.NumberFormat('de-DE', {
  style: 'unit',
  unit: 'kilometer-per-hour'
});

console.log(speedFormatterDE.format(100)); // "100 km/h"

Common units include 'meter', 'kilogram', 'celsius', 'liter', and many more.

Formatting Percentages

For percentages, use style: 'percent'. The formatter automatically divides your input by 100:

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

console.log(percentFormatter.format(0.58)); // "58%"

// French adds a space before the percent sign
const percentFormatterFR = new Intl.NumberFormat('fr-FR', {
  style: 'percent',
  minimumFractionDigits: 1
});

console.log(percentFormatterFR.format(0.582)); // "58,2 %"

Key Takeaway: Use Intl.NumberFormat with style: 'unit' for measurements and style: 'percent' for percentages to automatically apply locale-appropriate symbols, spacing, and formatting conventions—making your app globally accessible without manual string concatenation.