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.

415 lines
12 KiB

4 years ago
  1. ![bignumber.js](https://raw.githubusercontent.com/MikeMcl/bignumber.js/gh-pages/bignumberjs.png)
  2. A JavaScript library for arbitrary-precision decimal and non-decimal arithmetic.
  3. [![Build Status](https://travis-ci.org/MikeMcl/bignumber.js.svg)](https://travis-ci.org/MikeMcl/bignumber.js)
  4. <br />
  5. ## Features
  6. - Faster, smaller, and perhaps easier to use than JavaScript versions of Java's BigDecimal
  7. - 8 KB minified and gzipped
  8. - Simple API but full-featured
  9. - Works with numbers with or without fraction digits in bases from 2 to 64 inclusive
  10. - Replicates the `toExponential`, `toFixed`, `toPrecision` and `toString` methods of JavaScript's Number type
  11. - Includes a `toFraction` and a correctly-rounded `squareRoot` method
  12. - Supports cryptographically-secure pseudo-random number generation
  13. - No dependencies
  14. - Wide platform compatibility: uses JavaScript 1.5 (ECMAScript 3) features only
  15. - Comprehensive [documentation](http://mikemcl.github.io/bignumber.js/) and test set
  16. ![API](https://raw.githubusercontent.com/MikeMcl/bignumber.js/gh-pages/API.png)
  17. If a smaller and simpler library is required see [big.js](https://github.com/MikeMcl/big.js/).
  18. It's less than half the size but only works with decimal numbers and only has half the methods.
  19. It also does not allow `NaN` or `Infinity`, or have the configuration options of this library.
  20. See also [decimal.js](https://github.com/MikeMcl/decimal.js/), which among other things adds support for non-integer powers, and performs all operations to a specified number of significant digits.
  21. ## Load
  22. The library is the single JavaScript file *bignumber.js* (or minified, *bignumber.min.js*).
  23. ```html
  24. <script src='relative/path/to/bignumber.js'></script>
  25. ```
  26. For [Node.js](http://nodejs.org), the library is available from the [npm](https://npmjs.org/) registry
  27. $ npm install bignumber.js
  28. ```javascript
  29. var BigNumber = require('bignumber.js');
  30. ```
  31. To load with AMD loader libraries such as [requireJS](http://requirejs.org/):
  32. ```javascript
  33. require(['path/to/bignumber'], function(BigNumber) {
  34. // Use BigNumber here in local scope. No global BigNumber.
  35. });
  36. ```
  37. ## Use
  38. *In all examples below, `var`, semicolons and `toString` calls are not shown.
  39. If a commented-out value is in quotes it means `toString` has been called on the preceding expression.*
  40. The library exports a single function: `BigNumber`, the constructor of BigNumber instances.
  41. It accepts a value of type number *(up to 15 significant digits only)*, string or BigNumber object,
  42. ```javascript
  43. x = new BigNumber(123.4567)
  44. y = new BigNumber('123456.7e-3')
  45. z = new BigNumber(x)
  46. x.equals(y) && y.equals(z) && x.equals(z) // true
  47. ```
  48. and a base from 2 to 64 inclusive can be specified.
  49. ```javascript
  50. x = new BigNumber(1011, 2) // "11"
  51. y = new BigNumber('zz.9', 36) // "1295.25"
  52. z = x.plus(y) // "1306.25"
  53. ```
  54. A BigNumber is immutable in the sense that it is not changed by its methods.
  55. ```javascript
  56. 0.3 - 0.1 // 0.19999999999999998
  57. x = new BigNumber(0.3)
  58. x.minus(0.1) // "0.2"
  59. x // "0.3"
  60. ```
  61. The methods that return a BigNumber can be chained.
  62. ```javascript
  63. x.dividedBy(y).plus(z).times(9).floor()
  64. x.times('1.23456780123456789e+9').plus(9876.5432321).dividedBy('4444562598.111772').ceil()
  65. ```
  66. Many method names have a shorter alias.
  67. ```javascript
  68. x.squareRoot().dividedBy(y).toPower(3).equals(x.sqrt().div(y).pow(3)) // true
  69. x.cmp(y.mod(z).neg()) == 1 && x.comparedTo(y.modulo(z).negated()) == 1 // true
  70. ```
  71. Like JavaScript's number type, there are `toExponential`, `toFixed` and `toPrecision` methods
  72. ```javascript
  73. x = new BigNumber(255.5)
  74. x.toExponential(5) // "2.55500e+2"
  75. x.toFixed(5) // "255.50000"
  76. x.toPrecision(5) // "255.50"
  77. x.toNumber() // 255.5
  78. ```
  79. and a base can be specified for `toString`.
  80. ```javascript
  81. x.toString(16) // "ff.8"
  82. ```
  83. There is also a `toFormat` method which may be useful for internationalisation
  84. ```javascript
  85. y = new BigNumber('1234567.898765')
  86. y.toFormat(2) // "1,234,567.90"
  87. ```
  88. The maximum number of decimal places of the result of an operation involving division (i.e. a division, square root, base conversion or negative power operation) is set using the `config` method of the `BigNumber` constructor.
  89. The other arithmetic operations always give the exact result.
  90. ```javascript
  91. BigNumber.config({ DECIMAL_PLACES: 10, ROUNDING_MODE: 4 })
  92. x = new BigNumber(2);
  93. y = new BigNumber(3);
  94. z = x.div(y) // "0.6666666667"
  95. z.sqrt() // "0.8164965809"
  96. z.pow(-3) // "3.3749999995"
  97. z.toString(2) // "0.1010101011"
  98. z.times(z) // "0.44444444448888888889"
  99. z.times(z).round(10) // "0.4444444445"
  100. ```
  101. There is a `toFraction` method with an optional *maximum denominator* argument
  102. ```javascript
  103. y = new BigNumber(355)
  104. pi = y.dividedBy(113) // "3.1415929204"
  105. pi.toFraction() // [ "7853982301", "2500000000" ]
  106. pi.toFraction(1000) // [ "355", "113" ]
  107. ```
  108. and `isNaN` and `isFinite` methods, as `NaN` and `Infinity` are valid `BigNumber` values.
  109. ```javascript
  110. x = new BigNumber(NaN) // "NaN"
  111. y = new BigNumber(Infinity) // "Infinity"
  112. x.isNaN() && !y.isNaN() && !x.isFinite() && !y.isFinite() // true
  113. ```
  114. The value of a BigNumber is stored in a decimal floating point format in terms of a coefficient, exponent and sign.
  115. ```javascript
  116. x = new BigNumber(-123.456);
  117. x.c // [ 123, 45600000000000 ] coefficient (i.e. significand)
  118. x.e // 2 exponent
  119. x.s // -1 sign
  120. ```
  121. Multiple BigNumber constructors can be created, each with their own independent configuration which applies to all BigNumber's created from it.
  122. ```javascript
  123. // Set DECIMAL_PLACES for the original BigNumber constructor
  124. BigNumber.config({ DECIMAL_PLACES: 10 })
  125. // Create another BigNumber constructor, optionally passing in a configuration object
  126. BN = BigNumber.another({ DECIMAL_PLACES: 5 })
  127. x = new BigNumber(1)
  128. y = new BN(1)
  129. x.div(3) // '0.3333333333'
  130. y.div(3) // '0.33333'
  131. ```
  132. For futher information see the [API](http://mikemcl.github.io/bignumber.js/) reference in the *doc* directory.
  133. ## Test
  134. The *test* directory contains the test scripts for each method.
  135. The tests can be run with Node or a browser. For Node use
  136. $ npm test
  137. or
  138. $ node test/every-test
  139. To test a single method, e.g.
  140. $ node test/toFraction
  141. For the browser, see *every-test.html* and *single-test.html* in the *test/browser* directory.
  142. *bignumber-vs-number.html* enables some of the methods of bignumber.js to be compared with those of JavaScript's number type.
  143. ## Versions
  144. Version 1.x.x of this library is still supported on the 'original' branch. The advantages of later versions are that they are considerably faster for numbers with many digits and that there are some added methods (see Change Log below). The disadvantages are more lines of code and increased code complexity, and the loss of simplicity in no longer having the coefficient of a BigNumber stored in base 10.
  145. ## Performance
  146. See the [README](https://github.com/MikeMcl/bignumber.js/tree/master/perf) in the *perf* directory.
  147. ## Build
  148. For Node, if [uglify-js](https://github.com/mishoo/UglifyJS2) is installed
  149. npm install uglify-js -g
  150. then
  151. npm run build
  152. will create *bignumber.min.js*.
  153. A source map will also be created in the root directory.
  154. ## Feedback
  155. Open an issue, or email
  156. Michael
  157. <a href="mailto:M8ch88l@gmail.com">M8ch88l@gmail.com</a>
  158. ## Licence
  159. MIT.
  160. See [LICENCE](https://github.com/MikeMcl/bignumber.js/blob/master/LICENCE).
  161. ## Change Log
  162. #### 4.1.0
  163. * 26/09/2017
  164. * Remove node 0.6 from *.travis.yml*.
  165. * Add *bignumber.mjs*.
  166. #### 4.0.4
  167. * 03/09/2017
  168. * Add missing aliases to *bignumber.d.ts*.
  169. #### 4.0.3
  170. * 30/08/2017
  171. * Add types: *bignumber.d.ts*.
  172. #### 4.0.2
  173. * 03/05/2017
  174. * #120 Workaround Safari/Webkit bug.
  175. #### 4.0.1
  176. * 05/04/2017
  177. * #121 BigNumber.default to BigNumber['default'].
  178. #### 4.0.0
  179. * 09/01/2017
  180. * Replace BigNumber.isBigNumber method with isBigNumber prototype property.
  181. #### 3.1.2
  182. * 08/01/2017
  183. * Minor documentation edit.
  184. #### 3.1.1
  185. * 08/01/2017
  186. * Uncomment `isBigNumber` tests.
  187. * Ignore dot files.
  188. #### 3.1.0
  189. * 08/01/2017
  190. * Add `isBigNumber` method.
  191. #### 3.0.2
  192. * 08/01/2017
  193. * Bugfix: Possible incorrect value of `ERRORS` after a `BigNumber.another` call (due to `parseNumeric` declaration in outer scope).
  194. #### 3.0.1
  195. * 23/11/2016
  196. * Apply fix for old ipads with `%` issue, see #57 and #102.
  197. * Correct error message.
  198. #### 3.0.0
  199. * 09/11/2016
  200. * Remove `require('crypto')` - leave it to the user.
  201. * Add `BigNumber.set` as `BigNumber.config` alias.
  202. * Default `POW_PRECISION` to `0`.
  203. #### 2.4.0
  204. * 14/07/2016
  205. * #97 Add exports to support ES6 imports.
  206. #### 2.3.0
  207. * 07/03/2016
  208. * #86 Add modulus parameter to `toPower`.
  209. #### 2.2.0
  210. * 03/03/2016
  211. * #91 Permit larger JS integers.
  212. #### 2.1.4
  213. * 15/12/2015
  214. * Correct UMD.
  215. #### 2.1.3
  216. * 13/12/2015
  217. * Refactor re global object and crypto availability when bundling.
  218. #### 2.1.2
  219. * 10/12/2015
  220. * Bugfix: `window.crypto` not assigned to `crypto`.
  221. #### 2.1.1
  222. * 09/12/2015
  223. * Prevent code bundler from adding `crypto` shim.
  224. #### 2.1.0
  225. * 26/10/2015
  226. * For `valueOf` and `toJSON`, include the minus sign with negative zero.
  227. #### 2.0.8
  228. * 2/10/2015
  229. * Internal round function bugfix.
  230. #### 2.0.6
  231. * 31/03/2015
  232. * Add bower.json. Tweak division after in-depth review.
  233. #### 2.0.5
  234. * 25/03/2015
  235. * Amend README. Remove bitcoin address.
  236. #### 2.0.4
  237. * 25/03/2015
  238. * Critical bugfix #58: division.
  239. #### 2.0.3
  240. * 18/02/2015
  241. * Amend README. Add source map.
  242. #### 2.0.2
  243. * 18/02/2015
  244. * Correct links.
  245. #### 2.0.1
  246. * 18/02/2015
  247. * Add `max`, `min`, `precision`, `random`, `shift`, `toDigits` and `truncated` methods.
  248. * Add the short-forms: `add`, `mul`, `sd`, `sub` and `trunc`.
  249. * Add an `another` method to enable multiple independent constructors to be created.
  250. * Add support for the base 2, 8 and 16 prefixes `0b`, `0o` and `0x`.
  251. * Enable a rounding mode to be specified as a second parameter to `toExponential`, `toFixed`, `toFormat` and `toPrecision`.
  252. * Add a `CRYPTO` configuration property so cryptographically-secure pseudo-random number generation can be specified.
  253. * Add a `MODULO_MODE` configuration property to enable the rounding mode used by the `modulo` operation to be specified.
  254. * Add a `POW_PRECISION` configuration property to enable the number of significant digits calculated by the power operation to be limited.
  255. * Improve code quality.
  256. * Improve documentation.
  257. #### 2.0.0
  258. * 29/12/2014
  259. * Add `dividedToIntegerBy`, `isInteger` and `toFormat` methods.
  260. * Remove the following short-forms: `isF`, `isZ`, `toE`, `toF`, `toFr`, `toN`, `toP`, `toS`.
  261. * Store a BigNumber's coefficient in base 1e14, rather than base 10.
  262. * Add fast path for integers to BigNumber constructor.
  263. * Incorporate the library into the online documentation.
  264. #### 1.5.0
  265. * 13/11/2014
  266. * Add `toJSON` and `decimalPlaces` methods.
  267. #### 1.4.1
  268. * 08/06/2014
  269. * Amend README.
  270. #### 1.4.0
  271. * 08/05/2014
  272. * Add `toNumber`.
  273. #### 1.3.0
  274. * 08/11/2013
  275. * Ensure correct rounding of `sqrt` in all, rather than almost all, cases.
  276. * Maximum radix to 64.
  277. #### 1.2.1
  278. * 17/10/2013
  279. * Sign of zero when x < 0 and x + (-x) = 0.
  280. #### 1.2.0
  281. * 19/9/2013
  282. * Throw Error objects for stack.
  283. #### 1.1.1
  284. * 22/8/2013
  285. * Show original value in constructor error message.
  286. #### 1.1.0
  287. * 1/8/2013
  288. * Allow numbers with trailing radix point.
  289. #### 1.0.1
  290. * Bugfix: error messages with incorrect method name
  291. #### 1.0.0
  292. * 8/11/2012
  293. * Initial release