Angular calculate percentage in the html
You can use the toFixed
method of Number
.
((myValue/totalValue)*100).toFixed(2)
I often use the built-in 'number' filter for that purpose;
<span>{{myPercentage | number}}</span>
For 2 decimals:
<span>{{myPercentage | number:2}}</span>
For 0 decimal;
<span>{{myPercentage | number:0}}</span>
You could use a filter, like this one below by jeffjohnson9046
The filter makes the assumption that the input will be in decimal form (i.e. 17% is 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>