Function reduce produce a lot of decimals. .toFixed(2) give me the wrong result

Viewed 17
removeOneFromCart(dish) {
    let cart = JSON.parse(sessionStorage.getItem("cart"));
    let index = cart.findIndex((item) => item.id == dish.id);
    if (index !== -1) {
        cart[index].quantity--;
        if (cart[index].quantity == 0) {
            cart.splice(index, 1);
        }
    }
    sessionStorage.setItem("cart", JSON.stringify(cart));
    this.cart = JSON.parse(sessionStorage.getItem("cart"));
    this.partialTotal = this.cart
        .reduce((acc, dish) => acc + dish.price * dish.quantity, 0)
        .toFixed(2);
    sessionStorage.setItem(
        "partialTotal",
        JSON.stringify(this.partialTotal)
    );

    this.total = this.partialTotal + this.restaurant.delivery_price;
    sessionStorage.setItem("total", JSON.stringify(this.total));
},

In this function, I've tried to delete one item from the cart and update the result. The problem is that the total is correct, but I don't know why, but sometimes returns a lot of decimals, like this (124.30000000000001). How can I fix this bug?

enter image description here

If I use .toFixed(2) the total is wrong.

0 Answers
Related