角货币管道没有小数值

时间:2018-03-21 10:30:14

标签: angular angular5

我正在尝试使用角度的货币管道来显示整数价格,我不需要它将.00添加到我的号码,事情是,它不是根据我的指示格式化它。 这是我的HTML:

              <h5 class="price"><span>{{billingInfo.amount | currency:billingInfo.currencyCode:'1.0-0'}}</span> {{billingInfo.period}}</h5>

这是我的问题:

ngOnInit() {
      this.billingInfo = {amount: 100, currencyCode: 'USD', period: 'Hour'};
}

这是输出:

$100.00 Hour

我试图做的事情:

1.使用十进制数管道(不好,货币管道将其变成一个字符串)

2.添加号码格式化程序(:1.0-0)到我的currencyPipe但似乎被忽略了

我错过了什么?

6 个答案:

答案 0 :(得分:21)

要从货币管道中删除.00,您可以使用此模式。

{{ amount | currency : 'USD' : 'symbol' : '1.0' }} 

如果您不需要小数,则可以使用数字管道。

${{amount | number}}

答案 1 :(得分:1)

最好的我可以告诉你,只是错过了显示参数,它应该是货币管道的第二个参数,所以如果你把它改成以下它应该有效:

<h5 class="price"><span>{{billingInfo.amount | currency:billingInfo.currencyCode:'symbol':'1.0-0'}}</span> {{billingInfo.period}}</h5>

答案 2 :(得分:0)

参考https://angular.io/api/common/DecimalPipe

    <h5 class="price"><span>{{billingInfo.amount | 
    currency:billingInfo.currencyCode:true:'1.0-0'}}</span> {{billingInfo.period}}</h5>

答案 3 :(得分:0)

在Angular中,要格式化货币,请在此处显示的数字上使用货币管道。

<p>{{amount | currency:'USD':true:'1.2-2'}}</p>
  • 管道的第一个参数“ USD”是ISO货币代码(例如 “ USD”,“ EUR”等)
  • 第二个参数true为可选布尔值,用于指定是否 是否要呈现货币符号(“ $”,“€”);默认是 错误
  • 第三个参数“ 1.2-2”(也是可选参数)指定如何设置格式 该数字,使用与该数字相同的格式设置规则 管道。

答案 4 :(得分:0)

这是我的解决方案:

amount = 5; //or 5.00;
{{ amount | currency: 'USD':true:'2.0' }}

Output would be: 5, 

如果金额设置为5.99,则输出将为5.99

您可以在https://angular.io/api/common/CurrencyPipe

处从尖锐的CurrencyPipe中获取更多信息

答案 5 :(得分:0)

我正在阅读这个主题,如果你问我,我没有看到“完美”的答案。如果提供了小数,我曾经使用以下解决方法显示小数,但如果不需要,则将其删除。

{{ value | currency: 'EUR':'symbol': (value % 1 == 0) ? '1.0-0': '1.2-2' }}

如您所见,value % 1 == 0 是一个 mod 功能,用于检查该值是否可被整数整除。如果不是这样,它将返回 false

(15.00 % 1 == 0) -> true
(15.50 % 1 == 0) -> false
相关问题