Create a gist now

Instantly share code, notes, and snippets.

number formatting
function formatNumber(num) {
num = num.toString();
var result = '';
for (var i = 1; i <= num.length; i++) {
result = num[num.length - i] + result;
if (i % 3 === 0 && i < num.length) {
result = ',' + result;
}
}
return result;
}
console.log(formatNumber(1));
console.log(formatNumber(22));
console.log(formatNumber(333));
console.log(formatNumber(4444));
console.log(formatNumber(55555));
console.log(formatNumber(666666));
console.log(formatNumber(7777777));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment