如何在angularjs中复数和格式化数字

Ben*_*ier 15 javascript pluralize number-formatting angularjs

我想格式化一个数字并用angular来复数.

例如(给定一些比特币):

         0 => "John has no bitcoins"
         1 => "John has 1 bitcoin"
         2 => "John has 2 bitcoins"
12345.6789 => "John has 12,345.67 bitcoins"
Run Code Online (Sandbox Code Playgroud)

我尝试过的:

John has
<ng-pluralize count="bitcoin_amount | round:2" 
              when="{'0': 'no bitcoins', 
                     '1': '1 bitcoin', 
                     'other': '{} bitcoins'}">
</ng-pluralize>
Run Code Online (Sandbox Code Playgroud)

但这种失败草草收场,因为对数字等于或大于1000更大,它们是作为传递1,000count属性,所以只有几千所示.例如:

1001 => 1
1000 => 1
2000 => 2
etc...
Run Code Online (Sandbox Code Playgroud)

尝试粘贴1,000在框中number of people这个演示的例子.


如何格式化数字并将其复数为有角度?

Ben*_*ier 25

这里没有必要使用常规表达.

您可以直接在指令的when属性中传递逻辑,ng-pluralize如下所示:

<ng-pluralize count="amount" when="{'0': 'no bitcoins', 
                     '1': '1 bitcoin', 
                     'other': '{{amount | number:2}} bitcoins'}">
</ng-pluralize>
Run Code Online (Sandbox Code Playgroud)

工作的plunker.


Mat*_*erg 6

你可以删除逗号并让它处理吗?

John has
<ng-pluralize count="bitcoin_amount.replace(',','') | round:2" 
              when="{'0': 'no bitcoins', 
                     '1': '1 bitcoin', 
                     'other': '{} bitcoins'}">
</ng-pluralize>
Run Code Online (Sandbox Code Playgroud)

jsfiddle http://jsfiddle.net/9zmVW/