Created
September 11, 2023 03:58
-
-
Save nkalvi/4da84a61d443f1aa77a410df629972bb to your computer and use it in GitHub Desktop.
Get locale-based number formatting options in JavaScript
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
// 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