解决js浮点数计算精度丢失问题

解决前端浮点数之间运算精度丢失问题。

实现1

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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
// 加
Number.prototype['add'] = function (...arg) {
var r1, r2, m, result = this;
arg.forEach(value => {
try { r1 = result.toString().split(".")[1].length } catch (e) { r1 = 0 }
try { r2 = value.toString().split(".")[1].length } catch (e) { r2 = 0 }
m = Math.pow(10, Math.max(r1, r2));
result = Math.round(result * m + value * m) / m;
});
return result;
};
// 减
Number.prototype['sub'] = function (...arg) {
var r1, r2, m, result = this;
arg.forEach(value => {
try { r1 = result.toString().split(".")[1].length } catch (e) { r1 = 0 }
try { r2 = value.toString().split(".")[1].length } catch (e) { r2 = 0 }
m = Math.pow(10, Math.max(r1, r2));
var n = (r1 >= r2) ? r1 : r2;
result = (Math.round(result * m - value * m) / m).toFixed(n);
});
return result;
};
// 乘
Number.prototype['mul'] = function (...arg) {
var result = this;
arg.forEach(value => {
var m = 0, s1 = result.toString(), s2 = value.toString();
try { m += s1.split(".")[1].length } catch (e) { }
try { m += s2.split(".")[1].length } catch (e) { }
result = Number(s1.replace(".", "")) * Number(s2.replace(".", "")) / Math.pow(10, m);
});
return result;
};
// 除
Number.prototype['div'] = function (...arg) {
var result = this;
arg.forEach(value => {
var t1 = 0, t2 = 0, r1, r2;
try { t1 = result.toString().split(".")[1].length } catch (e) { }
try { t2 = value.toString().split(".")[1].length } catch (e) { }
r1 = Number(result.toString().replace(".", ""));
r2 = Number(value.toString().replace(".", ""));
result = (r1 / r2) * Math.pow(10, t2 - t1);
});
return result;
};

使用方式

1
2
3
4
5
6
7
8
9

// 0.1 + 0.2
console.log(0.1.add(0.2)); // 0.3

// 同时支持传入多个参与计算的数
var a = 5;
// a * 2 * 3
console.log(a.mul(2, 3)); // 30

实现2

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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46

const calc = {
//加法
add : function (arg1,arg2){
var r1,r2,m;
try{r1=arg1.toString().split(".")[1].length}catch(e){r1=0}
try{r2=arg2.toString().split(".")[1].length}catch(e){r2=0}
m=Math.pow(10,Math.max(r1,r2));
return (arg1*m+arg2*m)/m;
},

//减法
sub : function (arg1,arg2){
var r1,r2,m,n;
try{r1=arg1.toString().split(".")[1].length}catch(e){r1=0}
try{r2=arg2.toString().split(".")[1].length}catch(e){r2=0}
m=Math.pow(10,Math.max(r1,r2));
n=(r1>=r2)?r1:r2;
return ((arg1*m-arg2*m)/m).toFixed(n);
},

//乘法
mul : function (arg1,arg2) {
var m=0,s1=arg1.toString(),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 : function (arg1,arg2){

var t1=0,t2=0,r1,r2;

try{t1=arg1.toString().split(".")[1].length}catch(e){}
try{t2=arg2.toString().split(".")[1].length}catch(e){}

r1=Number(arg1.toString().replace(".",""));
r2=Number(arg2.toString().replace(".",""));

return calc.mul( (r1/r2), Math.pow(10,t2-t1) );

}
}


使用方式

1
2
3
4
5
6
7
8
9
10
11
12
13

// 1 + 2.33
calc.add( 1 , 2.33)

// 6 - 3.33
calc.sub( 6 , 3.33)

// 5 * 2.33
calc.mul( 5 , 2.33 )

// 7 / 4.55
calc.div( 7 , 4.55 )