js运算丢精度问题

js运算丢精度问题解决方法

解决方法:使用toFixed(2) 四舍五入来处理

有问题的计算
console.log((4565.15100)) // 456514.99999999994
console.log((4565.15
100).toFixed(2)) // 456515.00
console.log((4565.19100)) // 456518.99999999994
console.log((4565.19
100).toFixed(2)) // 456519.00
console.log((0.1 + 0.2)) // 0.30000000000000004
console.log((0.1 + 0.2).toFixed(2)) // 0.30
console.log(0.11+0.10, (0.11+0.10).toFixed(2), ‘------1’);
console.log(0.11+0.12, (0.11+0.12).toFixed(2), ‘------2’);
console.log(0.12+0.17, (0.12+0.17).toFixed(2), ‘------3’);
console.log(0.13+0.16, (0.13+0.16).toFixed(2), ‘------4’);
console.log(0.13+0.17, (0.13+0.17).toFixed(2), ‘------5’);
console.log(0.14+0.15, (0.14+0.15).toFixed(2), ‘------6’);
console.log(0.14+0.16, (0.14+0.16).toFixed(2), ‘------7’);
console.log(0.14+0.17, (0.14+0.17).toFixed(2), ‘------8’);
console.log(0.14+0.18, (0.14+0.18).toFixed(2), ‘------9’);
console.log(0.15+0.17, (0.15+0.17).toFixed(2), ‘------10’);
console.log(0.15+0.18, (0.15+0.18).toFixed(2), ‘------11’);
console.log(0.15+0.19, (0.15+0.19).toFixed(2), ‘------12’);
console.log(0.16+0.18, (0.16+0.18).toFixed(2), ‘------13’);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值