You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

244 lines
7.4 KiB

  1. ![decimal.js](https://raw.githubusercontent.com/MikeMcl/decimal.js/gh-pages/decimaljs.png)
  2. An arbitrary-precision Decimal type for JavaScript.
  3. [![npm version](https://img.shields.io/npm/v/decimal.js.svg)](https://www.npmjs.com/package/decimal.js)
  4. [![npm downloads](https://img.shields.io/npm/dw/decimal.js)](https://www.npmjs.com/package/decimal.js)
  5. [![Build Status](https://travis-ci.org/MikeMcl/decimal.js.svg)](https://travis-ci.org/MikeMcl/decimal.js)
  6. [![CDNJS](https://img.shields.io/cdnjs/v/decimal.js.svg)](https://cdnjs.com/libraries/decimal.js)
  7. <br>
  8. ## Features
  9. - Integers and floats
  10. - Simple but full-featured API
  11. - Replicates many of the methods of JavaScript's `Number.prototype` and `Math` objects
  12. - Also handles hexadecimal, binary and octal values
  13. - Faster, smaller, and perhaps easier to use than JavaScript versions of Java's BigDecimal
  14. - No dependencies
  15. - Wide platform compatibility: uses JavaScript 1.5 (ECMAScript 3) features only
  16. - Comprehensive [documentation](http://mikemcl.github.io/decimal.js/) and test set
  17. - Includes a TypeScript declaration file: *decimal.d.ts*
  18. ![API](https://raw.githubusercontent.com/MikeMcl/decimal.js/gh-pages/API.png)
  19. The library is similar to [bignumber.js](https://github.com/MikeMcl/bignumber.js/), but here
  20. precision is specified in terms of significant digits rather than decimal places, and all
  21. calculations are rounded to the precision (similar to Python's decimal module) rather than just
  22. those involving division.
  23. This library also adds the trigonometric functions, among others, and supports non-integer powers,
  24. which makes it a significantly larger library than *bignumber.js* and the even smaller
  25. [big.js](https://github.com/MikeMcl/big.js/).
  26. For a lighter version of this library without the trigonometric functions see [decimal.js-light](https://github.com/MikeMcl/decimal.js-light/).
  27. ## Load
  28. The library is the single JavaScript file *decimal.js* or ES module *decimal.mjs*.
  29. Browser:
  30. ```html
  31. <script src='path/to/decimal.js'></script>
  32. ```
  33. ```html
  34. <script type="module">
  35. import Decimal from './path/to/decimal.mjs';
  36. ...
  37. </script>
  38. ```
  39. [Node.js](http://nodejs.org):
  40. ```bash
  41. $ npm install decimal.js
  42. ```
  43. ```js
  44. var Decimal = require('decimal.js');
  45. ```
  46. ES module:
  47. ```js
  48. //import Decimal from 'decimal.js';
  49. import {Decimal} from 'decimal.js';
  50. ```
  51. AMD loader libraries such as [requireJS](http://requirejs.org/):
  52. ```js
  53. require(['decimal'], function(Decimal) {
  54. // Use Decimal here in local scope. No global Decimal.
  55. });
  56. ```
  57. ## Use
  58. *In all examples below, `var`, semicolons and `toString` calls are not shown.
  59. If a commented-out value is in quotes it means `toString` has been called on the preceding expression.*
  60. The library exports a single function object, `Decimal`, the constructor of Decimal instances.
  61. It accepts a value of type number, string or Decimal.
  62. ```js
  63. x = new Decimal(123.4567)
  64. y = new Decimal('123456.7e-3')
  65. z = new Decimal(x)
  66. x.equals(y) && y.equals(z) && x.equals(z) // true
  67. ```
  68. A value can also be in binary, hexadecimal or octal if the appropriate prefix is included.
  69. ```js
  70. x = new Decimal('0xff.f') // '255.9375'
  71. y = new Decimal('0b10101100') // '172'
  72. z = x.plus(y) // '427.9375'
  73. z.toBinary() // '0b110101011.1111'
  74. z.toBinary(13) // '0b1.101010111111p+8'
  75. ```
  76. Using binary exponential notation to create a Decimal with the value of `Number.MAX_VALUE`:
  77. ```js
  78. x = new Decimal('0b1.1111111111111111111111111111111111111111111111111111p+1023')
  79. ```
  80. A Decimal is immutable in the sense that it is not changed by its methods.
  81. ```js
  82. 0.3 - 0.1 // 0.19999999999999998
  83. x = new Decimal(0.3)
  84. x.minus(0.1) // '0.2'
  85. x // '0.3'
  86. ```
  87. The methods that return a Decimal can be chained.
  88. ```js
  89. x.dividedBy(y).plus(z).times(9).floor()
  90. x.times('1.23456780123456789e+9').plus(9876.5432321).dividedBy('4444562598.111772').ceil()
  91. ```
  92. Many method names have a shorter alias.
  93. ```js
  94. x.squareRoot().dividedBy(y).toPower(3).equals(x.sqrt().div(y).pow(3)) // true
  95. x.cmp(y.mod(z).neg()) == 1 && x.comparedTo(y.modulo(z).negated()) == 1 // true
  96. ```
  97. Like JavaScript's Number type, there are `toExponential`, `toFixed` and `toPrecision` methods,
  98. ```js
  99. x = new Decimal(255.5)
  100. x.toExponential(5) // '2.55500e+2'
  101. x.toFixed(5) // '255.50000'
  102. x.toPrecision(5) // '255.50'
  103. ```
  104. and almost all of the methods of JavaScript's Math object are also replicated.
  105. ```js
  106. Decimal.sqrt('6.98372465832e+9823') // '8.3568682281821340204e+4911'
  107. Decimal.pow(2, 0.0979843) // '1.0702770511687781839'
  108. ```
  109. There are `isNaN` and `isFinite` methods, as `NaN` and `Infinity` are valid `Decimal` values,
  110. ```js
  111. x = new Decimal(NaN) // 'NaN'
  112. y = new Decimal(Infinity) // 'Infinity'
  113. x.isNaN() && !y.isNaN() && !x.isFinite() && !y.isFinite() // true
  114. ```
  115. and a `toFraction` method with an optional *maximum denominator* argument
  116. ```js
  117. z = new Decimal(355)
  118. pi = z.dividedBy(113) // '3.1415929204'
  119. pi.toFraction() // [ '7853982301', '2500000000' ]
  120. pi.toFraction(1000) // [ '355', '113' ]
  121. ```
  122. All calculations are rounded according to the number of significant digits and rounding mode
  123. specified by the `precision` and `rounding` properties of the Decimal constructor.
  124. For advanced usage, multiple Decimal constructors can be created, each with their own independent configuration which
  125. applies to all Decimal numbers created from it.
  126. ```js
  127. // Set the precision and rounding of the default Decimal constructor
  128. Decimal.set({ precision: 5, rounding: 4 })
  129. // Create another Decimal constructor, optionally passing in a configuration object
  130. Decimal9 = Decimal.clone({ precision: 9, rounding: 1 })
  131. x = new Decimal(5)
  132. y = new Decimal9(5)
  133. x.div(3) // '1.6667'
  134. y.div(3) // '1.66666666'
  135. ```
  136. The value of a Decimal is stored in a floating point format in terms of its digits, exponent and sign.
  137. ```js
  138. x = new Decimal(-12345.67);
  139. x.d // [ 12345, 6700000 ] digits (base 10000000)
  140. x.e // 4 exponent (base 10)
  141. x.s // -1 sign
  142. ```
  143. For further information see the [API](http://mikemcl.github.io/decimal.js/) reference in the *doc* directory.
  144. ## Test
  145. The library can be tested using Node.js or a browser.
  146. The *test* directory contains the file *test.js* which runs all the tests when executed by Node,
  147. and the file *test.html* which runs all the tests when opened in a browser.
  148. To run all the tests, from a command-line at the root directory using npm
  149. ```bash
  150. $ npm test
  151. ```
  152. or at the *test* directory using Node
  153. ```bash
  154. $ node test
  155. ```
  156. Each separate test module can also be executed individually, for example, at the *test/modules* directory
  157. ```bash
  158. $ node toFraction
  159. ```
  160. ## Build
  161. For Node, if [uglify-js](https://github.com/mishoo/UglifyJS2) is installed
  162. ```bash
  163. npm install uglify-js -g
  164. ```
  165. then
  166. ```bash
  167. npm run build
  168. ```
  169. will create *decimal.min.js*, and a source map will also be added to the *doc* directory.
  170. ## Licence
  171. MIT.
  172. See *LICENCE.md*