Created
December 12, 2012 00:09
-
-
Save ashblue/4263619 to your computer and use it in GitHub Desktop.
Converts numbers into a string with commas
This file contains 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
/** | |
* Turns a number into a more readable format with commas | |
* @param {number} num Number for conversion | |
* @returns {string} Readable number such as 700,345,384 | |
*/ | |
function setCommas (num) { | |
var letters = (num).toString().split(''), | |
count = 0, | |
readableNum = ''; | |
// Add commas where appropriate | |
letters.forEach(function (l) { | |
readableNum += l; | |
count += 1; | |
if (count !== letters.length && (letters.length - count) % 3 === 0) { | |
readableNum += ','; | |
} | |
}); | |
return readableNum; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment