Skip to content

Instantly share code, notes, and snippets.

@vstoykov
Created July 30, 2012 11:43
Show Gist options
  • Save vstoykov/3206375 to your computer and use it in GitHub Desktop.
Save vstoykov/3206375 to your computer and use it in GitHub Desktop.
Pretty print numbers with thousand delimiter
var intcomma = function(value, delimiter) {
/*
Original script found here https://gist.github.com/559757
Added optional argument delimiter to choose witch symbol
to use for thousand delimiter
*/
var origValue = String(value);
if (typeof(delimiter) == 'undefined') delimiter = ',';
var newValue = origValue.replace(/^(-?\d+)(\d{3})/, '$1'+delimiter+'$2');
if (origValue == newValue){
return newValue;
} else {
return intcomma(newValue, delimiter);
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment