A small, fast JavaScript library for arbitrary-precision decimal arithmetic.
The little sister to bignumber.js. See also decimal.js, and here for the difference between them.
- Faster, smaller and easier-to-use than JavaScript versions of Java's BigDecimal
- Only 2.7 KB minified and gzipped
- Simple API
- Replicates the
toExponential,toFixedandtoPrecisionmethods of JavaScript's Number type - Includes a
sqrtmethod - Stores values in an accessible decimal floating point format
- No dependencies
- Comprehensive documentation and test set
The library is the single JavaScript file big.js (or big.min.js, which is big.js minified).
It can be loaded via a script tag in an HTML document for the browser
<script src='./relative/path/to/big.js'></script>or as a CommonJS, Node.js or AMD module using require.
var Big = require('big.js');For Node.js, the library is available from the npm registry:
$ npm install big.js
The test directory contains the test scripts for each Big number method.
The tests can be run with Node or a browser.
To test a single method, from a command-line shell at the test directory, use e.g.
$ node toFixed
To test all the methods
$ node every-test
For the browser, see single-test.html and every-test.html in the test/browser directory.
big-vs-number.html enables some of the methods of big.js to be compared with those of JavaScript's Number type.
The perf directory contains two applications and a lib directory containing the BigDecimal libraries used by both.
big-vs-bigdecimal.html tests the performance of big.js against the JavaScript translations of two versions of BigDecimal, its use should be more or less self-explanatory. (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
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.
bigtime.js is a Node command-line application which tests the performance of big.js against the GWT version of BigDecimal from the npm registry.
For example, to compare the time taken by the big.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
I.e. minify.
For Node, if uglify-js is installed globally ( npm install uglify-js -g ) then
uglifyjs -o ./big.min.js ./big.js
will create big.min.js.
The big.min.js already present was created with Microsoft Ajax Minifier 5.11.
The DefinitelyTyped project has a TypeScript definitions file for big.js.
The definitions file can be added to your project via the big.js.TypeScript.DefinitelyTyped NuGet package or via tsd.
tsd query big.js --action install
Any questions about the TypeScript definitions file should be addressed to the DefinitelyTyped project.
Feedback is welcome.
Bugs/comments/questions? Open an issue, or email
Michael M8ch88l@gmail.com
Bitcoin donation to: 1DppGRQSjVSMgGxuygDEHQuWEdTiVEzJYG Thank you
See LICENCE.