AngularJS 货币过滤器:如果金额中没有美分,我可以删除 .00 吗?

2024-05-07

我正在关注这个:http://docs.angularjs.org/api/ng.filter:currency 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 -->
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

AngularJS 货币过滤器:如果金额中没有美分,我可以删除 .00 吗? 的相关文章