Created
January 16, 2019 16:44
-
-
Save giolvani/cc4e0a28c9830fcd39bc10462c091eb5 to your computer and use it in GitHub Desktop.
Get number separator by locale
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
function getNumberSeparators (locale) { | |
// defaults | |
var res = { "decimal": ".", "thousand": "" }; | |
// convert a number formatted according to locale | |
var str = parseFloat(1234.56).toLocaleString(locale); | |
// if the resulting number does not contain previous number | |
// (i.e. in some Arabic formats), return defaults | |
if (!str.match("1")) | |
return res; | |
// get decimal and thousand separators | |
res.decimal = str.replace(/.*4(.*)5.*/, "$1"); | |
res.thousand = str.replace(/.*1(.*)2.*/, "$1"); | |
// return results | |
return res; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment