AngularJS货币过滤:我可以删除.00是否有量无分? [英] AngularJS currency filter: can I remove the .00 if there are no cents in the amount?

查看:100
本文介绍了AngularJS货币过滤:我可以删除.00是否有量无分?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我下面这样:<一href=\"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。我不想小数和零出现。
如何才能做到这一点?

I am following this: http://docs.angularjs.org/api/ng.filter:currency When I type 1234.56 in the field then the output should be $1,234.56. But if I type in the input 1234 then the output is $1,234.00. I don't want the decimal and the zeros to appear. How can this be done?

推荐答案

添加一个新的过滤器:

'use strict';

angular.module('myApp').filter('myCurrency', ['$filter', function ($filter) {
  return function(input) {
    input = parseFloat(input);

    if(input % 1 === 0) {
      input = input.toFixed(0);
    }
    else {
      input = input.toFixed(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货币过滤:我可以删除.00是否有量无分?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆