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?
If I use .toFixed(2) the total is wrong.
