angularjs - how to manage rounding in angular filter? -


i developing angular filter rounds of 6 decimals. cant figure out how round 2 decimals when decimals contain zeros. 1.00000 convert 1.00. filter looks like:

app.filter('customcurrency',function ($filter) {     return function (amount, currencysymbol,fractionsize) {         var currency = $filter('currency');          if (amount < 0) {             return currency(amount, currencysymbol).replace('(', '-').replace(')', '');         }          debugger;         if (fractionsize !== undefined) {             amount = currency(amount, currencysymbol, fractionsize);         } else {             amount = currency(amount, currencysymbol)         }           debugger;         var amounts = amount.split(".");         var amounthtml ;           if (amounts[1].length==2 && amounts[1][0]==0 && amounts[1][1]==0)          {              amounthtml = amounts[0] + '<span class="decimals">.00</span>';          }         else          {              amounthtml= amounts[0] + '<span class="decimals">.' + amounts[1] + '</span>';           }         return amounthtml;     }; }); 

plunkr:http://plnkr.co/edit/cgmycsqwn3huanoex4g4?p=preview

not sure work out in long run i'm sure there better way go this, if want quick , dirty fix try this:

demo

app.filter('customcurrency',function ($filter) {      return function (amount, currencysymbol, fractionsize) {          var currency = $filter('currency');          if (amount < 0) {             return currency(amount, currencysymbol).replace('(', '-').replace(')', '');         }          debugger;         if (fractionsize !== undefined) {             amount = currency(amount, currencysymbol, fractionsize);         } else {             amount = currency(amount, currencysymbol)         }           debugger;          var amounts         = amount.split("."),             fractionalpart  = amounts[1],             decimals        = fractionalpart.length,             zerosfromright  = countzerosfromright(fractionalpart);           if(zerosfromright > 2){           return amounts[0] + '<span class="decimals">.00</span>';         }          return amounts[0] + '<span class="decimals">.' + amounts[1] + '</span>';         ///////////////////////////////////          function countzerosfromright(str){            var len   = str.length,               count = 0;            while(len--){              if(str[len] === '0'){               count++;               continue;             }              break;           }            return count         }       }; }); 

edit had rethink , think way better:

i've added test , maybe don't need functionality think bit more robust.

demo2

it('should remove zeros last non-zero number', function() {     expect($filter('customcurrency')(1.004000, '$', 6)).toequal('$1<span class="decimals">.004</span>');     expect($filter('customcurrency')(1.4066000, '$', 6)).toequal('$1<span class="decimals">.4066</span>');   }); 

app.js

var app = angular.module('plunker', ['ngroute']);  'use strict';  var app = angular.module('plunker');  app.filter('customcurrency',function ($filter) {      return function (amount, currencysymbol, fractionsize) {          var currency = $filter('currency');          if (amount < 0) {             return currency(amount, currencysymbol).replace('(', '-').replace(')', '');         }          debugger;          var rounded         = round(amount, fractionsize),             currencystring  = currency(rounded, currencysymbol, fractionsize),             amounts         = currencystring.split("."),              integerpart     = amounts[0],             fractionalpart  = amounts[1] || false,             indexlastnonzero = indexlastnonzero(fractionalpart);          // if zeros after decimal remove 2         if(indexlastnonzero === -1){           return integerpart + '<span class="decimals">.00</span>';         }          // if zeros , other numbers after decimal remove trailing zeros         if(indexlastnonzero > 1){           return integerpart + '<span class="decimals">.' + fractionalpart.slice(0, indexlastnonzero + 1) + '</span>';         }          return integerpart;         /////////////////////////////////////////////          function round(str, decimals){           var num = +str;            return num.tofixed(decimals);         }          function indexlastnonzero(str){           var len   = str.length;            while(len--){              if(str[len] !== '0'){               return len;             }           }            return -1;         }      }; }); 

Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

jquery - javascript onscroll fade same class but with different div -