The Intl.NumberFormat API provides locale-aware number formatting for currencies, percentages, and more. Here's how to use it.
Basic Usage#
Currency Formatting#
Percentage Formatting#
Unit Formatting#
Decimal Options#
Notation Options#
Sign Display#
Format to Parts#
Range Formatting#
Rounding Options#
Practical Examples#
Caching Formatters#
Best Practices#
Locale Handling:
✓ Use user's locale when possible
✓ Provide locale fallbacks
✓ Test with multiple locales
✓ Consider right-to-left locales
Currency:
✓ Use ISO 4217 currency codes
✓ Match locale to currency region
✓ Consider currencyDisplay option
✓ Handle accounting format
Performance:
✓ Cache formatter instances
✓ Reuse formatters
✓ Avoid creating in loops
✓ Use formatToParts for custom display
Avoid:
✗ Manual number formatting
✗ String concatenation for currency
✗ Ignoring locale differences
✗ Creating formatters repeatedly
Conclusion#
Intl.NumberFormat provides comprehensive locale-aware number formatting for currencies, percentages, units, and general numbers. Use it instead of manual string manipulation to ensure correct formatting across different locales. Cache formatter instances for performance and use formatToParts when you need fine-grained control over the output display.