|
3 | 3 | The light version of [decimal.js](https://github.com/MikeMcl/decimal.js/), an arbitrary-precision Decimal type for JavaScript. |
4 | 4 |
|
5 | 5 | <br /> |
6 | | -[](https://travis-ci.org/MikeMcl/decimal.js-light) |
| 6 | +[](https://travis-ci.org/MikeMcl/decimal.js-light) |
7 | 7 | <br /> |
8 | 8 |
|
9 | | -The API is more or less a subset of the API of *decimal.js*. |
| 9 | +This library is the newest of the family of libraries: [bignumber.js](https://github.com/MikeMcl/bignumber.js/), [big.js](https://github.com/MikeMcl/big.js/), [decimal.js](https://github.com/MikeMcl/decimal.js/) and *decimal.js-light*.<br> |
| 10 | +The API is more or less a subset of the API of *decimal.js*. |
10 | 11 |
|
11 | 12 |  |
12 | 13 |
|
13 | | -Size of *decimal.js-light* minified: 12.7 KB. |
14 | | -Size of *decimal.js* minified: 32.1 KB. |
| 14 | +__Differences between this library and *decimal.js*__ |
15 | 15 |
|
16 | | -Other differences are that this library does not include `NaN`, `Infinity` or `-0` as legitimate values, or work with values in other bases. |
| 16 | +Size of *decimal.js* minified: 32.1 KB.<br> |
| 17 | +Size of *decimal.js-light* minified: 12.7 KB. |
17 | 18 |
|
18 | | -Also, here, the `Decimal.round` property is just the default rounding mode for `toDecimalPlaces`, `toExponential`, `toFixed`, `toPrecision` and `toSignificantDigits`. It does not apply to arithmetic operations, which are simply truncated at the required precision. |
| 19 | +This library does not include `NaN`, `Infinity` or `-0` as legitimate values, or work with values in other bases. |
19 | 20 |
|
20 | | -Another difference is that here the `naturalExponential`, `naturalLogarithm`, `logarithm`, and `toPower` methods have by default a limited precision of around 100 digits. This limit can be increased by changing the value of `LN10` (the natural logarithm of ten) in the source code, or at runtime as shown below. |
| 21 | +Here, the `Decimal.round` property is just the default rounding mode for `toDecimalPlaces`, `toExponential`, `toFixed`, `toPrecision` and `toSignificantDigits`. It does not apply to arithmetic operations, which are simply truncated at the required precision. |
| 22 | + |
| 23 | +If rounding is required just apply it explicitly, for example |
| 24 | + |
| 25 | +```js |
| 26 | +x = new Decimal(2); |
| 27 | +y = new Decimal(3); |
| 28 | + |
| 29 | +// decimal.js |
| 30 | +x.dividedBy(y).toString(); // '0.66666666666666666667' |
| 31 | + |
| 32 | +// decimal.js-light |
| 33 | +x.dividedBy(y).toString(); // '0.66666666666666666666' |
| 34 | +x.dividedBy(y).toDecimalPlaces(19).toString(); // '0.6666666666666666667' |
| 35 | +``` |
| 36 | + |
| 37 | +The `naturalExponential`, `naturalLogarithm`, `logarithm`, and `toPower` methods in this library have by default a limited precision of around 100 digits. This limit can be increased at runtime using the `LN10` (the natural logarithm of ten) configuration object property. |
| 38 | + |
| 39 | +For example, if a maximum precision of 400 digits is required for these operations use |
21 | 40 |
|
22 | 41 | ```js |
23 | 42 | // 415 digits |
24 | | -Decimal.set({ |
| 43 | +Decimal.set({ |
25 | 44 | LN10: '2.302585092994045684017991454684364207601101488628772976033327900967572609677352480235997205089598298341967784042286248633409525465082806756666287369098781689482907208325554680843799894826233198528393505308965377732628846163366222287698219886746543667474404243274365155048934314939391479619404400222105101714174800368808401264708068556774321622835522011480466371565912137345074785694768346361679210180644507064800027' |
26 | 45 | }); |
27 | 46 | ``` |
28 | 47 |
|
| 48 | +In this library the `e` property of a Decimal is the base 10000000 exponent, not the base 10 exponent as in *decimal.js*.<br> |
| 49 | +Use the `exponent` method to get the base 10 exponent. |
| 50 | + |
29 | 51 | ## Quickstart |
30 | 52 |
|
| 53 | +Browser: |
| 54 | + |
| 55 | +```html |
| 56 | +<script src='path/to/decimal.js-light'></script> |
| 57 | +``` |
| 58 | + |
| 59 | +Node package manager: |
| 60 | + |
| 61 | +```shell |
| 62 | +$ npm install --save decimal.js-light |
| 63 | +``` |
| 64 | + |
31 | 65 | ```js |
32 | | -// Adjust the global configuration if required. |
| 66 | +// Node.js |
| 67 | +var Decimal = require('decimal.js-light'); |
| 68 | + |
| 69 | +// Adjust the global configuration if required (these are the defaults) |
33 | 70 | Decimal.set({ |
34 | 71 | precision: 20, |
35 | 72 | rounding: Decimal.ROUND_HALF_UP, |
36 | 73 | toExpNeg: -7, |
37 | 74 | toExpPos: 21 |
38 | 75 | }); |
39 | 76 |
|
40 | | -x = new Decimal(2); |
41 | | -y = new Decimal(3); |
42 | | - |
43 | | -// decimal.js |
44 | | -x.dividedBy(y).toString(); // '0.66666666666666666667' |
45 | | - |
46 | | -// decimal.js-light |
47 | | -x.dividedBy(y).toString(); // '0.66666666666666666666' |
48 | | - |
49 | | -x.dividedBy(y).toDecimalPlaces(19).toString(); // '0.6666666666666666667' |
50 | | - |
51 | 77 | phi = new Decimal('1.61803398874989484820458683436563811772030917980576'); |
52 | 78 |
|
53 | 79 | phi.toFixed(10); // '1.6180339887' |
|
0 commit comments