public
Created — forked from kevinvaldek/gist:232271

  • Download Gist
gistfile1.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
 
function number_with_delimiter(number, delimiter) {
number = number + '', delimiter = delimiter || ',';
var split = number.split('.');
split[0] = split[0].replace(/(\d)(?=(\d\d\d)+(?!\d))/g, '$1' + delimiter);
return split.join('.');
};
 
// Test drive
 
number_with_delimiter(12345678)
> 12,345,678
 
number_with_delimiter(12345678, ':')
> 12:345:678
 
number_with_delimiter(12345678.555)
> 12,345,678.555

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.