• Home
Name Date Size #Lines LOC

..--

doc/07-Sep-2024-2,7372,324

test/07-Sep-2024-27,59118,740

.npmignoreD07-Sep-202417 43

.travis.ymlD07-Sep-2024221 2423

BUILD.gnD07-Sep-20241.5 KiB5041

CHANGELOG.mdD07-Sep-20245.9 KiB250199

LICENCE.mdD07-Sep-20241.1 KiB2418

OAT.xmlD07-Sep-20242.5 KiB5132

README.OpenSourceD07-Sep-2024325 1211

README.mdD07-Sep-20248.1 KiB247176

README_zh.mdD07-Sep-20243.5 KiB11279

bundle.jsonD07-Sep-2024837 3535

decimal.cppD07-Sep-20241.3 KiB5133

decimal.d.tsD07-Sep-20248.4 KiB300200

decimal.global.d.tsD07-Sep-20248.9 KiB321218

decimal.jsD07-Sep-2024127.9 KiB4,9352,288

decimal.mjsD07-Sep-2024120.4 KiB4,9154,012

package.jsonD07-Sep-20241.1 KiB5655

README.OpenSource

1[
2    {
3        "Name":"decimal.js",
4        "License":"MIT",
5        "License File":"LICENSE",
6        "Version Number":"10.4.3",
7        "Owner":"gongjunsong@huawei.com",
8        "Upstream URL":"https://github.com/MikeMcl/decimal.js.git",
9        "Description":"An arbitrary-precision Decimal type for JavaScript."
10    }
11]
12

README.md

1![decimal.js](https://raw.githubusercontent.com/MikeMcl/decimal.js/gh-pages/decimaljs.png)
2
3An arbitrary-precision Decimal type for JavaScript.
4
5[![npm version](https://img.shields.io/npm/v/decimal.js.svg)](https://www.npmjs.com/package/decimal.js)
6[![npm downloads](https://img.shields.io/npm/dw/decimal.js)](https://www.npmjs.com/package/decimal.js)
7[![Build Status](https://travis-ci.org/MikeMcl/decimal.js.svg)](https://travis-ci.org/MikeMcl/decimal.js)
8[![CDNJS](https://img.shields.io/cdnjs/v/decimal.js.svg)](https://cdnjs.com/libraries/decimal.js)
9
10<br>
11
12## Features
13
14  - Integers and floats
15  - Simple but full-featured API
16  - Replicates many of the methods of JavaScript's `Number.prototype` and `Math` objects
17  - Also handles hexadecimal, binary and octal values
18  - Faster, smaller, and perhaps easier to use than JavaScript versions of Java's BigDecimal
19  - No dependencies
20  - Wide platform compatibility: uses JavaScript 1.5 (ECMAScript 3) features only
21  - Comprehensive [documentation](https://mikemcl.github.io/decimal.js/) and test set
22  - Used under the hood by [math.js](https://github.com/josdejong/mathjs)
23  - Includes a TypeScript declaration file: *decimal.d.ts*
24
25![API](https://raw.githubusercontent.com/MikeMcl/decimal.js/gh-pages/API.png)
26
27The library is similar to [bignumber.js](https://github.com/MikeMcl/bignumber.js/), but here
28precision is specified in terms of significant digits rather than decimal places, and all
29calculations are rounded to the precision (similar to Python's decimal module) rather than just
30those involving division.
31
32This library also adds the trigonometric functions, among others, and supports non-integer powers,
33which makes it a significantly larger library than *bignumber.js* and the even smaller
34[big.js](https://github.com/MikeMcl/big.js/).
35
36For a lighter version of this library without the trigonometric functions see
37[decimal.js-light](https://github.com/MikeMcl/decimal.js-light/).
38
39## Load
40
41The library is the single JavaScript file *decimal.js* or ES module *decimal.mjs*.
42
43Browser:
44
45```html
46<script src='path/to/decimal.js'></script>
47
48<script type="module">
49  import Decimal from './path/to/decimal.mjs';
50  ...
51</script>
52```
53
54[Node.js](https://nodejs.org):
55
56```bash
57npm install decimal.js
58```
59```js
60const Decimal = require('decimal.js');
61
62import Decimal from 'decimal.js';
63
64import {Decimal} from 'decimal.js';
65```
66
67## Use
68
69*In all examples below, semicolons and `toString` calls are not shown.
70If a commented-out value is in quotes it means `toString` has been called on the preceding expression.*
71
72The library exports a single constructor function, `Decimal`, which expects a single argument that is a number, string or Decimal instance.
73
74```js
75x = new Decimal(123.4567)
76y = new Decimal('123456.7e-3')
77z = new Decimal(x)
78x.equals(y) && y.equals(z) && x.equals(z)        // true
79```
80
81If using values with more than a few digits, it is recommended to pass strings rather than numbers to avoid a potential loss of precision.
82
83```js
84// Precision loss from using numeric literals with more than 15 significant digits.
85new Decimal(1.0000000000000001)         // '1'
86new Decimal(88259496234518.57)          // '88259496234518.56'
87new Decimal(99999999999999999999)       // '100000000000000000000'
88
89// Precision loss from using numeric literals outside the range of Number values.
90new Decimal(2e+308)                     // 'Infinity'
91new Decimal(1e-324)                     // '0'
92
93// Precision loss from the unexpected result of arithmetic with Number values.
94new Decimal(0.7 + 0.1)                  // '0.7999999999999999'
95```
96
97As with JavaScript numbers, strings can contain underscores as separators to improve readability.
98
99```js
100x = new Decimal('2_147_483_647')
101```
102
103String values in binary, hexadecimal or octal notation are also accepted if the appropriate prefix is included.
104
105```js
106x = new Decimal('0xff.f')            // '255.9375'
107y = new Decimal('0b10101100')        // '172'
108z = x.plus(y)                        // '427.9375'
109
110z.toBinary()                         // '0b110101011.1111'
111z.toBinary(13)                       // '0b1.101010111111p+8'
112
113// Using binary exponential notation to create a Decimal with the value of `Number.MAX_VALUE`.
114x = new Decimal('0b1.1111111111111111111111111111111111111111111111111111p+1023')
115// '1.7976931348623157081e+308'
116```
117
118Decimal instances are immutable in the sense that they are not changed by their methods.
119
120```js
1210.3 - 0.1                     // 0.19999999999999998
122x = new Decimal(0.3)
123x.minus(0.1)                  // '0.2'
124x                             // '0.3'
125```
126
127The methods that return a Decimal can be chained.
128
129```js
130x.dividedBy(y).plus(z).times(9).floor()
131x.times('1.23456780123456789e+9').plus(9876.5432321).dividedBy('4444562598.111772').ceil()
132```
133
134Many method names have a shorter alias.
135
136```js
137x.squareRoot().dividedBy(y).toPower(3).equals(x.sqrt().div(y).pow(3))     // true
138x.comparedTo(y.modulo(z).negated() === x.cmp(y.mod(z).neg())              // true
139```
140
141Most of the methods of JavaScript's `Number.prototype` and `Math` objects are replicated.
142
143```js
144x = new Decimal(255.5)
145x.toExponential(5)                       // '2.55500e+2'
146x.toFixed(5)                             // '255.50000'
147x.toPrecision(5)                         // '255.50'
148
149Decimal.sqrt('6.98372465832e+9823')      // '8.3568682281821340204e+4911'
150Decimal.pow(2, 0.0979843)                // '1.0702770511687781839'
151
152// Using `toFixed()` to avoid exponential notation:
153x = new Decimal('0.0000001')
154x.toString()                             // '1e-7'
155x.toFixed()                              // '0.0000001'
156```
157
158And there are `isNaN` and `isFinite` methods, as `NaN` and `Infinity` are valid `Decimal` values.
159
160```js
161x = new Decimal(NaN)                                           // 'NaN'
162y = new Decimal(Infinity)                                      // 'Infinity'
163x.isNaN() && !y.isNaN() && !x.isFinite() && !y.isFinite()      // true
164```
165
166There is also a `toFraction` method with an optional *maximum denominator* argument.
167
168```js
169z = new Decimal(355)
170pi = z.dividedBy(113)        // '3.1415929204'
171pi.toFraction()              // [ '7853982301', '2500000000' ]
172pi.toFraction(1000)          // [ '355', '113' ]
173```
174
175All calculations are rounded according to the number of significant digits and rounding mode specified
176by the `precision` and `rounding` properties of the Decimal constructor.
177
178For advanced usage, multiple Decimal constructors can be created, each with their own independent
179configuration which applies to all Decimal numbers created from it.
180
181```js
182// Set the precision and rounding of the default Decimal constructor
183Decimal.set({ precision: 5, rounding: 4 })
184
185// Create another Decimal constructor, optionally passing in a configuration object
186Dec = Decimal.clone({ precision: 9, rounding: 1 })
187
188x = new Decimal(5)
189y = new Dec(5)
190
191x.div(3)                           // '1.6667'
192y.div(3)                           // '1.66666666'
193```
194
195The value of a Decimal is stored in a floating point format in terms of its digits, exponent and sign, but these properties should be considered read-only.
196
197```js
198x = new Decimal(-12345.67);
199x.d                            // [ 12345, 6700000 ]    digits (base 10000000)
200x.e                            // 4                     exponent (base 10)
201x.s                            // -1                    sign
202```
203
204For further information see the [API](http://mikemcl.github.io/decimal.js/) reference in the *doc* directory.
205
206## Test
207
208To run the tests using Node.js from the root directory:
209
210```bash
211npm test
212```
213
214Each separate test module can also be executed individually, for example:
215
216```bash
217node test/modules/toFraction
218```
219
220To run the tests in a browser, open *test/test.html*.
221
222## Minify
223
224Two minification examples:
225
226Using [uglify-js](https://github.com/mishoo/UglifyJS) to minify the *decimal.js* file:
227
228```bash
229npm install uglify-js -g
230uglifyjs decimal.js --source-map url=decimal.min.js.map -c -m -o decimal.min.js
231```
232
233Using [terser](https://github.com/terser/terser) to minify the ES module version, *decimal.mjs*:
234
235```bash
236npm install terser -g
237terser decimal.mjs --source-map url=decimal.min.mjs.map -c -m --toplevel -o decimal.min.mjs
238```
239
240```js
241import Decimal from './decimal.min.mjs';
242```
243
244## Licence
245
246[The MIT Licence (Expat).](LICENCE.md)
247

README_zh.md

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)中所描述的许可证。