• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# decimal.js
2
3API官方文档:[https://mikemcl.github.io/decimal.js/](https://mikemcl.github.io/decimal.js/)
4版本:10.4.3
5
6decimal.js是JavaScript的一个高精度数学库,它具有以下特性
7
8- 可以进行128位的高精度计算和数据表示
9
10- 简单且功能齐全的API
11
12- 复用了许多JavaScript的 `Number.prototype` 和 `Math` 对象的方法
13
14- 可以转换为二进制、八进制和十六进制值
15
16- 比Java的BigDecimal的JavaScript版本更快、更小,也更容易使用
17
18- 广泛的平台兼容性:仅使用JavaScript 1.5(ECMAScript 3)特性
19
20- 添加了三角函数,非整数幂等数学计算方法
21
22
23
24OpenHarmony上引入decimal.js主要用于提供高精度浮点运算能力。
25
26## 主要目录结构
27
28```
29doc/             #文档
30test/            #测试代码
31decimal.mjs      #decimal源码
32LICENSE          #版权补充说明
33README.md        #软件说明
34```
35
36## OpenHarmony如何集成decimal.js
37
38decimal.js被引入在OpenHarmony的third_party目录下,通过OpenHarmony中部件依赖的方式进行编译。
39
401. 主干代码下载
41
42   ```
43   repo init -u https://gitee.com/openharmony/manifest.git -b master --no-repo-verify
44   repo sync -c
45   repo forall -c 'git lfs pull'
46   ```
47
482. 在需要使用该库的模块中添加依赖
49
50   ```
51   deps = [ "//third_party/decimal.js:decimal" ]
52   ```
53
543. 预处理
55
56   ```
57   ./build/prebuilts_download.sh
58   ```
59
604. 编译
61
62   ```
63   ./build.sh --product-name rk3568 --ccache
64   ```
65
66   编译生成的文件对应路径:`out/rk3568/thirdparty/decimal.js/libdecimal.z.so`。
67
685. 运行
69
70   ```ts
71   // Decimal功能需要在ArkTs中使用
72   // Index.ets
73   import { Decimal } from '@kit.ArkTS';
74   @Entry
75   @Component
76   struct Index {
77     build() {
78       Row() {
79         Column() {
80           Text("Test")
81             .fontSize(50)
82             .fontWeight(FontWeight.Bold)
83             .onClick(() => {
84               let a0 : Decimal = new Decimal(1.2345678912345678)  // 可以使用Decimal表示数值
85               console.log("test Decimal :" + a0.toString());      // 可以通过toString获取Decimal表示的数值
86                                                                   // '1.2345678912345678'
87               Decimal.set({ precision : 10 })                     // 可以通过Decimal.set设置精度等"global"配置
88               let a1 : Decimal = a0.add(0.5)                      // 进行加法操作
89               console.log("test Decimal set:" + a1.toString());   // 当前全局精度为10,结果为'1.734567891'
90
91               Decimal.set({ defaults : true })                    // 设置回默认值配置
92               let dCos = Decimal.cos(0.5)                         // 可以使用Decimal中的三角函数等数学方法输出高精度浮点数
93               console.log("test Decimal cos:" + dCos.toString()); // '0.87758256189037271612'
94               console.log("test Math cos:" + Math.cos(0.5));      // '0.8775825618903728'
95
96               let a2 = Decimal.add(0.1, 0.2)                      // 此外, Decimal可以解决一些低精度计算导致的bug
97               console.log("test Decimal add:" + a2.toString());   // '0.3'
98               console.log("test Decimal add:" + (0.1 + 0.2));     // '0.30000000000000004'
99             })
100         }
101         .width('100%')
102       }
103       .height('100%')
104     }
105   }
106
107   ```
108
109
110## 许可证
111
112本项目遵从[LICENSE](https://gitee.com/openharmony-sig/third_party_decimal.js/blob/master/LICENCE.md)中所描述的许可证。