Why AngularJS currency filter formats negative numbers with parenthesis?

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

Live Demo

Why this:

# Controller
$scope.price = -10;

# View
{{ price | currency }}

results in ($10.00) rather than

相关标签:
7条回答
  • 2020-12-28 14:40

    This is a popular way to represent negative currencies. Wikipedia:

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

    You can see in the Angular source code where they do this (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: '$'
          },
    
    0 讨论(0)
提交回复
热议问题