Created
December 13, 2019 17:44
-
-
Save AndersonFirmino/e0909152382a1ced1ffeaca24afacf6c to your computer and use it in GitHub Desktop.
Formatar moeda no valor $ americano para R$ real.
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 formatMoney(n, c, d, t) { | |
c = isNaN(c = Math.abs(c)) ? 2 : c, d = d == undefined ? "," : d, t = t == undefined ? "." : t, s = n < 0 ? "-" : "", i = parseInt(n = Math.abs(+n || 0).toFixed(c)) + "", j = (j = i.length) > 3 ? j % 3 : 0; | |
return s + (j ? i.substr(0, j) + t : "") + i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + t) + (c ? d + Math.abs(n - i).toFixed(c).slice(2) : ""); | |
} | |
console.log(formatMoney(600000.00)); | |
console.log(formatMoney(10)); | |
console.log(formatMoney(100)); | |
console.log(formatMoney(0.5)); | |
console.log(formatMoney(1500)); | |
console.log(formatMoney(89)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment