js运算BUG
2011-06-23 09:35 迷路中的路人甲 阅读(356) 评论(0) 编辑 收藏 举报 1 /*JS浮点计算BUG屏蔽终极解决办法*/
2
3 function accDiv(arg1, arg2) {
4
5 var t1 = 0, t2 = 0, r1, r2;
6
7 try { t1 = arg1.toString().split(".")[1].length } catch (e) { }
8
9 try { t2 = arg2.toString().split(".")[1].length } catch (e) { }
10
11 with (Math) {
12
13 r1 = Number(arg1.toString().replace(".", ""))
14
15 r2 = Number(arg2.toString().replace(".", ""))
16
17 return (r1 / r2) * pow(10, t2 - t1);
18
19 }
20
21 }
22
23
24
25 //给Number类型增加一个div方法,调用起来更加方便。
26
27 Number.prototype.div = function(arg) {
28
29 return accDiv(this, arg);
30
31 }
32
33
34
35
36 function accMul(arg1, arg2) {
37
38 var m = 0, s1 = arg1.toString(), s2 = arg2.toString();
39
40 try { m += s1.split(".")[1].length } catch (e) { }
41
42 try { m += s2.split(".")[1].length } catch (e) { }
43
44 return Number(s1.replace(".", "")) * Number(s2.replace(".", "")) / Math.pow(10, m)
45
46 }
47
48 Number.prototype.mul = function(arg) {
49
50 return accMul(arg, this);
51
52 }
53
54 function accAdd(arg1, arg2) {
55
56 var r1, r2, m, c;
57
58 try { r1 = arg1.toString().split(".")[1].length } catch (e) { r1 = 0 }
59
60 try { r2 = arg2.toString().split(".")[1].length } catch (e) { r2 = 0 }
61
62 c = Math.abs(r1 - r2);
63 m = Math.pow(10, Math.max(r1, r2))
64 if (c > 0) {
65 var cm = Math.pow(10, c);
66 if (r1 > r2) {
67 arg1 = Number(arg1.toString().replace(".", ""));
68 arg2 = Number(arg2.toString().replace(".", "")) * cm;
69 }
70 else {
71 arg1 = Number(arg1.toString().replace(".", "")) * cm;
72 arg2 = Number(arg2.toString().replace(".", ""));
73 }
74 }
75 else {
76 arg1 = Number(arg1.toString().replace(".", ""));
77 arg2 = Number(arg2.toString().replace(".", ""));
78 }
79 return (arg1 + arg2) / m
80
81 }
82
83 Number.prototype.add = function(arg) {
84
85 return accAdd(arg, this);
86
87 }
2
3 function accDiv(arg1, arg2) {
4
5 var t1 = 0, t2 = 0, r1, r2;
6
7 try { t1 = arg1.toString().split(".")[1].length } catch (e) { }
8
9 try { t2 = arg2.toString().split(".")[1].length } catch (e) { }
10
11 with (Math) {
12
13 r1 = Number(arg1.toString().replace(".", ""))
14
15 r2 = Number(arg2.toString().replace(".", ""))
16
17 return (r1 / r2) * pow(10, t2 - t1);
18
19 }
20
21 }
22
23
24
25 //给Number类型增加一个div方法,调用起来更加方便。
26
27 Number.prototype.div = function(arg) {
28
29 return accDiv(this, arg);
30
31 }
32
33
34
35
36 function accMul(arg1, arg2) {
37
38 var m = 0, s1 = arg1.toString(), s2 = arg2.toString();
39
40 try { m += s1.split(".")[1].length } catch (e) { }
41
42 try { m += s2.split(".")[1].length } catch (e) { }
43
44 return Number(s1.replace(".", "")) * Number(s2.replace(".", "")) / Math.pow(10, m)
45
46 }
47
48 Number.prototype.mul = function(arg) {
49
50 return accMul(arg, this);
51
52 }
53
54 function accAdd(arg1, arg2) {
55
56 var r1, r2, m, c;
57
58 try { r1 = arg1.toString().split(".")[1].length } catch (e) { r1 = 0 }
59
60 try { r2 = arg2.toString().split(".")[1].length } catch (e) { r2 = 0 }
61
62 c = Math.abs(r1 - r2);
63 m = Math.pow(10, Math.max(r1, r2))
64 if (c > 0) {
65 var cm = Math.pow(10, c);
66 if (r1 > r2) {
67 arg1 = Number(arg1.toString().replace(".", ""));
68 arg2 = Number(arg2.toString().replace(".", "")) * cm;
69 }
70 else {
71 arg1 = Number(arg1.toString().replace(".", "")) * cm;
72 arg2 = Number(arg2.toString().replace(".", ""));
73 }
74 }
75 else {
76 arg1 = Number(arg1.toString().replace(".", ""));
77 arg2 = Number(arg2.toString().replace(".", ""));
78 }
79 return (arg1 + arg2) / m
80
81 }
82
83 Number.prototype.add = function(arg) {
84
85 return accAdd(arg, this);
86
87 }
From:http://blog.csdn.net/l3917055/archive/2010/09/04/5863215.aspx