Formatting Numbers with format()
You'll learn how to transform raw numbers into properly localized strings using the format() method of Intl.NumberFormat.
The Core Idea
Once you've created an Intl.NumberFormat instance (as you learned in the previous lesson), the format() method is what actually converts your numeric values into human-readable strings. Think of the NumberFormat instance as a configured template, and format() as the stamp that applies that template to your data.
Using format()
Call format() on your NumberFormat instance and pass in the number you want to display:
const formatter = new Intl.NumberFormat('en-US');
const result = formatter.format(1234567.89);
console.log(result); // "1,234,567.89"
The method returns a string—not a number—formatted according to the locale and options you specified when creating the instance.
Multiple Values, One Formatter
You can reuse the same formatter for many numbers:
const usdFormatter = new Intl.NumberFormat('en-US', {
style: 'currency',
currency: 'USD'
});
console.log(usdFormatter.format(19.99)); // "$19.99"
console.log(usdFormatter.format(1500)); // "$1,500.00"
console.log(usdFormatter.format(0.5)); // "$0.50"
This is efficient—create the formatter once, use it repeatedly.
Why This Matters
The format() method handles all the complexity of grouping separators, decimal symbols, currency signs, and digit placement according to locale rules. You don't need to write conditional logic for different regions; format() does it automatically based on your NumberFormat configuration.
Key Takeaway: The format() method transforms numbers into locale-aware strings using the rules defined in your Intl.NumberFormat instance—create the formatter once, then call format() as many times as needed.