0

adding subtotal+vat+cst+round to grand total

function update_totals()
    {
    var grand_total = 0;
    var grand_subtotal = 0;
        var vat=0;
        var cst=0;
       var rounds =  parseInt($('.round').val());


    var vat_percent =  parseInt($('.vat-percent').val());
    if ( (vat_percent < 0) || (vat_percent > 90) ) { vat_percent=10; $('.vat-percent').find('input').val(10); }

    var cst_percent = parseInt($('.cst-percent').val());
    if ( (cst_percent < 0) || (cst_percent > 90) ) { cst_percent=10; $('.cst-percent').find('input').val(10); }

        $('#invoice-items .cell').children('.subtotal-cell').each(function(){

        grand_subtotal += parseFloat( $(this).find('input').val() );
    });



        vat=(grand_subtotal * (vat_percent/100)).toFixed(2);
        cst=(grand_subtotal * (cst_percent/100)).toFixed(2);
       grand_total = (grand_subtotal + vat + cst rounds);
    grand_subtotal = grand_subtotal.toFixed(2);

        grand_total =(grand_subtotal) + (vat);
    //update dom
    $('.sub-total').val(grand_subtotal);
    $('.vat').val(vat);
        $('.cst').val(cst);
        $('.grand-total').val(grand_total);

    }

look for error in picture two values and adding side by side adding to grand_total =grand_subtotal + vat + cst + round it shows 25.001.25 in this 25.00 is subtotal 1.25 is vat

thanks

guest
  • 6,450
  • 30
  • 44
sundar
  • 3
  • 3
  • `toFixed()` returns a string, so change those lines and just use `toFixed()` right before outputting. – Sirko Nov 21 '14 at 22:56

2 Answers2

1

Number.toFixed() returns a string.

As a side note: Given that 0.1 * 0.2 in JavaScript comes out at 0.020000000000000004 do you really want to be doing financial calculations using floating point arithmetic?

How to deal with floating point number precision in JavaScript?

Community
  • 1
  • 1
spender
  • 117,338
  • 33
  • 229
  • 351
0

Not sure here but are you missing +.

grand_total = (grand_subtotal + vat + cst rounds);

I mean it should be

grand_total = (grand_subtotal + vat + cst + rounds);

Just a small input.

Weafs.py
  • 22,731
  • 9
  • 56
  • 78