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

标签 angularjs angularjs-filter

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

关于angularjs - 为什么AngularJS货币过滤器使用括号将负数格式化?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17441254/

相关文章:

angularjs - 每次新建项目都要安装express、node等吗?

javascript - 单元测试 Angular + Protractor

javascript - Angularjs ui-router 无法从状态 'results/0' 解析 'survey'

javascript - Angularjs - 动态添加和更新 DOM

javascript - AngularJS - 以编程方式检查过滤器是否存在

javascript - orderBy 过滤器只工作一次

javascript - AngularJs 动态 orderBy 表达式

javascript - 当输入 ng-model 是 ngRepeat 变量时如何从输入数据中获取?

javascript - Angular JS 类别过滤器无法递归工作

javascript - 如何设置自定义对象属性以在 Angular typeahead 中进行过滤?