AngularJS:货币格式化。更改小数位数、千位分隔符和小数分隔符。

6

我希望在我的网站中特定的表格中以3位小数显示价格。

价格将以英镑、美元和欧元显示。

我的货币过滤器似乎自动将价格舍入到2位小数。

此外,如果货币为欧元,最好将千位分隔符自动更改为 . ,小数分隔符更改为 ,

AngularJS是否支持此功能?

视图

<div ng-controller="SpotController">
    <table class="header-table-spot-prices">
        <thead>
            <tr><th>Spot Prices</th><th>Price</th></tr>
        </thead>
        <tbody>
            <tr ng-repeat="spot in spots">
                <td>{{ spot.item }}</td>

                    <!-- This needs to be 3dp -->
                <td>{{ spot.price | currency:spot.currency }}</td>
            </tr>
        </tbody>
    </table>
</div>
1个回答

0

这可能已经不相关了,因为原帖是两年前的了,但Angular的“currency”过滤器将符号和小数位数作为参数输入。

例如:

<td>{{ spot.price | currency:spot.currency : 3 }}</td>

可以输出您想要的内容,而不用根据货币更改分隔符的警告。 您可以创建自己的货币过滤器,首先使用Angular过滤器,然后检查条件并更改分隔符。 这个plunker演示了这一点。

.filter('myCurrency', ['$filter', function($filter){
      return function(input, symbol, fractionSize){
          input = $filter('currency')(input, symbol, fractionSize);
          if(symbol === '\u20AC'){
            var tempString = "###";
            input = input.replace(",", tempString).replace(".", ",").replace(tempString, ".");
          }
          return input;
      }
    }])

你可能已经为自己的需求解决了这个问题,但是将答案放在这里以防其他遇到类似问题并发现这篇文章的人。


这里提供的解决方案不够完善,因为它只会将第一个 "," 替换为 ".";因此对于大于1000000的数字,它将无法正确工作。更好的解决方案是:input = input.split(",").join(tempString).replace(".", ",").split(tempString).join("."); - nplatis

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接