Guide
Why parseFloat misreads comma decimals
parseFloat does not fail on numbers written with a comma decimal separator. It does something worse: it returns a wrong number.
What parseFloat actually does#
parseFloat reads characters from the start of the string while they look like an English number, then stops and returns whatever it has. It never reports an error for the rest of the string.
parseFloat("1,5"); // 1 (stops at ",")
parseFloat("1.234,56"); // 1.234 (stops at the second ".")
parseFloat("1'234.56"); // 1 (stops at "'")
parseFloat("12,34,567.89"); // 12 (stops at ",")
parseFloat("R$ 10,00"); // NaN (starts with "R")Watch out
A wrong number is more dangerous than NaN: 1.234 passes every isNaN check and quietly ends up in your database, invoice or chart.
Number() is stricter, but not smarter#
Number("1,5"); // NaN
Number("1.234,56"); // NaN
Number("1 234"); // NaNNumber rejects the whole string, which is safer, but it still only understands one format: a dot as the decimal separator and no grouping at all.
Why replace(",", ".") is not enough#
The most common answer online is to swap the comma for a dot. It works for "1,5" and breaks for everything else:
| Input | Meaning | Number(s.replace(",", ".")) |
|---|---|---|
1,5 | 1.5 (de-DE) | 1.5 |
1.234,56 | 1234.56 (pt-BR) | NaN |
1,234 | 1234 (en-US) | 1.234 |
1 234,56 | 1234.56 (fr-FR) | NaN |
R$ 10,00 | 10 (pt-BR) | NaN |
Removing every dot first (s.replace(/\./g, "").replace(",", ".")) fixes Brazilian input and silently corrupts English input, where the dot is the decimal separator. There is no character swap that is right for every locale, because the meaning of . and , depends on who wrote the number.
The fix#
Parse with the locale the number was written in. numberparrot gets the separators from Intl.NumberFormat and validates the whole string:
import { parse } from "numberparrot";
parse("1,5", "pt-BR"); // 1.5
parse("1.234,56", "pt-BR"); // 1234.56
parse("1'234.56", "de-CH"); // 1234.56
parse("12,34,567.89", "en-IN"); // 1234567.89
// Strict mode throws with a reason instead of returning NaN
parse("1,23", "en-US", { strict: true }); // NumberParseError: INVALID_GROUPINGComparison#
| Input | Locale | parseFloat | Number | numberparrot |
|---|---|---|---|---|
1,5 | pt-BR | 1 | NaN | 1.5 |
1.234,56 | de-DE | 1.234 | NaN | 1234.56 |
1 234,56 | fr-FR | 1 | NaN | 1234.56 |
-1.234,56 | it-IT | -1.234 | NaN | -1234.56 |
When you do not know the locale#
For a pasted value, detect() works out the decimal separator from the string itself and says when it cannot be sure. For a whole CSV column, parseColumn() lets the cells that are clear decide for the ones that are not:
import { detect, parseColumn } from "numberparrot";
detect("1.234,56").value; // 1234.56, confidence 0.95
detect("1,234"); // ambiguous: true, alternatives [1234, 1.234]
parseColumn(["1,234", "12,5", "1.000,00"]).values;
// [1.234, 12.5, 1000]: "12,5" shows the comma is the decimal separatorIn a form field#
tryParse never throws and says why a value was rejected, which is handy for validation messages:
import { createParser } from "numberparrot";
const local = createParser(navigator.language); // for example "pt-BR"
input.addEventListener("change", () => {
const result = local.tryParse(input.value);
input.setCustomValidity(result.ok ? "" : `Not a number here: ${result.message}`);
});Install#
npm install numberparrotZero dependencies, ESM and CommonJS, TypeScript types included. Works in Node.js 18+, Deno, Bun and browsers. Paste your own numbers on the home page or read the API reference.