删除AngularJS货币过滤小数/美分 [英] Removing AngularJS currency filter decimal/cents

查看:475
本文介绍了删除AngularJS货币过滤小数/美分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法从货币滤波器的输出去掉小数点/分?我在做这样的事情:

Is there a way to remove the decimal/cents from the output of a currency filter? I'm doing something like this:

<div>{{Price | currency}}</div>

它输出:

$ 1,000.00

$1,000.00

相反,我想:

1000 $

可以在使用货币过滤器做了什么?我知道我可以prePEND一个美元符号到一个号码,我可以写我自己的过滤器,但我希望一个简单的方法,与现有的货币过滤器存在。

Can that be done using the currency filter? I know I can prepend a dollar sign onto a number, and I could write my own filter, but I was hoping a simple method exists with the existing currency filter.

感谢您。

推荐答案

更新:随着版本的 1.3.0 - currencyFilter:添加fractionSize作为可选参数,请参阅commit
和更新 plunker

{{10 | currency:undefined:0}}

请注意,它的第二个参数,所以你需要在不确定的传递给使用当前区域设置的货币符号

Note that it's the second parameter so you need to pass in undefined to use the current locale currency symbol

更新:注意到,对于货币符号这只作品的号码前显示。
随着版本的 1.2.9 它仍然很难coded到小数点后2位。

Update: Take note that this only works for currency symbols that are displayed before the number. As of version 1.2.9 it's still hardcoded to 2 decimal places.

这里是采用棱角分明的formatNumber副本的修改版本,以使0 fractionSize货币

Here is a modified version that uses a copy of angular's formatNumber to enable 0 fractionSize for currency.

通常这应该是可配置无论是在语言环境定义或currencyFilter呼叫,但现在( 1.0.4 ),这是很难coded到小数点后2位。

Normally this should be configurable either in the locale definition or the currencyFilter call but right now(1.0.4) it's hardcoded to 2 decimal places.

自定义过滤器:

myModule.filter('noFractionCurrency',
  [ '$filter', '$locale',
  function(filter, locale) {
    var currencyFilter = filter('currency');
    var formats = locale.NUMBER_FORMATS;
    return function(amount, currencySymbol) {
      var value = currencyFilter(amount, currencySymbol);
      var sep = value.indexOf(formats.DECIMAL_SEP);
      if(amount >= 0) { 
        return value.substring(0, sep);
      }
      return value.substring(0, sep) + ')';
    };
  } ]);

模板:

<div>{{Price | noFractionCurrency}}</div>

示例:

更新:在处理负值时,修正了一个错误

Update: fixed a bug when handling negative values

这篇关于删除AngularJS货币过滤小数/美分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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