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

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

问题描述

有没有办法从货币过滤器的输出中删除小数/美分?我正在做这样的事情:

{{价格 |货币}}

输出:

<块引用>

$1,000.00

相反,我想:

<块引用>

1,000 美元

可以使用货币过滤器来完成吗?我知道我可以在数字前添加美元符号,也可以编写自己的过滤器,但我希望现有货币过滤器存在一种简单的方法.

谢谢.

解决方案

更新:从版本1.3.0 -currencyFilter:添加fractionSize作为可选参数,见提交并更新了 plunker

<代码>{{10 |货币:未定义:0}}

请注意,这是第二个参数,因此您需要传入 undefined 以使用当前语言环境的货币符号

更新:请注意,这仅适用于显示在数字之前的货币符号.从 1.2.9 版本开始,它仍然硬编码到小数点后两位.

这里是一个修改版本,它使用 angular 的 formatNumber 的副本来启用 0 fractionSize用于货币.

<小时>

通常这应该可以在语言环境定义或货币过滤器调用中进行配置,但现在(1.0.4)它被硬编码到小数点后两位.

自定义过滤器:

myModule.filter('noFractionCurrency',[ '$filter', '$locale',功能(过滤器,语言环境){var currencyFilter = filter('currency');var 格式 = locale.NUMBER_FORMATS;返回函数(金额,货币符号){var value =currencyFilter(amount,currencySymbol);var sep = value.indexOf(formats.DECIMAL_SEP);如果(金额> = 0){return value.substring(0, sep);}return value.substring(0, sep) + ')';};}]);

模板:

{{价格 |noFractionCurrency}}</div>

示例:

更新:修复了处理负值时的错误

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>

Which outputs:

$1,000.00

Instead, I'd like:

$1,000

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.

Thank you.

解决方案

Update: as of version 1.3.0 - currencyFilter: add fractionSize as optional parameter, see commit and updated 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

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.

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


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.

Custom filter:

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) + ')';
    };
  } ]);

Template:

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

Example:

Update: fixed a bug when handling negative values

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

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