Stanislas Piotrowski
Stanislas Piotrowski

Reputation: 2694

My VAT calculation is wrong

I'm trying to do a function for making a small calcul with vat.

So I have the following code:

<script type="text/javascript">
    function calcdebours()
    {
        var taux         = document.getElementById('debours_taux_tva').value;
        var ht_no_tva     = document.getElementById('debours_montant_ht_no_tva').value;
        var ht_tva        = document.getElementById('debours_montant_ht_tva').value;
        var tva= Math.round((((ht_tva)*(taux))/100)*100)/100;
        ;
        if(taux=='')
        {
            taux=0;
        }
        if(ht_no_tva=='')
        {
            ht_no_tva=0;
        }
        if(ht_tva=='')
        {
            ht_tva=0;
        }
        document.getElementById('debours_montant_tva').value = tva  ;
        document.getElementById('debours_montant_ttc').value = (tva) + parseInt(ht_tva)+ parseInt(ht_no_tva)


    }

    </script>

And below the fiddle:

http://jsfiddle.net/6zzRZ/

But for all it make the wrong calculation, I think it does not count the cent.

I've tried using just var 1 + var 2 but it just used to concatenate the number sor I use the parseInt function.

It has worked but the result is wrong for any kind of amount.

The trouble is that I tried parseDecimal but it say that this function does not exist.

Any kind of help will be much appreciated.

Upvotes: 0

Views: 816

Answers (2)

Matt Burland
Matt Burland

Reputation: 45135

Try parseFloat. Or use the unitary + like this: var ht_no_tva = +document.getElementById('debours_montant_ht_no_tva').value; This gives JavaScript the hint that it should treat the value as a number.

Upvotes: 1

Exter
Exter

Reputation: 136

Use parseFloat() instead of parseDouble: http://www.w3schools.com/jsref/jsref_parsefloat.asp

Upvotes: 1

Related Questions