Create a gist now

Instantly share code, notes, and snippets.

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment