Created
July 9, 2014 13:09
-
-
Save leandrowd/23bb94004d48cc0e19c5 to your computer and use it in GitHub Desktop.
Format decimal numbers
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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