小数相加减 精度问题
Posted MR崔
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了小数相加减 精度问题相关的知识,希望对你有一定的参考价值。
发现问题:
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; }
以上是关于小数相加减 精度问题的主要内容,如果未能解决你的问题,请参考以下文章
给Number类型增加加法减乘除函数,解决float相加结果精度错乱的问题