Skip to content

Instantly share code, notes, and snippets.

@AlexanderKozhevin
Created July 31, 2017 14:46
Show Gist options
  • Save AlexanderKozhevin/da71251291e3b9e020d0fe63c9b30f3f to your computer and use it in GitHub Desktop.
Save AlexanderKozhevin/da71251291e3b9e020d0fe63c9b30f3f to your computer and use it in GitHub Desktop.
var putThousandsSeparators;
putThousandsSeparators = function(value, sep) {
if (sep == null) {
sep = ',';
}
// check if it needs formatting
if (value.toString() === value.toLocaleString()) {
// split decimals
var parts = value.toString().split('.')
// format whole numbers
parts[0] = parts[0].replace(/\B(?=(\d{3})+(?!\d))/g, sep);
// put them back together
value = parts[1] ? parts.join('.') : parts[0];
} else {
value = value.toLocaleString();
}
return value;
};
alert(putThousandsSeparators(1234567.890));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment