How to calculate tax percentage in jQuery?
<p>This is my first time asking a question here, so I hope I'll do it correctly.</p>

<p>Just to give context, I'm developing a small website where lawyers can buy a digital version of some legal documents, but I'm having trouble calculating tax on the total cost. </p>

<p>The taxes are calculated with two rates used in the province of Quebec, Canada: GST (5%) and QST (9.975%). Both rates are calculated using the subtotal amount. Here's what I tried so far:</p>

<pre class="lang-js prettyprint-override"><code>$("#formchoix #checkall").click(function () {
        var tps = 0.05; //5%
        var tvq = 0.09975; //9.975%
        var subtotal = 0;
        var total = 0;
        if ($("#formchoix #checkall").is(':checked')) {
            $("#formchoix input[type=checkbox].checkchoix").each(function () {
                //check all forms
                $(this).prop("checked", true);
                $(".checknom").prop("checked", true);
                $(".checkid").prop("checked", true);

                subtotal += 15; //each form is 15$

                $('#subtotal').html(subtotal + '.00$'); //subtotal shown to user

                var taxes = subtotal * tps * tvq;
                total = subtotal + taxes;
                $('#totalcost').html(total.toFixed(2) + '$'); //total shown to user
                $('#inputTotal').val(total.toFixed(2)); //value to be sent to server
            });

        } else {
            $("#formchoix input[type=checkbox]").each(function () {
                //reset everything: checkboxes, value and total shown to user
                $(this).prop("checked", false);
                $(".checknom").prop("checked", false);
                $(".checkid").prop("checked", false);

                subtotal = 0;
                total = 0;
                $('#subtotal').html('0.00$');
                $('#totalcost').html('0.00$');
                $('#inputTotal').val('0.00');
            });
        }
    });
</code></pre>

<p>The above code doesn't give me the right number though. For example, if my subtotal is 30$, it will show 30.15$ instead of 30.49$ like it's supposed to (based on a tax calculator online). </p>

<p>I've also tried using values like 1.05 and 1.09975 and multiplicating them to the subtotal directly using <code>total = subtotal * tps * tvq</code>, but the total gave me 30.64$ (if the subtotal is 30$ like my previous example)</p>

<p>Clearly what I did is wrong, so how can I make sure the total is right? </p>