-1

In c# you can do this

String.format(5000, "$###,###,##0.00;-$###,###,##0.00;$0.00")

Which will output: $5,000

Or you can do this

String.format(-5000, "$###,###,##0.00;-$###,###,##0.00;$0.00")

Which will output: -$5,000

How do I do the same in Javascript??

UPDATE: I would also need to format a currency like this

 String.format(-5000, "###.###.##0.00€;-###,###,##0.00€;0.00€")

Which would output: 5.000,00 €

Paul
  • 1,457
  • 1
  • 19
  • 36
  • [http://stackoverflow.com/questions/149055/how-can-i-format-numbers-as-money-in-javascript][1] [1]: http://stackoverflow.com/questions/149055/how-can-i-format-numbers-as-money-in-javascript – imnancysun Dec 30 '14 at 01:21
  • The format above i.e. $###,###,##0.00;-$###,###,##0.00;$0.00 is fixed and cannot be changed. I don't believe the suggested answer solves that problem. For example if the currency is in Euro's the sign will exist at the end of the format – Paul Dec 30 '14 at 01:25
  • Wow you ask a question then downvote when you are not satisfied with the answer. We are here to help not compete! This is the first time I encountered an OP that downvote people that are trying to help him. – agentpx Dec 30 '14 at 02:37
  • I didn't downvote it was someone else... – Paul Dec 30 '14 at 02:38

2 Answers2

0

you should really consider using toLocaleString so your code can rely on configuration settings like locale and currency rather than customized string format patterns.

const formatCurrency = (num, locale = 'en-US', currency = 'USD', minimumFractionDigits = 2) => {
    if (isNaN(num)) {
        return num;
    }
  return num.toLocaleString(locale, {style: 'currency', currency, minimumFractionDigits});
};

Here is an example fiddle

Ken Gregory
  • 7,180
  • 1
  • 39
  • 43
-1

I Ended up rolling my own:

app.filter('formatMoney', ['$filter', function ($filter) {
    return function (value, format) {
        var v = format.split(';');
        var index = 2;
        if (value > 0)
            index = 0;
        else if (value < 0)
            index = 1;
        else {
            return v[index];
        }

        var len = v[index].length;
        var fIndex = v[index].indexOf('#');
        var lIndex = v[index].lastIndexOf('0');
        var f = v[index].substring(fIndex, lIndex + 1);
        var symbolFirst = '';
        var symbolLast = '';
        if (lIndex !== len - 1) {
            symbolLast = v[index].substring(len, len - 1);
        }
        if (fIndex > 0) {
            symbolFirst = v[index].substring(0, fIndex);
        }
        var valueStr = value.toFixed(2).toString();
        for (var i = f.length - 1; i > 0 && valueStr.length > 0; i--) {
            var slice = valueStr.slice(-1);
            if (f[i] === '#' || f[i].isNumeric()) {
                valueStr = valueStr.slice(0, -1);
                if (slice.isNumeric()) {
                    f = f.replaceAt(i, slice);
                    var j = valueStr.length - 1;
                    while (j > 0 && !valueStr[j].isNumeric()) {
                        valueStr = valueStr.slice(0, -1);
                        j = j - 1;
                    }
                }
            }
        }
        return symbolFirst + f.substr(f.search(/\d/), f.length - 1) + symbolLast;
    };
}]);
Paul
  • 1,457
  • 1
  • 19
  • 36