运算问题(加法)(小数后三位)300.022+400=700.0219999999999。

小程序 文章 2020-08-25 12:41 0 27 全屏看文

运算问题(加法)(小数后三位)300.022+400=700.0219999999999。Operation problem (addition) (three decimal places) 300.022+400=700.0219999999999.

运算问题(加法)(小数后三位)300.022+400=700.0219999999999。
// pages/shopping/shopping.jsPage({  /**   * 页面的初始数据   */  data: {    products: [{      title:'普洱茶礼盒装1',      size:'规格:500g 礼盒装',      rmb:300.022,      nub:1    },{      title:'普洱茶礼盒装2',      size:'规格:400g 礼盒装',      rmb:400.00,      nub:1    }],    total: 0      },  /**   * 生命周期函数--监听页面加载   */  onLoad: function (options) {    this.total();  },  total:function(e){    var total = 0;    for(let i in this.data.products){      total+= (this.data.products[i].rmb*this.data.products[i].nub);      console.log(total);    };    this.setData({      products: this.data.products,      total:total    });  }})

// pages/shopping/ shopping.jsPage ({/ * * * page initial data * / data: {products: [{Title: 'Pu'er tea gift box 1', size: 'specification: 500g gift box', rmb:300.022 , nub:1 }, {Title: 'Pu'er tea gift box 2', size: 'specification: 400g gift box', rmb:400.00 , nub:1 }], total: 0 }, /*** lifecycle function -- listen to page load * / onload: function (options){ this.total (); }, total:function (e){ var total = 0; for(let i in this.data.products ){ total+= ( this.data.products [i].rmb* this.data.products [i].nub); console.log (total); }; this.setData ({ products: this.data.products , total:total }); }})

回答:

搞文艺的猿:

电脑二进制计算,精度不够,建议保留两位小数,

Distance:

js精度问题 了解一下

@空之音:

-EOF-