js 0.1+0.2 结果不是0.3,而是0.30000000000000004 处理方法
js 0.1+0.2 结果不是0.3,而是0.30000000000000004 处理方法
转换为整数,得到结果后再转为小数
function plus(a, b){ // code let mode = 10 let count = 1 let tmp = Math.min(a, b) while(!Number.isInteger(tmp)){ tmp *= mode count += 1 } let res = parseFloat((a*(count*mode)+b*(count*mode))/(count*mode)) return res } console.log(plus(0.1, 0.2))
toFixed错误的场景
console.log(50250/50000, (50250/50000).toFixed(2))