当前位置:/AngularJS API/ng/过滤器(filter)/currency

ng

格式化数字为货币格式。


用法

在html的模板绑定中

{{ currency_expression | currency : symbol : fractionSize}}	

在Javascript中

$filter('currency')(amount, symbol, fractionSize)

参数

参数 类型 描述
amount number 用来过滤的数值
symbol (可选) string 要显示的货币符号或标识符。
fractionSize (可选) number 整数的小数点的数目,默认为当前区域的默认最大大小

返回

string 格式化的数字


实例

<script>
  angular.module('currencyExample', [])
    .controller('ExampleController', ['$scope', function($scope) {
      $scope.amount = 1234.56;
    }]);
</script>
<div ng-controller="ExampleController">
  <input type="number" ng-model="amount" aria-label="amount"> <br>
  default currency symbol ($): <span id="currency-default">{{amount | currency}}</span><br>
  custom currency identifier (USD$): <span id="currency-custom">{{amount | currency:"USD$"}}</span><br>
  no fractions (0): <span id="currency-no-fractions">{{amount | currency:"USD$":0}}</span>
</div>
it('should init with 1234.56', function() {
  expect(element(by.id('currency-default')).getText()).toBe('$1,234.56');
  expect(element(by.id('currency-custom')).getText()).toBe('USD$1,234.56');
  expect(element(by.id('currency-no-fractions')).getText()).toBe('USD$1,235');
});
it('should update', function() {
  if (browser.params.browser === 'safari') {
    // Safari does not understand the minus key. See
    // https://github.com/angular/protractor/issues/481
    return;
  }
  element(by.model('amount')).clear();
  element(by.model('amount')).sendKeys('-1234');
  expect(element(by.id('currency-default')).getText()).toBe('-$1,234.00');
  expect(element(by.id('currency-custom')).getText()).toBe('-USD$1,234.00');
  expect(element(by.id('currency-no-fractions')).getText()).toBe('-USD$1,234');
});