Intl.NumberFormat
Learn locale-aware number and currency formatting with Intl.NumberFormat.
Intl.NumberFormat
Intl.NumberFormat formats numbers by locale. Each locale uses its own thousands separator and decimal mark: 1,234.56 in the US; 1.234,56 in Germany. You pass a locale string to the constructor: 'tr-TR', 'en-US', 'de-DE'. The format method takes a number and returns the formatted string. The options object controls style, fraction digits and currency. When you need a new formatter, call the constructor again.
Currency formatting
To format currency, set style: 'currency' and pass the ISO 4217 code in currency: 'TRY', 'EUR', 'USD'. Choose what appears in the output with currencyDisplay: 'symbol', 'narrowSymbol', 'code', 'name'. Decimal digits are controlled by minimumFractionDigits and maximumFractionDigits. Two digits are standard for most currencies; exceptions like the Japanese yen use zero.
Exercise EUR currency formatter
Create an Intl.NumberFormat for the 'de-DE' locale. Set style to 'currency' and currency to 'EUR'. Call format with 49.99 and assign the result to the price variable.
Show example solution
1. Create with new Intl.NumberFormat('de-DE', { ... }).
2. In options set style 'currency' and currency 'EUR'.
3. Call .format(49.99) and assign to price.
Example solution:
const price = new Intl.NumberFormat('de-DE', {
style: 'currency',
currency: 'EUR'
}).format(49.99);
Test yourself
-
What does the style field in the Intl.NumberFormat options decide?
- The formatting type: decimal, currency, percent or unit
- How many digits the number has
- Which locale is used
- Whether the output is uppercase
Show answer
The formatting type: decimal, currency, percent or unit — Correct. The style field picks the formatting type; currency also requires a currency code.
-
Add the EUR code
const price = new Intl.NumberFormat('tr-TR', { style: 'currency', currency: ____ }).format(1250);Show answer
const price = new Intl.NumberFormat('tr-TR', { style: 'currency', currency: 'EUR' }).format(1250); -
Match options to their meaning
- style
- Formatting type
- currency
- ISO 4217 code
- minimumFractionDigits
- Minimum fraction digits
- currencyDisplay
- Symbol or code