0
const getCartTotal = () => {
    let totalAmount = 0;
    for (const item in cartItems) {
      if (cartItems[item] > 0) {
        let itemInfo = PRODUCT.find((product) => product.id === Number(item));
        totalAmount += Math.floor(cartItems[item] * itemInfo.price);
      }
    }

    return totalAmount;
  };

I am making a ecommerce store for the artwork that I make and when this function runs on a product that has .99 in its price tag sometimes I get a really whacky number. example when this function is run on a an item worth 19.99 I get- 99.94999999999999

Why is this happening? And if I can just round to the hundredths place this should fix the issue, is there a method for this?

I tried doing Math.floor() but that rounds to the number to be a whole number which I dont want. I could try tweaking the function but its not that complicated since its just basic math, maybe my variables are being called in a funky way.

5 items worth 19.99 gets me a value of 99.94999999999999 7 items worth 19.99 gets me a value of 139.92999999999998

is my math wrong?

Visnu
  • 1
  • 1

0 Answers0