calc.js
1.16 KB
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
import { Decimal } from './lib/decimal.min'
export default {
formatNumber(value) {
return value ? Number(value) : 0
},
// 加
add(x, y) {
if (!x) {
x = 0
}
if (!y) {
y = 0
}
const xx = new Decimal(this.formatNumber(x))
const yy = new Decimal(this.formatNumber(y))
return xx.plus(yy).toNumber()
},
// 减
reduce(x, y) {
if (!x) {
x = 0
}
if (!y) {
y = 0
}
const a = this.formatNumber(x)
const b = this.formatNumber(y)
const xx = new Decimal(a)
const yy = new Decimal(b)
const value = xx.minus(yy).toNumber()
if (a < b && value > 0) {
return -value
}
return value
},
// 乘
ride(x, y) {
if (!x) {
x = 0
}
if (!y) {
y = 0
}
const xx = new Decimal(this.formatNumber(x))
const yy = new Decimal(this.formatNumber(y))
return xx.mul(yy).toNumber()
},
// 除
except(x, y) {
if (!x) {
x = 0
return 0
}
if (!y) {
y = 0
return 0
}
const xx = new Decimal(this.formatNumber(x))
const yy = new Decimal(this.formatNumber(y))
return xx.div(yy).toNumber().toFixed(4)
}
}