JavaScript Intl.NumberFormat: Locale-aware Numbers
Quick answer
Intl.NumberFormat formats numeric values for a locale and options such as currency, grouping and fraction digits. The result is display text, not a new numeric value.
How it works
Construct new Intl.NumberFormat(locale, options) and reuse its format function. For currency formatting provide style: "currency" and an ISO currency code. Percent formatting multiplies the displayed magnitude by 100. formatToParts exposes labeled components; resolvedOptions() reports selected settings. Text may vary with locale data, so never parse a display string as a universal machine format.
const f=new Intl.NumberFormat("en-US",{style:"percent",maximumFractionDigits:0}); console.log(f.format(0.25));Expected output in Node.js:
25%Percent style displays the value multiplied by 100.
const f=new Intl.NumberFormat("en-US",{style:"currency",currency:"USD"}); console.log(f.formatToParts(12).find(p=>p.type==="currency").value);Expected output in Node.js:
$The parts expose the currency token in the selected display style.
Common mistakes
Currency formatting does not convert currencies or apply exchange rates. Keep exact business calculations separate from display formatting.
Practice
Format a bounded number with two fraction digits and no grouping in an explicitly specified locale.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
JavaScript Intl.NumberFormat: Locale-aware Numbers — Knowledge Check
Apply the taught concept and predict the output.
What does Intl.NumberFormat.format produce?
Which option is necessary for currency style?
Why prefer formatToParts for styling a currency token?
What is the exact console output in JavaScript Intl.NumberFormat: Locale-aware Numbers example 1?
const f=new Intl.NumberFormat("en-US",{style:"percent",maximumFractionDigits:0}); console.log(f.format(0.25));What is the exact console output in JavaScript Intl.NumberFormat: Locale-aware Numbers example 2?
const f=new Intl.NumberFormat("en-US",{style:"currency",currency:"USD"}); console.log(f.formatToParts(12).find(p=>p.type==="currency").value);