javascript - JQuery not showing correct decimal output -


my problem is, fine 2 decimal places when preceded 1-9, if number 10th, script show 1 decimal place.

e.g 200.19, 5000.42, 12.98 << fine if output should 123.10 display 123.1

here's code:

jquery(document).ready(function() {   function replacenumberwithcommas(yournumber) {     //seperates components of number     var n= yournumber.tostring().split(".");     //comma-fies first part     n[0] = n[0].replace(/\b(?=(\d{3})+(?!\d))/g, ",");     //combines 2 sections     return n.join(".");   }    function loancalc() {     //get parameter form     var rate=jquery('#start_rate').val();     var pricevalue=jquery('#input_1_2').val();     console.log("price value before"+pricevalue);      if(pricevalue.indexof('£') == 0) {       var strend=jquery('#input_1_2').val().indexof(',') - 1;       pricevalue=parseint(pricevalue.substr(1,pricevalue.strend))*1000;     }else{       pricevalue=pricevalue;     }      var princ = parseint(pricevalue);     var term  = parseint(jquery('#input_1_3').val());      var intr   = rate / 1200;     console.log("price"+pricevalue+" term"+term+" rate"+rate);     var calculation = princ * intr / (1 - (math.pow(1/(1 + intr), term)));      console.log("paymenet"+calculation);      var repayment= calculation*term;     var costofcredit=princ-repayment;     //set value      jquery('#figure').html('£'+ replacenumberwithcommas(calculation.tofixed(2)));     jquery('#rate').html(rate+"%");      jquery('#total').html('£' +          replacenumberwithcommas(repayment.tofixed(2)));        jquery('#credit').html('£' + replacenumberwithcommas(         math.abs(costofcredit.tofixed(2    )))); 

as can guess, there 3 fields displaying, calculation, percentage, 'repayment' , 'costofcredit'. weird thing 'repayment' works fine , has no problem showing 2nd decimal place if 10th/ends zero.

so question can guys me find problem getting 2nd decimal place show is?

when call math.abs(), it's converting string 2 digits after decimal number, loses trailing zeroes. should call math.abs() first, call tofixed() on add trailing zeroes.

jquery('#credit').html('£' +     replacenumberwithcommas(math.abs(costofcredit).tofixed(2))); 

Comments

Popular posts from this blog

java - pagination of xlsx file to XSSFworkbook using apache POI -

Unlimited choices in BASH case statement -

apache - How do I stop my index.php being run twice for every user -