我想格式化一个数字并用 Angular 将其复数。
例如(给定一些比特币):
0 => "John has no bitcoins"
1 => "John has 1 bitcoin"
2 => "John has 2 bitcoins"
12345.6789 => "John has 12,345.67 bitcoins"
我尝试过的:
John has
<ng-pluralize count="bitcoin_amount | round:2"
when="{'0': 'no bitcoins',
'1': '1 bitcoin',
'other': '{} bitcoins'}">
</ng-pluralize>
但这失败得很惨,因为对于等于或大于 1000 的数字,它们在 count
属性中作为 1,000
传递,因此只显示千。
例如:
1001 => 1
1000 => 1
2000 => 2
etc...
例如,尝试将 1,000
粘贴到 this demo 的 number of people
框中。
我如何格式化一个数字并将其复数化?
最佳答案
这里不需要使用正则表达式。
您可以直接在 ng-pluralize
指令的 when
属性中传递您的逻辑,如下所示:
<ng-pluralize count="amount" when="{'0': 'no bitcoins',
'1': '1 bitcoin',
'other': '{{amount | number:2}} bitcoins'}">
</ng-pluralize>
关于javascript - 如何在angularjs中对数字进行复数化和格式化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20526897/