解决js中小数相加损失精度的问题
作者:
温木先生 | 来源:发表于
2021-11-18 19:27 被阅读0次function addFloatNumber() {
const args = arguments;
const len = args.length;
let initFloatLen = 0;
let sum = 0;
let strItem = '';
let tmpLen = 0;
for (key in args) {
strItem = args[key].toString();
if (strItem.indexOf('.') != -1) {
tmpLen = strItem.split('.')[1].length;
initFloatLen = initFloatLen < tmpLen ? tmpLen : initFloatLen;
}
} // for
const m = Math.pow(10, initFloatLen);
for (key in args) {
sum += args[key] * m;
}
return sum / m;
}
本文标题:解决js中小数相加损失精度的问题
本文链接:https://www.haomeiwen.com/subject/skljtrtx.html
网友评论