AngularJS 货币过滤器 : can I remove the . 00 如果金额中没有美分?

标签 angularjs

我正在关注这个: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/

相关文章:

javascript - 如何使用 AngularJS 创建嵌套表?

javascript - Angularjs $scope 值未更新

javascript - Paypal 不接受 angularjs 中的 FORM

javascript - Excel 到 Javascript - 忽略从 Excel 复制的列中的换行符

javascript - 使用 Angularjs 在 DeviceReady 后注入(inject)服务或执行服务

javascript - Angular Material 2 - 未在整页中渲染

javascript Angularjs 从数组中获取单个值

angularjs - 在 AngularJS 中使用 ng-repeat 过滤结果 6 到 10(共 100 个)

html - 如何找到控制台错误的位置

javascript - 使用深度比较或 json.stringify 比较两个对象?