当前位置:网站首页>JS to solve the problem of floating point multiplication precision loss

JS to solve the problem of floating point multiplication precision loss

2022-07-01 04:44:00 Advanced giant 001

for example

1.11 * 100 = 111.00000000000001

terms of settlement

export const format = function(f?, digit?) {
    
  //  solve js Loss of accuracy 
  // digit It's to keep a few decimal places 
  const m = Math.pow(10, digit);
  return Math.round(f * m) / m;
};
format(1.11 * 100)
原网站

版权声明
本文为[Advanced giant 001]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/182/202207010436375795.html