仅当货币类型具有分数时,才将金额显示为带分数的货币 [英] Display amount as currency with fractions only when currency type has fractions
本文介绍了仅当货币类型具有分数时,才将金额显示为带分数的货币的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用此代码显示金额:
I use this code to display amount:
<td>{{transaction.amount}} {{transaction.currency}}</td>
当前我的输出为:56030 USD
Currently I have this output: 56030 USD
类似的东西:
<td [ngSwitch]="transaction.currency">
<span *ngSwitchCase="'JPY'">/// Display 1000 JPY</span>
<span *ngSwitchCase="'USD'">/// Display 10.00 USD</span>
<span *ngSwitchDefault class="badge">/// Display 10.00 USD</span>
</td>
如何根据货币添加.00?
How I can add .00 based on currency?
推荐答案
您现在可以稍作改动,以实现所需的目标:
You can alter the way you do it now a bit to achieve what you want:
<td *ngIf='transaction.currency=="JPY"'>{{transaction.amount|number:'1.0-0'}} {{transaction.currency}}</td>
<td *ngIf='transaction.currency!=="JPY"'>{{transaction.amount|number:'1.2-2'}} {{transaction.currency}}</td>
第一个管道"| number:'1.2-2'"会将其格式设置为数字,点后有2个小数,另一部分只是交易对象中的文本
The first pipe "|number:'1.2-2'" will format it as number, with 2 fraction after the dot, and the other section is just text from your transaction object
建议的更好的解决方案是
A bit better solution as suggested is
<td>{{ transaction.amount | currency:transaction.currency:'' }} {{ transaction.currency }}</td>
或者如果您不介意左侧的货币代码,则可以使用:
or if you don't mind the currency code on the left side you can use:
<td>{{ transaction.amount | currency:transaction.currency:'code' }}</td>
这篇关于仅当货币类型具有分数时,才将金额显示为带分数的货币的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文