Created
February 15, 2017 04:02
-
-
Save olveirap/7271ae2459ef17745f266def96a7fe3b to your computer and use it in GitHub Desktop.
Price formatter function
This file contains hidden or 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
//http://stackoverflow.com/questions/149055/how-can-i-format-numbers-as-money-in-javascript | |
function formatPrice(aPrice){ | |
var n = aPrice; | |
var i = String(parseInt(n = Math.abs(Number(n) || 0).toFixed(2))); | |
var j = (j = i.length) > 3 ? j % 3 : 0; | |
return (j ? i.substr(0, j) + "." : "") + i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + ".") + (2 ? "," + Math.abs(n - i).toFixed(2).slice(2) : ""); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment