为什么 AngularJS 货币过滤器用括号格式化负数? [英] Why AngularJS currency filter formats negative numbers with parenthesis?

查看:28
本文介绍了为什么 AngularJS 货币过滤器用括号格式化负数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现场演示

为什么会这样:

# Controller
$scope.price = -10;

# View
{{ price | currency }}

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

推荐答案

这是表示负货币的一种流行方式.维基百科:

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.

您可以在 Angular 源代码中看到他们在何处执行此操作 (negSuf/negPre):

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: '$'
      },

这篇关于为什么 AngularJS 货币过滤器用括号格式化负数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆