Skip to content

Instantly share code, notes, and snippets.

@gu-stav
Last active August 9, 2021 08:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gu-stav/c8ab96802d2826939e3996ba351e1f65 to your computer and use it in GitHub Desktop.
Save gu-stav/c8ab96802d2826939e3996ba351e1f65 to your computer and use it in GitHub Desktop.
Parse a number-like string to a locale aware Number
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), '.')
);
}
@mstoltenburg
Copy link

mstoltenburg commented Aug 9, 2021

Grundsätzlich fein. Hier ist ein recht kompletter Ansatz: https://observablehq.com/@mbostock/localized-number-parsing

Hier gefunden: https://stackoverflow.com/a/55366435

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment