Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

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