小程序为什么计算 917.52 - 1000= -82.48000000000002?
其他的计算都没有问题,就这一个有问题,
如图所示,图中为console.log显示的结果
数据是从数据库中读取的,源代码如下:
var that = this ; wx.cloud.callFunction({ name: 'getMoney' , data: { objThisId: that.data.thisId, }, complete: function (res) { try { var objNub = res.result.data.length //获取数据量 if (objNub > 0) { //获取数据 for (let i = 0; i < objNub; i++) { if (res.result.data[i].moneyBool == "true" ) { objlbMoney = objlbMoney + parseFloat(res.result.data[i].money); objlbIncome = objlbIncome + parseFloat(res.result.data[i].money); console.log( "原余额:" + objlbMoney) console.log( "原收入:" + objlbIncome) console.log( "收入:" +parseFloat(res.result.data[i].money)) console.log( "---------------" ) } else if (res.result.data[i].moneyBool == "false" ) { objlbMoney = objlbMoney - parseFloat(res.result.data[i].money); objlbExpend = objlbExpend - parseFloat(res.result.data[i].money); console.log( "原余额:" + objlbMoney) console.log( "原支出:" + objlbExpend) console.log( "支出:" +parseFloat(res.result.data[i].money)) console.log( "---------------" ) } } } //查询错误 else if (objNub < 0) { console.log( '查询结果:' + objNub) console.log( '系统错误!' ) wx.switchTab({ url: '../index/home' , }) } that.setData({ lbMoney: objlbMoney, lbIncome: objlbIncome, lbExpend: objlbExpend, }) } catch (e) { console.log( '调用云函数错误!' ) wx.switchTab({ url: '../index/home' , }) } } }) |
accSub: function(arg1, arg2) {
var r1, r2, m, n, rtn;
try {
r1 = arg1.toString().split(".")[1].length;
} catch (e) {
r1 = 0;
}
try {
r2 = arg2.toString().split(".")[1].length;
} catch (e) {
r2 = 0;
}
m = Math.pow(10, Math.max(r1, r2)); //last modify by deeka //动态控制精度长度
n = (r1 >= r2) ? r1 : r2;
rtn = ((arg1 * m - arg2 * m) / m);
return rtn.toString();
}
(917.52*100 - 1000*100)/100
js都这样,小数加减都先乘以10的最大小数位数次方,运算后再除回来
了解下精度问题。
经典问题了,建议百度了解下
你在console里试试 0.1+0.2呢