Glossary / Locale-aware numbers
Locale-aware numbers
Locale-aware number formatting presents numeric values using a user’s conventions for decimal and grouping separators, digits, percent, currency, signs, and units. The same value can appear as 1,234.5, 1.234,5, or use non-Latin digits depending on locale. Store numbers as numeric values and format them for display with locale data; parse user input according to a declared locale rather than stripping punctuation heuristically.
What to know
- JavaScript
Intl.NumberFormatformats decimal, percent, currency, unit, compact, and significant/fraction-digit output. - A currency amount is the pair amount + ISO currency code, not a floating-point display string. Currency fraction digits vary; use decimal-safe arithmetic for money when required.
- Locale controls separators, grouping, symbol placement, spacing, and digit systems. Currency symbol alone can be ambiguous (
$may mean multiple currencies); use code or localized name when clarity matters. - Formatting and parsing are separate.
Intl.NumberFormatdoes not provide a general inverse parser; implement a locale-aware input strategy or use a well-reviewed library. - Avoid converting large integers or exact decimal monetary values through binary floating point if precision matters. Consider integer minor units or decimal types, with explicit rounding policy.
- Negative values, accounting formats, percent scaling, significant digits, compact notation, and rounding can change meaning. Specify options deliberately.
- Bidirectional contexts may require directional isolation for formatted numbers embedded in RTL text.
Testing and pitfalls
Test decimals, large values, zero, negative amounts, percentages, currencies with different minor units, and locales with alternate digits or separators. Validate user input without silently misreading 1,234 as 1.234 or 1234. Keep raw value and display string separate. Do not use locale-formatted strings for sorting, calculations, APIs, or database keys.
Interview questions
- Why not format with
toFixed? It lacks locale conventions and can expose binary floating-point rounding surprises. - What should represent money? A value with currency code and precision/rounding rules; use a decimal-safe representation where exact arithmetic is needed.
- How do you parse localized numeric input? Define accepted locale and grammar, normalize validated digits/separators carefully, and reject ambiguous or malformed forms rather than guessing.
- Why can
$be insufficient? Multiple currencies use the symbol; in cross-border contexts display the code or localized currency name. - What is a common percent bug?
Intlpercent formatting expects a fractional value (for example 0.15 to display 15%), so passing 15 can display 1500%.