Skip to content

Instantly share code, notes, and snippets.

@ashblue
Created December 12, 2012 00:09
Show Gist options
  • Save ashblue/4263619 to your computer and use it in GitHub Desktop.
Save ashblue/4263619 to your computer and use it in GitHub Desktop.
Converts numbers into a string with commas
/**
* 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