浮点数在扩乘100倍时,会出现算法失精;
最简单的失精例子:
1
| 0.1 + 0.2 = 0.30000000000000004
|
有时候在处理小数时也会出现这种情况
比如:
1
| 0.007*100 = 0.7000000000000001
|
处理办法就是:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| //按照有效数字位数进行四舍五入,默认6位有效数字 Math.signFigures = function(num, rank = 6) { if(!num) return(0); const sign = num / Math.abs(num); const number = num * sign; const temp = rank - 1 - Math.floor(Math.log10(number)); let ans; if (temp > 0) { ans = parseFloat(number.toFixed(temp)); } else if (temp < 0) { const temp = Math.pow(10, temp); ans = Math.round(number / temp) * temp; } else { ans = Math.round(number); } return (ans * sign); }; // 实际运用 0.56 * 100; //56.00000000000001 Math.signFigures(0.56 * 100); //56
|
浮点数算法 https://blog.csdn.net/tercel_zhang/article/details/52537726