1.将数字转成整数
这是最容易想到的方法,也相对简单
function add(num1, num2) {
const num1Digits = (num1.toString().split('.')[1] || '').length;
const num2Digits = (num2.toString().split('.')[1] || '').length;
const baseNum = Math.pow(10, Math.max(num1Digits, num2Digits));
return (num1 * baseNum + num2 * baseNum) / baseNum;
}
标签:const,num1,num2,toString,split,baseNum,小数,精度,运算 From: https://www.cnblogs.com/ahalvxiaobu/p/17248444.html