0
function updateTotalBasket(){
    var basketItems = document.querySelectorAll(".basket_item");
    var quantity;
    var price;
    var totalPrice = 0.00;
    basketItems.forEach((item) => { 
        quantity = item.querySelector(".quantity");
        price = item.querySelector(".price");
        console.log("quant : " + quantity.value);
        console.log("price : " + price.innerHTML);
        totalPrice += parseFloat(quantity.value) * parseFloat(price.innerHTML);
    });
    var totalPriceElement = document.querySelector("#totalPrice");
    totalPriceElement.innerHTML = totalPrice.toFixed(2);
    console.log(totalPrice);
}

enter image description here

Total Price should be 2953.48, but 2952.00 is coming. I think there is a problem with float multiplications. how can i fix this?

  • 1
    Please don't post code as an image. Type the text. Also include the actual HTML that drives the results you get from `querySelector[All]` calls. We should be able to just run your code "out of the box" and see the results you mention at the end. – trincot Aug 12 '22 at 12:51
  • 1
    Relevant code and error messages need to be included in your question *as text*, [not as pictures of text](https://meta.stackoverflow.com/q/285551/328193). Just linking to screen shots makes it more difficult for people to help you. To learn more about this community and how we can help you, please start with the [tour] and read [ask] and its linked resources. – David Aug 12 '22 at 12:51
  • Could you add the logs in order to have the same data as you? And try to log the data allready with the parseFloat – Edoldin Aug 12 '22 at 12:54
  • I did what you wanted :) – Hasan Polat Aug 12 '22 at 12:55
  • Does this answer your question? [Is floating point math broken?](https://stackoverflow.com/questions/588004/is-floating-point-math-broken) – Liam Aug 12 '22 at 12:56
  • The reason for my question is 313.15 in the price section, so it does not see the comma when using js. stubbornly wants a point. – Hasan Polat Aug 12 '22 at 13:01

1 Answers1

0

Your decimal formatting is a problem with parseFloat - replace the "," with a "."

Bob
  • 1,589
  • 17
  • 25