私はこれに従っています: http://docs.angularjs.org/api/ng.filter:currency フィールドに 1234.56 と入力すると、出力は $1,234.56 になります。しかし、入力 1234 を入力すると、出力は $1,234.00 になります。小数とゼロを表示したくありません。これはどのように行うことができますか?
17783 次
3 に答える
17
新しいフィルターを追加します。
'use strict';
angular
.module('myApp')
.filter('myCurrency', ['$filter', function($filter) {
return function(input) {
input = parseFloat(input);
input = input.toFixed(input % 1 === 0 ? 0 : 2);
return '$' + input.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ',');
};
}]);
あなたの見解では:
<span>{{ '100' | myCurrency }}</span> <!-- Result: $100 -->
<span>{{ '100.05' | myCurrency }}</span> <!-- Result: $100.05 -->
<span>{{ '1000' | myCurrency }}</span> <!-- Result: $1,000 -->
<span>{{ '1000.05' | myCurrency }}</span> <!-- Result: $1,000.05 -->
于 2013-07-16T00:06:27.547 に答える