javascript - 如何在angularjs中对数字进行复数化和格式化

标签 javascript angularjs number-formatting pluralize

我想格式化一个数字并用 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 demonumber of people 框中。


我如何格式化一个数字并将其复数化?

最佳答案

这里不需要使用正则表达式。

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

<ng-pluralize count="amount" when="{'0': 'no bitcoins', 
                     '1': '1 bitcoin', 
                     'other': '{{amount | number:2}} bitcoins'}">
</ng-pluralize>

Working plunker .

关于javascript - 如何在angularjs中对数字进行复数化和格式化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20526897/

相关文章:

javascript - 为什么我的 reducer 在 react/redux 中返回一个空数组?

javascript - 使用循环更改 DOM &lt;input&gt; 值

javascript - Angular 1.4.8/JS : create constructor and inherit properties into a 3rd object

java - 解析带有负后缀的数字

java - 无法解释数字格式异常

javascript - 世博会文字转语音语音信息

javascript - 为什么单击 ag-grid 中的过滤器时行会消失?

javascript - Angular JS 中的 $event 是未定义的类型错误

javascript - 何时在 Controller 中使用 $scope.$watch

ruby-on-rails - 如何将零填充为小数结果