Chapter 16·Strings, Names, and Input·~7 min·
Parsing Localized Numbers and Digits
ECMA-402 formats numbers for display but provides no built-in parser for localized user input. Input handlers must extract group and decimal separators from formatToParts() and normalize non-Latin digit scripts before numeric conversion.
Overview
ECMAScript provides `Intl.NumberFormat` to format localized numbers, but includes no built-in inverse API to parse localized user input. Passing localized strings such as `1.234,56` or Arabic-Indic digits to `Number()` or `parseFloat()` returns `NaN` or silently truncates the value.
Core rule
Derive locale decimal and grouping separators from formatToParts, fold non-ASCII digit blocks to ASCII before parsing, and exchange data in locale-neutral wire formats.
Specification note: parseFloat("1.234,56") stops at the comma and returns 1.234, silently corrupting a German price input by a factor of a thousand.
Failure mode
When a German user types 1.234,56 into an amount field, parseFloat("1.234,56") stops at the comma and returns 1.234 without raising an error. When an Egyptian user types the same amount in Arabic-Indic digits (١٬٢٣٤٫٥٦), both Number() and parseFloat() return NaN. Unlike a display formatting defect, an input parsing defect writes corrupted numbers directly into persistent storage.
Users in many countries type numbers with commas as decimal separators (1,50) and periods as thousand separators. Naive parsers silently miscalculate amounts.
Business stakes & user risk
A German customer typing '1.500' meaning 1,500 parsed with parseFloat('1.500') results in 1.5, charging them 1,000 times too little.
Rule of thumb
Never use parseFloat() directly on user input. Extract locale separators via Intl.NumberFormat.formatToParts() before parsing.
The Novice Trap vs. Production Standard
Contrast the intuitive assumption with the architecture required for global releases.
“JavaScript parseFloat() or Number() correctly parses numbers typed by users in any country.”
Naive approach: Standard parseFloat
const quantity = parseFloat(userInput); // User types "2,5"Defect:Returns 2 instead of 2.5, silently discarding the fractional value without any error.
parseFloat() expects US English formatting (periods for decimals). It silently stops parsing when it encounters a comma, turning '12,50' into 12.
Production standard: Locale-aware decimal normalization
const clean = userInput.replace(/\s/g, "").replace(",", "."); const amount = parseFloat(clean);Benefit:Normalizes commas, spaces, and non-breaking spaces according to locale conventions before calculating financial totals.
Cross-Functional Review Questions
Questions for product managers, designers, engineers, and testers to ask during sprint reviews.
“Are monetary and numerical input fields parsed using locale-aware separator detection?”
“Does form validation accept both comma and period decimals for international payment amounts?”
“Are financial payment and cart forms tested with German ('1.500,50') and French ('1 500,50') numbers?”
“Does the numerical input keyboard trigger appropriate regional keyboards on mobile devices?”
Mechanism
Asymmetry of formatting and parsing in ECMA-402
ECMA-402 (Intl) provides formatting APIs (format, formatToParts, formatRange) but intentionally omits a localized parsing API. Meanwhile, JavaScript's built-in numeric parsers (Number() and parseFloat()) accept only the ECMAScript numeric literal grammar: ASCII digits (0-9), a period (.) as the decimal radix, and no grouping separators. Passing a German formatted string '1.234,56' to Number() returns NaN. Passing '1.234,56' to parseFloat() parses the prefix '1.234', stops at the comma, and silently returns 1.234, corrupting the value by a factor of 1,000.
To parse localized numeric input deterministically, inspect the output of new Intl.NumberFormat(locale).formatToParts(1234567.89) at runtime. Extracting the group and decimal token values from formatToParts yields the exact separators used by the active CLDR version (including U+202F narrow no-break space in French or ' in Swiss German) so the parser can strip grouping characters and normalize the decimal separator to . before calling Number().
const parts = new Intl.NumberFormat("de-DE").formatToParts(1234567.89);
// [{type:"integer",value:"1"},{type:"group",value:"."}, …]
const group = parts.find(p => p.type === "group")?.value; // "."
const decimal = parts.find(p => p.type === "decimal")?.value; // ","Interactive demo
Guided exercises
- 1Leave "Paste as a user from" on "de-DE · Germany" and read the results table. `Number('1.234,56')` returns `NaN`, whereas `parseFloat('1.234,56')` silently truncates at the comma and returns `1.234`.
- 2Switch to "fr-FR · France" and check the Group separator card under "A parser that asks the locale first". French uses `U+202F` (Narrow No-Break Space) as its grouping separator. Reading the separator from `formatToParts` handles both `U+202F` and regular spaces.
- 3Switch to "ar-EG · Egypt". Built-in JavaScript parsers reject Arabic-Indic digits (`U+0660–U+0669`). Folding each code point by subtracting `0x0660` and normalizing the Arabic decimal separator (`٫`, `U+066B`) recovers `1234.56`.
- 4Switch to "ja-JP · full-width digits". `parseFloat` returns `NaN` on full-width digits (`U+FF10–U+FF19`) produced by a Japanese IME, whereas subtracting `0xFF10` folds them to ASCII digits.
- 5Back on "de-DE · Germany", clear "Amount, as typed" and type 1,2,3. Press "Refill the example" when done. Replacing both commas with decimal points produces `'1.2.3'`, which `Number()` rejects as `NaN` instead of returning a partial prefix.
- 6Scroll to "The same string, three dates" and compare the "Reads 01/02/03 as" column. `01/02/03` resolves to January 2, 2003 in `en-US`, February 1, 2003 in `de-DE`, and February 3, 2001 in `ja-JP` with no syntax error, illustrating why APIs must exchange ISO 8601 strings.
What the standard parsers do with it
Picking a market fills the field with 1234.56 (123456.78 for India) in that market's format.
Dot groups thousands and comma marks the decimal, the reverse of en-US.
| Parser | Result | What happened |
|---|---|---|
| Number(input) | NaN | Accepts the whole string or nothing. Only U+002E is a decimal point. Grouping characters are not in the grammar. |
| parseFloat(input) | 1.234 ← kept only the leading digits | Reads up to the first character it cannot use and drops the rest, with no error. |
| <input type="number"> | — | What this browser keeps when code assigns the string to a number input. Engines differ. |
A parser that asks the locale first
Intl has no parser, but formatToParts() on a known number labels the locale's separators.
Deriving separators…
The same string, three dates
Every field of 01/02/03 is a valid day, month, and year, so every reading below produces a date and none raises an error. Field orders come from formatToParts() on 2003-02-01.
01/02/03
| Locale | Field order | Formats 2003-02-01 as | Reads 01/02/03 as |
|---|---|---|---|
| Deriving field orders… | |||
A date picker or three labeled fields removes the ambiguity at input. Between systems, ISO 8601 (2003-02-01) reads the same in every locale.
Implementation rules
- Extract locale separators dynamically with
formatToParts(). CallIntl.NumberFormat(locale).formatToParts(12345.6)to discover the active group and decimal separators. Strip the locale group separator and map the locale decimal separator to.before callingNumber(). - Normalize non-ASCII decimal digits to ASCII
0–9. Convert Arabic-Indic (U+0660–U+0669), Extended Arabic-Indic (U+06F0–U+06F9), and full-width (U+FF10–U+FF19) digits by subtracting each block's starting code point. - Keep wire payloads locale-independent. Transmit plain IEEE 754 JSON numbers and ISO 8601 date strings across APIs. Parse localized strings at the input boundary and format them at the display boundary.
- Avoid parsing free-text numeric dates. An input such as
01/02/03is ambiguous acrossen-US,en-GB, andja-JP. Use structured date pickers or separate labeled year, month, and day fields. - Use
inputmode="numeric"on text inputs for identifiers. Use<input type="text" inputmode="numeric">for postal codes, credit cards, and phone numbers so the browser does not strip leading zeros or apply floating-point rounding.
Operational context
Roles
Workflow stages
- ·Implementing price, quantity, and date input controls
- ·Validating forms in locales with comma decimals or non-Latin digits
- ·Reviewing API payload schemas
Use the tool: CLDR explorer · the formats users type →
Production incident
In February 2020, the UK Government Digital Service (GOV.UK Design System) deprecated <input type="number"> in favor of <input type="text" inputmode="numeric">. User testing showed browsers silently stripping non-ASCII characters, rounding 16-digit account numbers, and announcing numeric inputs as unlabeled spin buttons to screen readers.