Created
October 20, 2012 15:20
-
-
Save alexanderscott/3923551 to your computer and use it in GitHub Desktop.
comma-separated thousands fxn
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
/* comma-separated thousands */ | |
// if no decimals | |
function numberWithCommas(x) { | |
return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ","); | |
} | |
// if more than 3 decimal places | |
function numberWithCommas(x) { | |
var parts = x.toString().split("."); | |
parts[0] = parts[0].replace(/\B(?=(\d{3})+(?!\d))/g, ","); | |
return parts.join("."); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment