Answer the question
In order to leave comments, you need to log in
Where does the incomprehensible fractional part come from in calculations?
Hello! There is a code:
itemsAmount = 0;
$(".to-cart").click(function(){
var itemCoast = $(this).children('.inv-tr-item').children('.inv-item-price').text(); // Вытягиваем из дива содержимое (напр. "Цена 100,23)
itemCoast = itemCoast.replace(/[^-0-9,]/gim,''); // обрезаем "цена", оставляем только цифры (100,23)
itemCoast = itemCoast.replace(/\,/g, '.'); // Меняем запятую на точку (100.23)
itemCoast = parseFloat(itemCoast); // Парсим из строки во флоат (100.23)
if ($(this).hasClass("on-cart")) { // проверяем, есть ли товар уже в корзине через .hasClass
itemsAmount = itemsAmount - itemCoast; // из общей суммы корзины вычитаем стоимость товара
}else{
itemsAmount = itemsAmount + itemCoast; // к общей сумме корзины прибавляем стоимость товара
}
$('.cart-total span').html(itemsAmount); // Выводим стоимость в спан
});
Answer the question
In order to leave comments, you need to log in
Math in JavaScript
In general, forget about these ways of calculating the state:
Store the state model and recalculate the sum over it whenever it changes. Round numbers only after calculations.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question