Я работаю на сайте покупок и пытаюсь подсчитать промежуточную сумму продуктов.
Я получил свою цену из массива, а количество - из массива ответов getJSON. Два из них умножаются
приходит к моему промежуточному итогу. Я могу изменить количество, и это будет другой промежуточный итог.
Однако, когда я изменяю количество на определенное число, итоговый промежуточный итог выглядит как
259.99999999994 или длинное десятичное число. Я использую console.log для проверки $ price и $ qty. Они оба в правильном формате, например 299,99 и 6 штук. Я понятия не имею, что произойдет. Буду признателен, если кто-нибудь сможет мне помочь.
Вот мой код Jquery.
$(".price").each(function(index, price){
$price=$(this);
//get the product id and the price shown on the page
var id=$price.closest('tr').attr('id');
var indiPrice=$($price).html();
//take off $
indiPrice=indiPrice.substring(1)
//make sure it is number format
var aindiPrice=Number(indiPrice);
//push into the array
productIdPrice[id]=(aindiPrice);
var url = update.php
$.getJSON(
url,
{productId:tableId, //tableId is from the other jquery code which refers to
qty:qty}, productId
function(responseProduct){
$.each(responseProduct, function(productIndex, Qty){
//loop the return data
if(productIdPrice[productIndex]){
//get the price from the previous array we create X Qty
newSub=productIdPrice[productIndex]*Number(Qty);
//productIdPrice[productIndex] are the price like 199.99 or 99.99
// Qty are Quantity like 9 or 10 or 3
sum+=newSub;
newSub.toFixed(2); //try to solve the problem with toFixed but
didn't work
console.log("id: "+productIdPrice[productIndex])
console.log("Qty: "+Qty);
console.log(newSub); **//newSub sometime become XXXX.96999999994**
};
Еще раз спасибо!