Skip to content

Instantly share code, notes, and snippets.

@AndersonFirmino
Created December 13, 2019 17:44
Show Gist options
  • Select an option

  • Save AndersonFirmino/e0909152382a1ced1ffeaca24afacf6c to your computer and use it in GitHub Desktop.

Select an option

Save AndersonFirmino/e0909152382a1ced1ffeaca24afacf6c to your computer and use it in GitHub Desktop.

Revisions

  1. AndersonFirmino created this gist Dec 13, 2019.
    10 changes: 10 additions & 0 deletions formatMoney.js
    Original 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));