Skip to content

Instantly share code, notes, and snippets.

@juangesino
Forked from jeffjohnson9046/percent-filter.js
Last active September 1, 2015 01:35
Show Gist options
  • Save juangesino/0c3b4b27792320870822 to your computer and use it in GitHub Desktop.
Save juangesino/0c3b4b27792320870822 to your computer and use it in GitHub Desktop.
Format percentages in AngularJS
// In app.js or main.js or whatever:
// var myApp = angular.module('askchisne', ['ngSanitize', 'ngAnimate', 'ui.bootstrap', 'ui.bootstrap.tpls']);
// This 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) {
percentage = $filter('number')(input * 100, decimals);
percentage = percentage.toString().replace(/(\.[0-9]*?)0+$/, "$1").replace(/\.$/, "");
return percentage + '%';
};
}]);
// Usage:
<tr ng-repeat="i in items">
<td>{{i.statistic | percentage:2}}</td>
</tr>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment