Currency Formatting
What you'll learn: How to use Intl.NumberFormat to display currency values with the correct symbols, decimal places, and formatting conventions for any locale.
The Challenge of Currency Display
Displaying money on the web isn't as simple as slapping a dollar sign in front of a number. Different countries use different symbols, place them in different positions, use different decimal separators, and group digits differently. Japan uses ¥1,000 with no decimals, while the US uses $1,000.00 with two. Germany writes €1.000,00 while the UK writes £1,000.00.
Currency Formatting with Intl.NumberFormat
The Intl.NumberFormat API handles all these variations automatically when you specify style: 'currency' and provide a three-letter ISO currency code:
// US Dollars
const usdFormatter = new Intl.NumberFormat('en-US', {
style: 'currency',
currency: 'USD'
});
console.log(usdFormatter.format(1234.56)); // "$1,234.56"
// Euros in Germany (note comma as decimal separator)
const eurFormatter = new Intl.NumberFormat('de-DE', {
style: 'currency',
currency: 'EUR'
});
console.log(eurFormatter.format(1234.56)); // "1.234,56 €"
// Japanese Yen (no decimal places)
const jpyFormatter = new Intl.NumberFormat('ja-JP', {
style: 'currency',
currency: 'JPY'
});
console.log(jpyFormatter.format(1234.56)); // "¥1,235"
Notice how the API automatically:
- Picks the correct symbol
- Places it before or after the number based on locale
- Uses the appropriate decimal places for that currency
- Applies locale-specific grouping and decimal separators
Key Takeaway: Use Intl.NumberFormat with style: 'currency' and a currency code to automatically format monetary values correctly for any locale, ensuring proper symbols, decimal places, and regional conventions without manual string manipulation.