Created
December 13, 2019 17:44
-
-
Save AndersonFirmino/e0909152382a1ced1ffeaca24afacf6c to your computer and use it in GitHub Desktop.
Revisions
-
AndersonFirmino created this gist
Dec 13, 2019 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,10 @@ 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));