0

Every seven for so incriments, my toFixed function returns a lot of decimals. It's tied to round so I thought I was safe.

The product starts at 49.97 and winds up at $349.78999999999996 for 7 units. For 14 units: $699.5799999999999, 29 units: $1449.1299999999999. How can I fix this?

updateCart: function() {
                // if key exists
            if (storage.getItem('domehaOrder') != undefined) {
                //jQuery('.quantity').css('display', 'inline-block')
                //jQuery('.top-cart-item-image').css('display', 'block')

                //Get local storage order
                var order = storage.getItem('domehaOrder')

                //get local storage products


                // Parse existing order & products// local storage Must be string
                order = JSON.parse(order);
                console.log(order)

                //Get product data from spree backend
                storeobj = JSON.parse(storage.getItem('domehaProducts'));
                console.log("Store: " + JSON.stringify(storeobj))




            // Merge products and cart data
            var domecart = order.items.map(function(item) {
            merge = storeobj.products.filter(function(prod) {
                    return prod.id == item.productID;
                })[0];
                return Object.assign(item, { images: merge.image, name: merge.name, price: merge.display_price, intPrice: merge.price});
            });

            console.log("cart "+ JSON.stringify(domecart))
            //Grab Total
            var total = domecart.map(function(a) {return a.intPrice * a.quantity}).reduce((a, b) => a + b, 0);
            total = parseFloat(Math.round(total * 100) / 100).toFixed(2);



            var quantity = domecart.map(function(a) {return a.quantity}).reduce((a, b) => a + b, 0);
            console.log(quantity)
            // Refactor for KO

            domecart = domecart.map(function(e) {
                return {
                    price: "$" + e.intPrice * e.quantity,
                    images: e.images,
                    name: e.name,
                    quantity: e.quantity,
                    id: e.productID
                }
            });

            console.log("domecart" + domecart)
            newcart = {
                items: domecart, 
                total: "$" + total,
                quantity: "x " + quantity,
            }
            console.log("final cart" + JSON.stringify(newcart))
            //// Bind to KO

            this.cart = newcart;

            //Strinify and save for later
            //domecart = JSON.stringify(newcart)
            //storage.setItem('domehaCart', domecart)

            }
        }, 
QueSo
  • 269
  • 1
  • 3
  • 11
  • 1
    I notice you aren't using `.toFixed(2)` on the `domecart = ...` bit. Is this where you're seeing the issue? That aside, you should always work in pennies and only divide by 100 to show the value to the user at the very end. – Niet the Dark Absol May 26 '17 at 18:37
  • If you copy this into an answer I will accept it. Thank you. – QueSo May 26 '17 at 19:17

0 Answers0