-
-
Save jeffjohnson9046/9470800 to your computer and use it in GitHub Desktop.
// 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) { | |
return $filter('number')(input * 100, decimals) + '%'; | |
}; | |
}]); | |
// Usage: | |
<tr ng-repeat="i in items"> | |
<td>{{i.statistic | percentage:2}}</td> | |
</tr> |
Thanks for this.
Awesome! How do you apply this to an input tag though? I only need to put a '%' suffix in the input but read the numeric value in the model?
Thank you!
Works like charm...
Thanks!!!
i need to add decimal point with dot (.) or comma (,) separator.how could anyone find a way to solve the problem.
Useful, thanks!
Awesome, thanks!
Thanks
@LahiruDhananjaya, you only have to use i18n and l10n.
nice.
Adding a check for the decimal and default it to 2 is a better addition. In most cases, percentages are represented in 2 decimals.
myApp.filter('percentage', ['$filter', function ($filter) { return function (input, decimals) { if (typeof decimals === "undefined") { decimals = 2; } return $filter('number')(input * 100, decimals) + '%'; }; }]);
This solution taught me what i need to know about pipes AKA filters. Jeez!!
thanks!