Question

I am trying to do a pretty simple multiplication using the following script:

$('.itemRow.item').each(function(){
    var qty = $(this).find('input.quantity').val();
    var price = $(this).find('.claculatedPrice').data('price');
    var total = qty * price;

    $(this).find('.claculatedPrice').text(total);
});

The code above produces the following values:

qty = 7
price = 435.59
total = 3049.1299999999997

I would expect the total to be: 3049.13 - what's going on?

Was it helpful?

Solution

Just do:

total.toFixed(2);

The above will work

JS will calculate with precision points. You can round it off by using .toFixed to 2 places

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top