Why AngularJS currency filter formats negative numbers with parenthesis?

后端 未结 7 764
别跟我提以往
别跟我提以往 2020-12-28 14:05

Live Demo

Why this:

# Controller
$scope.price = -10;

# View
{{ price | currency }}

results in ($10.00) rather than

7条回答
  •  萌比男神i
    2020-12-28 14:21

    Do you mean display -$10.00 rather than ($10.00)?

    The default, at least angularJs version 1.2.1 is to display with parentheses. Eg.: ($10.00)).

    If so, this is my situation. I created a custom filter for that:

    var app = angular.module('myApp');
    
    app.filter('customCurrency', ["$filter", function ($filter) {       
      return function(amount, currencySymbol){
         var currency = $filter('currency');         
    
         if(amount.charAt(0) === "-"){
            return currency(amount, currencySymbol).replace("(", "-").replace(")", ""); 
         }
    
         return currency(amount, currencySymbol);
      };
    
    }]);
    

    So it delegates to the built-in currency filter and "decorates" or "un-decorates" the parentheses.

    I couldn't find a way to change $LocaleProvider on the fly. If someone knows please let me know.

    cheers Leonardo Correa

提交回复
热议问题