小数相加减 精度问题

发现问题:

 

 getCurrentMoney (money){
            let money1 = money * 100;
            let intMoney1 = parseInt(money1);
            let diff = money1 - intMoney1;
            let exit=String(diff).indexOf('e')
            let decDiff = parseInt(diff * 10);
            console.log(decDiff)
            if (decDiff > 5&&exit=='-1') {
                intMoney1 += 1;
            }
            return intMoney1/100;
        },

changeCartNum () {
                this.shopCart.num=0;
                this.shopCart.totalMoney=0;
                for(var i=0; i<this.shopCart.items.length;i++){
                    this.shopCart.num+=this.shopCart.items[i].sku.num;
                    this.shopCart.totalMoney+=this.shopCart.items[i].sku.num*this.shopCart.items[i].sku.price;
                }
                if(this.shopCart.num==0){
                    this.showCart= !this.showCart;
                }
                this.shopCart.totalMoney= this.getCurrentMoney(this.shopCart.totalMoney);
            },
            getCurrentMoney (money) {
                let money1 = money * 10000;
                let intMoney1 = parseInt(money1);
                let diff = money1 - intMoney1;
                let decDiff = parseInt(diff * 10);
                if (decDiff > 5) {
                    intMoney1 += 1;
                }

                return intMoney1/10000;
            }
原文地址:https://www.cnblogs.com/MR-cui/p/8617423.html