---js // 浮点数相乘 解决0.1 + 0.2 = 0.3的问题
function floatNum(n1, n2) { const n1Len = String(n1).split('.')[1].length; const n2Len = String(n2).split('.')[1].length;
//科学计算法
const maxLen = Math.max(n1Len, n2Len);
//加法
const comNum = Math.pow(10, maxLen);
return (n1 * comNum + n2 * comNum) / comNum;
}
floatNum(0.1 , 0.2); console.log(floatNum(0.1 , 0.2)); console.log(Number(0.1 + 0.2));