每7个左右固定(2)失败并返回大量小数

时间:2017-05-26 18:34:44

标签: javascript

对于这样的实施,每七个,我的toFixed函数返回很多小数。它与圆形相关,所以我认为我很安全。

该产品的起价为49.97,最高价为349.78999999999996  7个单位。 14个单位:699.5799999999999,29单位:1449.1299999999999。我怎样才能解决这个问题?

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)

            }
        }, 

0 个答案:

没有答案