@@ -19,6 +19,7 @@ An arbitrary-precision Decimal type for JavaScript.
1919 - No dependencies
2020 - Wide platform compatibility: uses JavaScript 1.5 (ECMAScript 3) features only
2121 - Comprehensive [ documentation] ( https://mikemcl.github.io/decimal.js/ ) and test set
22+ - Used under the hood by [ math.js] ( https://github.com/josdejong/mathjs )
2223 - Includes a TypeScript declaration file: * decimal.d.ts*
2324
2425![ API] ( https://raw.githubusercontent.com/MikeMcl/decimal.js/gh-pages/API.png )
@@ -43,12 +44,10 @@ Browser:
4344
4445``` html
4546<script src =' path/to/decimal.js' ></script >
46- ```
47- or
48- ``` html
47+
4948<script type =" module" >
50- import Decimal from ' ./path/to/decimal.mjs' ;
51- ...
49+ import Decimal from ' ./path/to/decimal.mjs' ;
50+ ...
5251 </script >
5352```
5453
@@ -58,25 +57,19 @@ import Decimal from './path/to/decimal.mjs';
5857npm install decimal.js
5958```
6059``` js
61- var Decimal = require (' decimal.js' );
62- ```
63- or
64- ``` js
60+ const Decimal = require (' decimal.js' );
61+
6562import Decimal from ' decimal.js' ;
66- ```
67- or
68- ``` js
63+
6964import {Decimal } from ' decimal.js' ;
7065```
7166
7267## Use
7368
74- * In all examples below, ` var ` , semicolons and ` toString ` calls are not shown.
69+ * In all examples below, semicolons and ` toString ` calls are not shown.
7570If a commented-out value is in quotes it means ` toString ` has been called on the preceding expression.*
7671
77- The library exports a single function object, ` Decimal ` , the constructor of Decimal instances.
78-
79- It accepts a value of type number, string or Decimal.
72+ The library exports a single constructor function, ` Decimal ` , which expects a single argument that is a number, string or Decimal instance.
8073
8174``` js
8275x = new Decimal (123.4567 )
@@ -85,7 +78,29 @@ z = new Decimal(x)
8578x .equals (y) && y .equals (z) && x .equals (z) // true
8679```
8780
88- A value can also be in binary, hexadecimal or octal if the appropriate prefix is included.
81+ If 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.
85+ new Decimal (1.0000000000000001 ) // '1'
86+ new Decimal (88259496234518.57 ) // '88259496234518.56'
87+ new Decimal (99999999999999999999 ) // '100000000000000000000'
88+
89+ // Precision loss from using numeric literals outside the range of Number values.
90+ new Decimal (2e+308 ) // 'Infinity'
91+ new Decimal (1e-324 ) // '0'
92+
93+ // Precision loss from the unexpected result of arithmetic with Number values.
94+ new Decimal (0.7 + 0.1 ) // '0.7999999999999999'
95+ ```
96+
97+ As with JavaScript numbers, strings can contain underscores as separators to improve readability.
98+
99+ ``` js
100+ x = new Decimal (' 2_147_483_647' )
101+ ```
102+
103+ String values in binary, hexadecimal or octal notation are also accepted if the appropriate prefix is included.
89104
90105``` js
91106x = new Decimal (' 0xff.f' ) // '255.9375'
@@ -94,15 +109,13 @@ z = x.plus(y) // '427.9375'
94109
95110z .toBinary () // '0b110101011.1111'
96111z .toBinary (13 ) // '0b1.101010111111p+8'
97- ```
98-
99- Using binary exponential notation to create a Decimal with the value of ` Number.MAX_VALUE ` :
100112
101- ``` js
113+ // Using binary exponential notation to create a Decimal with the value of `Number.MAX_VALUE`.
102114x = new Decimal (' 0b1.1111111111111111111111111111111111111111111111111111p+1023' )
115+ // '1.7976931348623157081e+308'
103116```
104117
105- A Decimal is immutable in the sense that it is not changed by its methods.
118+ Decimal instances are immutable in the sense that they are not changed by their methods.
106119
107120``` js
1081210.3 - 0.1 // 0.19999999999999998
@@ -115,41 +128,42 @@ The methods that return a Decimal can be chained.
115128
116129``` js
117130x .dividedBy (y).plus (z).times (9 ).floor ()
118- x .times (' 1.23456780123456789e+9' ).plus (9876.5432321 ).dividedBy (' 4_444_562_598 .111772' ).ceil ()
131+ x .times (' 1.23456780123456789e+9' ).plus (9876.5432321 ).dividedBy (' 4444562598 .111772' ).ceil ()
119132```
120133
121134Many method names have a shorter alias.
122135
123136``` js
124- x .squareRoot ().dividedBy (y).toPower (3 ).equals (x .sqrt ().div (y).pow (3 )) // true
125- x .cmp (y .mod (z).neg ()) == 1 && x . comparedTo (y .modulo (z).negated ()) == 1 // true
137+ x .squareRoot ().dividedBy (y).toPower (3 ).equals (x .sqrt ().div (y).pow (3 )) // true
138+ x .comparedTo (y .modulo (z).negated () === x . cmp (y .mod (z).neg ()) // true
126139` ` `
127140
128- Like JavaScript's Number type, there are ` toExponential ` , ` toFixed ` and ` toPrecision ` methods,
141+ Most of the methods of JavaScript's ` Number . prototype ` and ` Math ` objects are replicated.
129142
130143` ` ` js
131144x = new Decimal (255.5 )
132- x .toExponential (5 ) // '2.55500e+2'
133- x .toFixed (5 ) // '255.50000'
134- x .toPrecision (5 ) // '255.50'
135- ```
145+ x .toExponential (5 ) // '2.55500e+2'
146+ x .toFixed (5 ) // '255.50000'
147+ x .toPrecision (5 ) // '255.50'
136148
137- and almost all of the methods of JavaScript's Math object are also replicated.
138-
139- ``` js
140149Decimal .sqrt (' 6.98372465832e+9823' ) // '8.3568682281821340204e+4911'
141150Decimal .pow (2 , 0.0979843 ) // '1.0702770511687781839'
151+
152+ // Using `toFixed()` to avoid exponential notation:
153+ x = new Decimal (' 0.0000001' )
154+ x .toString () // '1e-7'
155+ x .toFixed () // ''0.0000001'
142156` ` `
143157
144- There are ` isNaN ` and ` isFinite ` methods, as ` NaN ` and ` Infinity ` are valid ` Decimal ` values,
158+ And there are ` isNaN` and ` isFinite` methods, as ` NaN ` and ` Infinity ` are valid ` Decimal` values.
145159
146160` ` ` js
147161x = new Decimal (NaN ) // 'NaN'
148162y = new Decimal (Infinity ) // 'Infinity'
149163x .isNaN () && ! y .isNaN () && ! x .isFinite () && ! y .isFinite () // true
150164` ` `
151165
152- and a ` toFraction ` method with an optional * maximum denominator* argument
166+ There is also a ` toFraction` method with an optional *maximum denominator* argument.
153167
154168` ` ` js
155169z = new Decimal (355 )
@@ -169,16 +183,16 @@ configuration which applies to all Decimal numbers created from it.
169183Decimal .set ({ precision: 5 , rounding: 4 })
170184
171185// Create another Decimal constructor, optionally passing in a configuration object
172- Decimal9 = Decimal .clone ({ precision: 9 , rounding: 1 })
186+ Dec = Decimal .clone ({ precision: 9 , rounding: 1 })
173187
174188x = new Decimal (5 )
175- y = new Decimal9 (5 )
189+ y = new Dec (5 )
176190
177191x .div (3 ) // '1.6667'
178192y .div (3 ) // '1.66666666'
179193` ` `
180194
181- The value of a Decimal is stored in a floating point format in terms of its digits, exponent and sign.
195+ The 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 .
182196
183197` ` ` js
184198x = new Decimal (- 12345.67 );
@@ -191,41 +205,32 @@ For further information see the [API](http://mikemcl.github.io/decimal.js/) refe
191205
192206## Test
193207
194- The library can be tested using Node.js or a browser.
195-
196- The * test* directory contains the file * test.js* which runs all the tests when executed by Node,
197- and the file * test.html* which runs all the tests when opened in a browser.
198-
199- To run all the tests, from a command-line at the root directory using npm
208+ To run the tests using Node.js from the root directory:
200209
201210` ` ` bash
202211npm test
203212` ` `
204213
205- or at the * test* directory using Node
214+ Each separate test module can also be executed individually, for example:
206215
207216` ` ` bash
208- node test
217+ node test/ modules / toFraction
209218` ` `
210219
211- Each separate test module can also be executed individually, for example, at the * test/modules* directory
212-
213- ``` bash
214- node toFraction
215- ```
220+ To run the tests in a browser, open *test/test.html*.
216221
217222## Minify
218223
219- The minified version of * decimal.js* and its associated source map found in this repository was created with
220- [ uglify-js] ( https://github.com/mishoo/UglifyJS ) using
224+ Two minification examples:
225+
226+ Using [uglify-js](https://github.com/mishoo/UglifyJS) to minify the *decimal.js* file:
221227
222228` ` ` bash
223229npm install uglify- js - g
224- uglifyjs decimal.js --source-map url=decimal.min.js.map --compress --mangle --output decimal.min.js
230+ uglifyjs decimal .js -- source- map url= decimal .min .js .map - c - m - o decimal .min .js
225231` ` `
226232
227- The minified version of * decimal.mjs* and its associated source map found in this repository was created with
228- [ terser] ( https://github.com/terser/terser ) using
233+ Using [terser](https://github.com/terser/terser) to minify the ES module version, *decimal.mjs*:
229234
230235` ` ` bash
231236npm install terser - g
@@ -238,6 +243,4 @@ import Decimal from './decimal.min.mjs';
238243
239244## Licence
240245
241- MIT.
242-
243- See * LICENCE.md*
246+ [The MIT Licence (Expat).](LICENCE.md)
0 commit comments