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

1
  • toFixed() returns a string, so change those lines and just use toFixed() right before outputting. Commented Nov 21, 2014 at 22:56

2 Answers 2

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?

Sign up to request clarification or add additional context in comments.

Comments

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.

1 Comment

If that was the actual issue, the code would not even be executed. It also doesn't solve the OP's problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.