Skip to content

Instantly share code, notes, and snippets.

@jeffjohnson9046
Last active September 4, 2020 23:25
Show Gist options
  • Save jeffjohnson9046/9470800 to your computer and use it in GitHub Desktop.
Save jeffjohnson9046/9470800 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) {
return $filter('number')(input * 100, decimals) + '%';
};
}]);
// Usage:
<tr ng-repeat="i in items">
<td>{{i.statistic | percentage:2}}</td>
</tr>
Copy link

ghost commented Jun 25, 2015

Works great!

@punkrockpolly
Copy link

👍 🚀

@e-stylzz
Copy link

e-stylzz commented Oct 5, 2015

Thanks!

@gpkarma
Copy link

gpkarma commented Dec 10, 2015

If I wanted to put this inline as in:

cellFilter: $filter(''number')(input * 100, decimals) + '%';

... if I put it inline, what would I put instead of 'input'? I am getting an error this way, & trying to inject the custom filter is not working for me right now for some reason.

@maniart
Copy link

maniart commented Dec 10, 2015

thanks for sharing this. 👍

@witnes
Copy link

witnes commented Jan 25, 2016

thanks for working example

@SakthiSureshAnand
Copy link

@jeffjohnson9046 In my project I have the same code what you have posted above , I confused on the
return function (input, decimals) { return $filter('number')(input * 100, decimals) + '%'; };
My question , where is the data comes from input , for my case in my jade like this
td(style='text-align: right', ng-show='view.showHistory') span(ng-bind='x.recentScore | percentage:0') trend-arrow(b='x.pastScore', a='x.recentScore')

I dont get any Clue where they assigned input value ? Please guide me.

@moshfeu
Copy link

moshfeu commented Mar 17, 2016

Thanks!

@jensenkd
Copy link

thanks!

@christrotter89
Copy link

Thanks for this.

@thorne51
Copy link

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?

@ffradegrada
Copy link

Thank you!

@ArpanTanna
Copy link

Works like charm...
Thanks!!!

@LahiruDhananjaya
Copy link

i need to add decimal point with dot (.) or comma (,) separator.how could anyone find a way to solve the problem.

@jamespsterling
Copy link

Useful, thanks!

@felixfrtz
Copy link

Awesome, thanks!

@ibs4lif
Copy link

ibs4lif commented Dec 14, 2017

Thanks

Copy link

ghost commented Jul 13, 2018

@LahiruDhananjaya, you only have to use i18n and l10n.

@parjun
Copy link

parjun commented Feb 21, 2019

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) + '%'; }; }]);

@richie50
Copy link

richie50 commented Sep 4, 2020

This solution taught me what i need to know about pipes AKA filters. Jeez!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment