js浮点数精准计算问题

在工作中遇到这种浮点数计算精度问题,例如

1
2
>  0.1+0.2
<· 0.30000000000000004

解决方案

  1. 将数字转成整数,但是这种方法对大数支持的依然不好

    1
    2
    3
    4
    5
    6
    function addFloatNum(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;
    }
  2. 第三方库 Math.js , bignumber.js,big.js

  3. 取巧方法 ~

    1
    parseFloat((0.1+0.2).toFixed(2)); //js浮点计算bug,取两位小数精度

加减乘除计算封装函数

替换js数学计算中产生的错误,比如:0.09999999 + 0.00000001

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
add(arg1, arg2) {
let s1 = arg1.toString();
let s2 = arg2.toString();
let arg1Arr = s1.split(".");
let arg2Arr = s2.split(".");
let d1 = arg1Arr.length == 2 ? arg1Arr[1] : "";
let d2 = arg2Arr.length == 2 ? arg2Arr[1] : "";
let maxLen = Math.max(d1.length, d2.length);
let m = Math.pow(10, maxLen);
return Number(((s1 * m + s2 * m) / m).toFixed(maxLen));
},
sub(arg1, arg2) {
return this.add(arg1, -arg2);
},
mul(arg1, arg2) {
let m = 0;
let s1 = arg1.toString();
let s2 = arg2.toString();
try { m += s1.split(".")[1].length } catch (e) {}
try { m += s2.split(".")[1].length } catch (e) {}
return Number(s1.replace(".", "")) * Number(s2.replace(".", "")) / Math.pow(10, m);
},
div(arg1, arg2) {
let t1 = 0;
let t2 = 0;
try { t1 = arg1.toString().split(".")[1].length } catch (e) {}
try { t2 = arg2.toString().split(".")[1].length } catch (e) {}
let r1 = Number(arg1.toString().replace(".", ""));
let r2 = Number(arg2.toString().replace(".", ""));
return this.mul((r1 / r2) , Math.pow(10, t2 - t1));
}