From São Paulo
Checkout and price fields
A shopper types 1.234,56 into your price field. normalizeInput keeps partial input valid while they type and hands you the number on blur.
normalizeInput("1.234,", "pt-BR").value;
// 1234Paste a number, see it read right
parseFloat("1.234,56") returns 1.234. numberparrot returns 1234.56. Paste a price, a bank export or a spreadsheet column in the box: it works out the format and reads every value.
Read as pt-BR, with "," as the decimal separator.
"1.234,56" (row 1) shows that "," is the decimal separator
1.234,56
1.234
1234.56
-87,90
-87
-87.9
12,5
12
12.5
1.000.000,00
1
1000000
0,29
0
0.29
1001159.45
World board
Type one number. Every locale writes it its own way, and numberparrot reads every one back.
Type a number in any format. Every tile writes it the local way, then reads it back.
parseFloat is wrong in 39 of 39 locales. numberparrot reads every one back.
New York
1,234,567.891
parseFloat1
numberparrot1234567.891
Toronto
1,234,567.891
parseFloat1
numberparrot1234567.891
Montréal
1 234 567,891
parseFloat1
numberparrot1234567.891
Mexico City
1,234,567.891
parseFloat1
numberparrot1234567.891
São Paulo
1.234.567,891
parseFloat1.234
numberparrot1234567.891
Buenos Aires
1.234.567,891
parseFloat1.234
numberparrot1234567.891
Bogotá
1.234.567,891
parseFloat1.234
numberparrot1234567.891
Santiago
1.234.567,891
parseFloat1.234
numberparrot1234567.891
London
1,234,567.891
parseFloat1
numberparrot1234567.891
Berlin
1.234.567,891
parseFloat1.234
numberparrot1234567.891
Paris
1 234 567,891
parseFloat1
numberparrot1234567.891
Madrid
1.234.567,891
parseFloat1.234
numberparrot1234567.891
Rome
1.234.567,891
parseFloat1.234
numberparrot1234567.891
Amsterdam
1.234.567,891
parseFloat1.234
numberparrot1234567.891
Lisbon
1 234 567,891
parseFloat1
numberparrot1234567.891
Zürich
1'234'567.891
parseFloat1
numberparrot1234567.891
Stockholm
1 234 567,891
parseFloat1
numberparrot1234567.891
Warsaw
1 234 567,891
parseFloat1
numberparrot1234567.891
Prague
1 234 567,891
parseFloat1
numberparrot1234567.891
Kyiv
1 234 567,891
parseFloat1
numberparrot1234567.891
Moscow
1 234 567,891
parseFloat1
numberparrot1234567.891
Istanbul
1.234.567,891
parseFloat1.234
numberparrot1234567.891
Cairo
١٬٢٣٤٬٥٦٧٫٨٩١
parseFloatNaN
numberparrot1234567.891
Riyadh
١٬٢٣٤٬٥٦٧٫٨٩١
parseFloatNaN
numberparrot1234567.891
Tehran
۱٬۲۳۴٬۵۶۷٫۸۹۱
parseFloatNaN
numberparrot1234567.891
Tel Aviv
1,234,567.891
parseFloat1
numberparrot1234567.891
Nairobi
1,234,567.891
parseFloat1
numberparrot1234567.891
Johannesburg
1 234 567,891
parseFloat1
numberparrot1234567.891
Mumbai
12,34,567.891
parseFloat12
numberparrot1234567.891
Delhi
12,34,567.891
parseFloat12
numberparrot1234567.891
Dhaka
১২,৩৪,৫৬৭.৮৯১
parseFloatNaN
numberparrot1234567.891
Karachi
1,234,567.891
parseFloat1
numberparrot1234567.891
Tokyo
1,234,567.891
parseFloat1
numberparrot1234567.891
Shanghai
1,234,567.891
parseFloat1
numberparrot1234567.891
Seoul
1,234,567.891
parseFloat1
numberparrot1234567.891
Bangkok
1,234,567.891
parseFloat1
numberparrot1234567.891
Hanoi
1.234.567,891
parseFloat1.234
numberparrot1234567.891
Jakarta
1.234.567,891
parseFloat1.234
numberparrot1234567.891
Sydney
1,234,567.891
parseFloat1
numberparrot1234567.891
Intl.NumberFormat in your browser and read back by numberparrot 1.1.0.
Departures
Each flight is a number written the local way. The status is what parseFloat did with it on arrival. numberparrot lands every one.
On time: parseFloat got it rightDiverted: a wrong numberCancelled: NaN
Use cases
Numbers rarely start in your code. They arrive from shoppers, banks, scrapers and spreadsheets, written the way their sender writes them.
From São Paulo
A shopper types 1.234,56 into your price field. normalizeInput keeps partial input valid while they type and hands you the number on blur.
normalizeInput("1.234,", "pt-BR").value;
// 1234From Berlin
A bank export has 1.234,56 in one column and 1,5 in the next. createParser reads every row in one pass, and detect() takes files with no locale.
createParser("de-DE").parse("1.234,56");
// 1234.56From Tokyo
Prices arrive with symbols and codes on either side. parseCurrency returns the amount and the ISO code, so you know what you are adding up.
parseCurrency("¥1,235", "ja-JP").currency;
// "JPY"From London
Negatives in parentheses, a trailing minus or the Unicode minus sign all read as negative, the way an accountant meant them.
parse("(1,234.56)", "en-GB");
// -1234.56From Mumbai
toMinorUnits shifts the decimal digits as text, so 0,29 is 29 cents and never 28.999999999999996. Lakh and crore grouping included.
parseCurrency("₹19.99", "en-IN", {
toMinorUnits: true,
}).amount;
// 1999From Cairo
Arabic-Indic, Persian, Devanagari, Bengali, Thai and full width digits read like Latin ones, with their own separators.
parse("١٬٢٣٤٫٥٦", "ar-EG");
// 1234.56API
Every function takes the locale as an argument, so data from Berlin, Mumbai and Cairo can sit side by side. No global state, nothing to register.
parse(input, locale?, options?): number
import { parse } from "numberparrot";
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
parse("(1.234,56)", "de-DE"); // -1234.56
parse("15%", "en-US"); // 0.15
parse("1.234,56", "en-US"); // NaN, not an en-US numberNode.js 18+, Deno, Bun and every modern browser. ESM, CommonJS and TypeScript types, about 7 kB gzipped.
npm install numberparrotGuides
Four short guides. Each one shows the usual approach, why it breaks and what to do instead.
FAQ
Because the meaning of . and , depends on who wrote the number. Swapping fixes "1,5" and breaks "1.234,56" and "1,234". parse(text, locale) reads the separators of that locale from Intl.NumberFormat instead.
It reads characters while they look like an English number and stops at the first one that does not, so parseFloat("1.234,56") is 1.234. A wrong number passes every isNaN check, which makes it worse than an error.
parseCurrency("R$ 1.234,56", "pt-BR") returns the amount 1234.56 and the code "BRL". Add toMinorUnits: true to get exact integer cents.
Paste it in the box at the top of this page. For one value, detect(text) works out the decimal separator and says when it cannot be sure. For a column, parseColumn(values) lets the clear cells decide for the unclear ones.
Node.js 18 and newer, Deno, Bun and modern browsers. It has zero dependencies, ships ESM, CommonJS and TypeScript types, and is about 7 kB gzipped.