Skip to content

Instantly share code, notes, and snippets.

@leandrowd
Created July 9, 2014 13:09
Show Gist options
  • Save leandrowd/23bb94004d48cc0e19c5 to your computer and use it in GitHub Desktop.
Save leandrowd/23bb94004d48cc0e19c5 to your computer and use it in GitHub Desktop.
Format decimal numbers
function format(number){
var str = ""+ number,
arr = str.split('').reverse(),
formatted = [],
ret;
arr.unshift('-');
for(var x = 1, len = arr.length; x <= len; x++){
formatted.push(arr[x]);
if(x % 3 === 0 && x < len - 1) {
formatted.push(',');
}
}
ret = formatted.reverse().join('');
console.log(ret);
return ret;
}
format(1);
format(10);
format(1000000);
format(10000000000);
format(1000000000);
format(78967689569580);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment