Domanda

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?

È stato utile?

Soluzione

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

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top