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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
|
//加
function add(a, b) {
var c, d, e;
try {
c = a.toString().split(".")[1].length;
} catch (f) {
c = 0;
}
try {
d = b.toString().split(".")[1].length;
} catch (f) {
d = 0;
}
return (e = Math.pow(10, Math.max(c, d))), (mul(a, e) + mul(b, e)) / e;
}
// 减
function sub(a, b) {
var c, d, e;
try {
c = a.toString().split(".")[1].length;
} catch (f) {
c = 0;
}
try {
d = b.toString().split(".")[1].length;
} catch (f) {
d = 0;
}
return (e = Math.pow(10, Math.max(c, d))), (mul(a, e) - mul(b, e)) / e;
}
//乘
function mul(a, b) {
var c = 0,
d = a.toString(),
e = b.toString();
try {
c += d.split(".")[1].length;
} catch (f) {}
try {
c += e.split(".")[1].length;
} catch (f) {}
return (
(Number(d.replace(".", "")) * Number(e.replace(".", ""))) / Math.pow(10, c)
);
}
//除
function div(a, b) {
var c,
d,
e = 0,
f = 0;
try {
e = a.toString().split(".")[1].length;
} catch (g) {}
try {
f = b.toString().split(".")[1].length;
} catch (g) {}
return (
(c = Number(a.toString().replace(".", ""))),
(d = Number(b.toString().replace(".", ""))),
mul(c / d, Math.pow(10, f - e))
);
}
//四舍五入
function round(num, ratio) {
var base = Math.pow(10, ratio);
return div(Math.round(mul(num, base)), base);
}
|