Created
June 27, 2013 05:36
-
-
Save ahsquared/5874202 to your computer and use it in GitHub Desktop.
Format currency putting in commas every 3 digits and a $ sign at the front. Input a number, returns a string.
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
function formatCurrency (num) { | |
aDigits = num.toFixed(2).split("."); | |
aDigits[0] = aDigits[0].split("").reverse().join("").replace(/(\d{3})(?=\d)/g, "$1,").split("").reverse().join(""); | |
return "$" + aDigits.join("."); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment