我正在关注这个:http://docs.angularjs.org/api/ng.filter:currency
当我在字段中输入 1234.56 时,输出应该是 $1,234.56。但是,如果我输入输入 1234,则输出为 $1,234.00。我不希望出现小数点和零。
如何才能做到这一点?
最佳答案
添加新过滤器:
'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 -->
关于AngularJS 货币过滤器 : can I remove the . 00 如果金额中没有美分?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17665487/