Skip to content

Instantly share code, notes, and snippets.

@giolvani
Created January 16, 2019 16:44
Show Gist options
  • Save giolvani/cc4e0a28c9830fcd39bc10462c091eb5 to your computer and use it in GitHub Desktop.
Save giolvani/cc4e0a28c9830fcd39bc10462c091eb5 to your computer and use it in GitHub Desktop.
Get number separator by locale
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