Decimal
描述
返回 decimal.js-light 库,暴露出的 Decimal 对象。
具体使用文档请看 https://github.com/MikeMcl/decimal.js-light (opens new window)
# 1.示例
import { Decimal } from 'sf-utils2'
// 下面是从官网找出的示例
x = new Decimal(2)
y = new Decimal(3)
// x 乘以 y
x.mul(y).toFixed()
// x 除以 y
x.div(y).toFixed()
// x 相加 y
x.add(y).toFixed()
// x 相减 y
x.sub(y).toFixed()
// decimal.js-light
x.dividedBy(y).toString() // '0.66666666666666666666'
x.dividedBy(y).toDecimalPlaces(19).toString() // '0.6666666666666666667'
// Adjust the global configuration if required (these are the defaults)
Decimal.set({
precision: 20,
rounding: Decimal.ROUND_HALF_UP,
toExpNeg: -7,
toExpPos: 21
})
phi = new Decimal('1.61803398874989484820458683436563811772030917980576')
phi.toFixed(10) // '1.6180339887'
phi.times(2).minus(1).toPower(2).plus('1e-19').equals(5) // true
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
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
上次更新: 2023/06/24, 19:35:48