我试图在 HTML 中显示百分比值,如下所示:
<td> {{ ((myvalue/totalvalue)*100) }}%</td>
它可以工作,但有时它会给出一个很长的小数,这看起来很奇怪。如何四舍五入到小数点后两位?有更好的方法吗?
您可以使用过滤器,如下所示杰夫约翰逊9046 https://gist.github.com/jeffjohnson9046/9470800
过滤器假设输入为小数形式(即 17% 为 0.17)。
myApp.filter('percentage', ['$filter', function ($filter) {
return function (input, decimals) {
return $filter('number')(input * 100, decimals) + '%';
};
}]);
Usage:
<tr ng-repeat="i in items">
<td>{{i.statistic | percentage:2}}</td>
</tr>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)