Last active
August 9, 2021 08:20
-
-
Save gu-stav/c8ab96802d2826939e3996ba351e1f65 to your computer and use it in GitHub Desktop.
Parse a number-like string to a locale aware Number
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const parseLocaleNumber = (input, locale) => { | |
const formatter = Intl.NumberFormat(locale); | |
const [, { value: thousandSeparator }] = formatter.formatToParts(11111); | |
const [, { value: decimalSeparator }] = formatter.formatToParts(1.1); | |
return parseFloat(input | |
.replace(new RegExp('\\' + thousandSeparator, 'g'), '') | |
.replace(new RegExp('\\' + decimalSeparator), '.') | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Grundsätzlich fein. Hier ist ein recht kompletter Ansatz: https://observablehq.com/@mbostock/localized-number-parsing
Hier gefunden: https://stackoverflow.com/a/55366435