Created
July 31, 2017 14:46
-
-
Save AlexanderKozhevin/da71251291e3b9e020d0fe63c9b30f3f to your computer and use it in GitHub Desktop.
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
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