Skip to content

Instantly share code, notes, and snippets.

@MakarovCode
Created November 12, 2020 14:47
Show Gist options
  • Save MakarovCode/8856d8db0fed8610dd1bb310aa70a735 to your computer and use it in GitHub Desktop.
Save MakarovCode/8856d8db0fed8610dd1bb310aa70a735 to your computer and use it in GitHub Desktop.
AngularJS pipe filter for handling decimals up to a max
var module = angular.module('app', [])
//Just add this after you declare your main module
module.filter('decimalmax', ['$filter', function ($filter) {
return function(num, params) {
if (num == null) return 0;
var v = (num + "").split(".");
var pres = 0
if (v.length > 1){
var p = v[1].length;
if (p <= params){
//return p;
pres = p;
}
else{
//return 3;
pres = params;
}
}
else{
//return 0
pres = 0
}
return $filter("number")(num, pres)
};
}]);
//Usage.
{{number_with_long_decimals | decimalmax:4}} // 1234.456789 shows 1234.4567
{{number_with_long_decimals | decimalmax:2}} // 1234.456789 shows 1234.45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment