Skip to content

Instantly share code, notes, and snippets.

@tytskyi
Last active August 29, 2015 14:10
Show Gist options
  • Save tytskyi/f6820d4ab8b577028fec to your computer and use it in GitHub Desktop.
Save tytskyi/f6820d4ab8b577028fec to your computer and use it in GitHub Desktop.
var formatThousands = function (input, separator) {
var output = [];
input = String(input).split('').reverse();
if (separator === void 0 || separator == null) {
separator = ',';
}
for (var i = 0, len = input.length, lastIndex = len - 1; i < len; i++) {
output.push(input[i]);
if (!((i + 1) % 3) && i < lastIndex) {
output.push(separator);
}
}
return output.reverse().join('');
};
formatThousands(1000);
// 1,000
formatThousands(3000, '.');
// 3.000
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment