Skip to content

Instantly share code, notes, and snippets.

@nkalvi
Created September 11, 2023 03:58
Show Gist options
  • Save nkalvi/4da84a61d443f1aa77a410df629972bb to your computer and use it in GitHub Desktop.
Save nkalvi/4da84a61d443f1aa77a410df629972bb to your computer and use it in GitHub Desktop.
Get locale-based number formatting options in JavaScript
// https://codepen.io/nkalvi/pen/MWZpzOg
// Get separator symbols as .decimal and .thousands
// for given locale or default locale
function getNumberSeparators(locale) {
const str = (1234.56).toLocaleString(locale);
const regex = /\p{Nd}(?<thousands>[^\p{Nd}]?)\p{Nd}{3}(?<decimal>[^\p{Nd}]?)\p{Nd}{2}/u;
const found = str.match(regex);
if (!found || !found.groups) return undefined;
return found.groups;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment