Skip to main content
 首页 » 编程设计

angularjs之为什么AngularJS货币过滤器使用括号将负数格式化

2024年09月07日16xing901022

Live Demo

为什么这个:

# Controller 
$scope.price = -10; 
 
# View 
{{ price | currency }} 

结果是 ($10.00)而不是 -$10.00

请您参考如下方法:

这是代表负货币的一种流行方法。 Wikipedia:

In bookkeeping, amounts owed are often represented by red numbers, or a number in parentheses, as an alternative notation to represent negative numbers.



您可以在Angular源代码中看到它们的执行位置( negSuf / negPre):
function $LocaleProvider(){ 
  this.$get = function() { 
    return { 
      id: 'en-us', 
 
      NUMBER_FORMATS: { 
        DECIMAL_SEP: '.', 
        GROUP_SEP: ',', 
        PATTERNS: [ 
          { // Decimal Pattern 
            minInt: 1, 
            minFrac: 0, 
            maxFrac: 3, 
            posPre: '', 
            posSuf: '', 
            negPre: '-', 
            negSuf: '', 
            gSize: 3, 
            lgSize: 3 
          },{ //Currency Pattern 
            minInt: 1, 
            minFrac: 2, 
            maxFrac: 2, 
            posPre: '\u00A4', 
            posSuf: '', 
            negPre: '(\u00A4', 
            negSuf: ')', 
            gSize: 3, 
            lgSize: 3 
          } 
        ], 
        CURRENCY_SYM: '$' 
      },