Skip to content

Instantly share code, notes, and snippets.

@ahsquared
Created June 27, 2013 05:36
Show Gist options
  • Save ahsquared/5874202 to your computer and use it in GitHub Desktop.
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.
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