How to display 1K as 1000, 1M as million and 1B as billion in ANGULAR JS

I have a scenario during coding in my angular js project where i need to display the currency in short form like suppose if currency is $12000 so it should be display like $12K , 3200000 => 3M (million), 7000000000 => 7B (billion) etc

To do this i just created a simple filter currencyFormat for your angular js application :

Code :


.filter("currentFormat",function(){
    return function(number, decPlaces){
  decPlaces = decPlaces || 0;
  decPlaces = Math.pow(10,decPlaces);
  var abbrev = [ "K", "M", "B", "T" ];
  for (var i=abbrev.length-1; i>=0; i--) {
   var size = Math.pow(10,(i+1)*3);
   if(size <= number) {
     number = Math.round(number*(decPlaces/size))/decPlaces;
     if((number == 1000) && (i < abbrev.length - 1)) {
      number = 1;
      i++;
     }
     number += abbrev[i];
     break;
   }
  }
  return number;
    }
})


Now you just need to call this filter in you html code like below:


<p style="font-size:25px;font-weight:bold">&pound;{{totallendings | currentFormat}} <span>{{'LENT' | translate}}</span></p>

Output :





Please don't forgot to leave comment if you like this post.

Chears 
Happy Coding:)

Post a Comment

Previous Post Next Post