Created
May 8, 2017 12:32
-
-
Save joshuacharleslake/14e5c2dd879433b5b35b2b99c9c6615d to your computer and use it in GitHub Desktop.
Format a number into currency.
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
//format number to currency | |
function formatCurrency(num) { | |
var p = num.toFixed(2).split("."); | |
return "£" + p[0].split("").reverse().reduce(function(acc, num, i, orig) { | |
return num=="-" ? acc : num + (i && !(i % 3) ? "," : "") + acc; | |
}, "") + "." + p[1]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment