Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular calculate percentage in the html

Angular noob here! I am trying to display a percentage value in my html as follows:

<td> {{ ((myvalue/totalvalue)*100) }}%</td>

It works but sometimes it gives a very long decimal which looks weird. How do i round it off to 2 digits after the decimal? Is there a better approach to this?

like image 207
Abhishek Avatar asked May 01 '15 14:05

Abhishek


People also ask

How does angular calculate discount?

The discount is the original price minus the sale price, so your equation should be ((product. price - product. newprice)/product. price)*100 .

How do you calculate percentage of website area?

Divide the smaller portion's area by the whole piece's area. For example, 40 / 160 = . 25. Therefore, the area of the smaller portion is 25 percent of the area of the original piece.


1 Answers

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>
like image 192
spik3s Avatar answered Sep 20 '22 09:09

spik3s