bignumber.js/README.md

246 lines
8.7 KiB
Markdown
Raw Permalink Normal View History

2014-06-19 15:38:58 +00:00
[![Build Status](https://travis-ci.org/MikeMcl/bignumber.js.svg)](https://travis-ci.org/MikeMcl/bignumber.js)
2013-11-28 10:34:47 +00:00
2012-11-08 20:02:27 +00:00
# bignumber.js #
2013-08-01 17:57:00 +00:00
A JavaScript library for arbitrary-precision decimal and non-decimal arithmetic.
2012-11-08 20:02:27 +00:00
## Features
2013-08-01 17:57:00 +00:00
- Faster, smaller, and perhaps easier to use than JavaScript versions of Java's BigDecimal
2012-11-08 20:02:27 +00:00
- 5 KB minified and gzipped
- Simple API but full-featured
2013-11-08 12:45:37 +00:00
- Works with numbers with or without fraction digits in bases from 2 to 64 inclusive
2013-08-01 17:57:00 +00:00
- Replicates the `toExponential`, `toFixed`, `toPrecision` and `toString` methods of JavaScript's Number type
2013-11-08 12:45:37 +00:00
- Includes a `toFraction` and a correctly-rounded `squareRoot` method
2012-11-08 20:02:27 +00:00
- No dependencies
2014-11-13 23:23:45 +00:00
- ECMAScript 3 compliant
2014-12-29 00:32:05 +00:00
- Comprehensive documentation and test set
2012-11-08 20:02:27 +00:00
2012-11-09 19:56:46 +00:00
If an even smaller and simpler library is required see [big.js](https://github.com/MikeMcl/big.js/).
2013-01-06 17:21:43 +00:00
It's half the size but only works with decimal numbers and only has half the methods.
2014-05-08 22:31:53 +00:00
It also does not allow `NaN` or `Infinity`, or have the configuration options of this library.
See also [decimal.js](https://github.com/MikeMcl/decimal.js/).
2012-11-08 20:02:27 +00:00
## Load
2013-08-01 17:57:00 +00:00
The library is the single JavaScript file *bignumber.js* (or minified, *bignumber.min.js*).
2012-11-08 20:02:27 +00:00
It can be loaded via a script tag in an HTML document for the browser
<script src='./relative/path/to/bignumber.js'></script>
or as a CommonJS, [Node.js](http://nodejs.org) or AMD module using `require`.
For Node, put the *bignumber.js* file into the same directory as the file that is requiring it and use
2014-06-04 21:41:14 +00:00
var BigNumber = require('./bignumber.js');
2012-11-08 20:02:27 +00:00
or put it in a *node_modules* directory within the directory and use `require('bignumber.js')`.
2012-11-09 19:56:46 +00:00
The library is also available from the [npm](https://npmjs.org/) registry, so
$ npm install bignumber.js
2014-05-08 22:31:53 +00:00
will install this directory in a *node_modules* directory within the current directory.
2012-11-08 20:02:27 +00:00
To load with AMD loader libraries such as [requireJS](http://requirejs.org/):
2014-05-08 22:31:53 +00:00
require(['path/to/bignumber'], function(BigNumber) {
2012-11-08 20:02:27 +00:00
// Use BigNumber here in local scope. No global BigNumber.
});
## Use
*In all examples below, `var`, semicolons and `toString` calls are not shown.
If a commented-out value is in quotes it means `toString` has been called on the preceding expression.*
The library exports a single function: BigNumber, the constructor of BigNumber instances.
2013-08-21 22:54:28 +00:00
It accepts a value of type Number *(up to 15 significant digits only)*, String or BigNumber Object,
2012-11-08 20:02:27 +00:00
x = new BigNumber(123.4567)
y = BigNumber('123456.7e-3') // 'new' is optional
z = new BigNumber(x)
x.equals(y) && y.equals(z) && x.equals(z) // true
2013-11-16 23:55:34 +00:00
and a base from 2 to 64 inclusive can be specified.
2012-11-08 20:02:27 +00:00
x = new BigNumber(1011, 2) // "11"
y = new BigNumber('zz.9', 36) // "1295.25"
z = x.plus(y) // "1306.25"
A BigNumber is immutable in the sense that it is not changed by its methods.
0.3 - 0.1 // 0.19999999999999998
x = new BigNumber(0.3)
x.minus(0.1) // "0.2"
x // "0.3"
The methods that return a BigNumber can be chained.
x.dividedBy(y).plus(z).times(9).floor()
x.times('1.23456780123456789e+9').plus(9876.5432321).dividedBy('4444562598.111772').ceil()
Method names over 5 letters in length have a shorter alias.
x.squareRoot().dividedBy(y).toPower(3).equals(x.sqrt().div(y).pow(3)) // true
x.cmp(y.mod(z).neg()) == 1 && x.comparedTo(y.modulo(z).negated()) == 1 // true
2013-08-01 17:57:00 +00:00
Like JavaScript's Number type, there are `toExponential`, `toFixed` and `toPrecision` methods
2012-11-08 20:02:27 +00:00
x = new BigNumber(255.5)
x.toExponential(5) // "2.55500e+2"
x.toFixed(5) // "255.50000"
x.toPrecision(5) // "255.50"
2014-05-08 22:31:53 +00:00
x.toNumber() // 255.5
2012-11-08 20:02:27 +00:00
and a base can be specified for `toString`.
x.toString(16) // "ff.8"
2013-08-21 22:54:28 +00:00
The maximum number of decimal places of, and the rounding mode applied to, the results of operations involving division (i.e. division, square root, base conversion, and negative power operations) is set by a configuration object passed to the `config` method of the `BigNumber` constructor.
2013-01-06 17:21:43 +00:00
The other arithmetic operations always give the exact result.
2012-11-08 20:02:27 +00:00
2013-08-21 22:54:28 +00:00
BigNumber.config({ DECIMAL_PLACES: 10, ROUNDING_MODE: 4 })
2012-11-08 20:02:27 +00:00
// Alternatively, BigNumber.config( 10, 4 );
x = new BigNumber(2);
y = new BigNumber(3);
z = x.div(y) // "0.6666666667"
z.sqrt() // "0.8164965809"
z.pow(-3) // "3.3749999995"
z.toString(2) // "0.1010101011"
2012-11-12 10:54:40 +00:00
z.times(z) // "0.44444444448888888889"
z.times(z).round(10) // "0.4444444445"
2012-11-08 20:02:27 +00:00
There is a `toFraction` method with an optional *maximum denominator* argument
y = new BigNumber(355)
pi = y.dividedBy(113) // "3.1415929204"
pi.toFraction() // [ "7853982301", "2500000000" ]
pi.toFraction(1000) // [ "355", "113" ]
and `isNaN` and `isFinite` methods, as `NaN` and `Infinity` are valid `BigNumber` values.
x = new BigNumber(NaN) // "NaN"
y = new BigNumber(Infinity) // "Infinity"
x.isNaN() && !y.isNaN() && !x.isFinite() && !y.isFinite() // true
The value of a BigNumber is stored in a decimal floating point format in terms of a coefficient, exponent and sign.
x = new BigNumber(-123.456);
x.c // "1,2,3,4,5,6" coefficient (i.e. significand)
x.e // 2 exponent
x.s // -1 sign
2014-12-29 00:32:05 +00:00
For futher information see the API reference from the *doc* folder.
2012-11-08 20:02:27 +00:00
## Test
The *test* directory contains the test scripts for each method.
The tests can be run with Node or a browser.
For a quick test of all the methods, from a command-line shell at the *test/* directory
$ node quick-test
To test a single method in more depth, e.g.
$ node toFraction
To test all the methods in more depth
$ node every-test
2013-01-06 17:21:43 +00:00
For the browser, see *quick-test.html*, *single-test.html* and *every-test.html* in the *test/browser* directory.
2013-08-01 17:57:00 +00:00
*bignumber-vs-number.html* enables some of the methods of bignumber.js to be compared with those of JavaScript's Number type.
2012-11-08 20:02:27 +00:00
## Performance
The *perf* directory contains two applications and a *lib* directory containing the BigDecimal libraries used by both.
2013-08-01 17:57:00 +00:00
*bignumber-vs-bigdecimal.html* tests the performance of bignumber.js against the JavaScript translations of two versions of BigDecimal, its use should be more or less self-explanatory.
2012-11-08 20:02:27 +00:00
(The GWT version doesn't work in IE 6.)
* GWT: java.math.BigDecimal
<https://github.com/iriscouch/bigdecimal.js>
* ICU4J: com.ibm.icu.math.BigDecimal
<https://github.com/dtrebbien/BigDecimal.js>
2012-11-09 19:56:46 +00:00
The BigDecimal in Node's npm registry is the GWT version. Despite its seeming popularity I have found it to have some serious bugs, see the Node script *perf/lib/bigdecimal_GWT/bugs.js* for examples of flaws in its *remainder*, *divide* and *compareTo* methods.
2012-11-08 20:02:27 +00:00
*bigtime.js* is a Node command-line application which tests the performance of bignumber.js against the GWT version of BigDecimal from the npm registry.
For example, to compare the time taken by the bignumber.js `plus` method and the BigDecimal `add` method:
$ node bigtime plus 10000 40
This will time 10000 calls to each, using operands of up to 40 random digits and will check that the results match.
For help:
$ node bigtime -h
See the README in the directory for more information.
## Build
I.e. minify.
For Node, if uglify-js is installed globally ( `npm install uglify-js -g` ) then
2014-05-08 22:31:53 +00:00
npm run build
2012-11-08 20:02:27 +00:00
will create *bignumber.min.js*.
## Feedback
2013-11-08 12:45:37 +00:00
Open an issue, or email
2012-11-08 20:02:27 +00:00
2013-08-01 17:57:00 +00:00
Michael
2012-11-08 20:02:27 +00:00
<a href="mailto:M8ch88l@gmail.com">M8ch88l@gmail.com</a>
2012-11-28 19:36:36 +00:00
Bitcoin donation to:
2013-11-08 12:45:37 +00:00
**1CauoGYrEoJFhcyxGVaiLTE6f3WCaSUjnm**
2012-11-28 19:36:36 +00:00
Thank you
2012-11-26 22:33:07 +00:00
2012-11-08 20:02:27 +00:00
## Licence
2013-08-21 22:54:28 +00:00
MIT.
2012-11-08 20:02:27 +00:00
See LICENCE.
## Change Log
2014-11-13 23:23:45 +00:00
####1.5.0
* 13/11/2014 Added `toJSON` and `decimalPlaces` methods.
2014-06-08 22:45:03 +00:00
####1.4.1
* 08/06/2014 Amend README.
2014-05-08 22:31:53 +00:00
####1.4.0
* 08/05/2014 Added `toNumber`.
2013-11-08 12:45:37 +00:00
####1.3.0
* 08/11/2013 Ensure correct rounding of `sqrt` in all, rather than almost all, cases.
* Maximum radix to 64.
2013-10-17 12:27:23 +00:00
####1.2.1
2013-11-08 12:45:37 +00:00
* 17/10/2013 Sign of zero when x < 0 and x + (-x) = 0.
2013-10-17 12:27:23 +00:00
2013-09-19 11:15:54 +00:00
####1.2.0
* 19/9/2013 Throw Error objects for stack.
2013-08-21 22:54:28 +00:00
####1.1.1
* 22/8/2013 Show original value in constructor error message.
2013-08-01 17:57:00 +00:00
####1.1.0
2013-10-17 12:27:23 +00:00
* 1/8/2013 Allow numbers with trailing radix point.
2013-08-01 17:57:00 +00:00
2013-01-06 17:21:43 +00:00
####1.0.1
* Bugfix: error messages with incorrect method name
2012-11-08 20:02:27 +00:00
####1.0.0
2013-10-17 12:27:23 +00:00
* 8/11/2012 Initial release