wtaggin
wtaggin

Reputation: 33

Javascript - Subtotal not adding cents

I have a script that is taking a product price and changing the line total onChange of the QTY input. Everything seems to work fine per line item, but the Subtotal and Total are not taking cents into account. Below is the function I have...

function updateOrder(id){
   var price = $("#qty_"+id).val()*$("#price_"+id).val();
$("#total_"+id).val(price.toFixed(2));

var inputs = document.getElementsByClassName('totals'),
    result = document.getElementById('order_subtotal'),
    tresult = document.getElementById('order_total'),
    odiscount = document.getElementById('order_discount'),
    sum = 0;            

for(var i=0; i<inputs.length; i++) {
    var ip = inputs[i];

    if (ip.name && ip.name.indexOf("order_subtotal") < 0) {
        sum += parseInt(ip.value) || 0;
    }

}
result.value = sum.toFixed(2);
tresult.value = (result.value - odiscount.value).toFixed(2);    


}

Any help to point me in the right direction would be greatly appreciated.

Upvotes: 0

Views: 194

Answers (1)

chopper
chopper

Reputation: 6709

You need to use parseFloat() instead of parseInt().

Upvotes: 5

Related Questions