big.js
A small, fast JavaScript library for arbitrary-precision decimal arithmetic.
The little sister to bignumber.js and decimal.js. See here for some notes on the difference between them.
Features
Faster, smaller and easier-to-use than JavaScript versions of Java's BigDecimal
Only 5.9 KB minified and 2.7 KB gzipped
Simple API
Replicates the
toExponential
,toFixed
andtoPrecision
methods of JavaScript's Number typeIncludes a
sqrt
methodStores values in an accessible decimal floating point format
No dependencies
Comprehensive documentation and test set
Set up
The library is the single JavaScript file big.js (or big.min.js, which is big.js minified).
Browser:
ES6 module:
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, Big
, the constructor of Big number instances. It accepts a value of type number, string or Big number object.
A Big number is immutable in the sense that it is not changed by its methods.
The methods that return a Big number can be chained.
Like JavaScript's Number type, there are toExponential
, toFixed
and toPrecision
methods.
The arithmetic methods always return the exact result except div
, sqrt
and pow
(with negative exponent), as these methods involve division.
The maximum number of decimal places and the rounding mode used to round the results of these methods is determined by the value of the DP
and RM
properties of the Big
number constructor.
Multiple Big number constructors can be created, each with an independent configuration.
The value of a Big number is stored in a decimal floating point format in terms of a coefficient, exponent and sign.
For further information see the API reference from the doc folder.
Test
The test directory contains the test scripts for each Big number method.
The tests can be run with Node.js or a browser.
To run all the tests
To test a single method
For the browser, see single-test.html and every-test.html in the test/browser directory.
big-vs-number.html is a simple application that enables some of the methods of big.js to be compared with those of JavaScript's Number type.
Performance
The perf directory contains two legacy 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 BigDecimal in the npm registry is the GWT version. It has some bugs, see the Node.js script perf/lib/bigdecimal_GWT/bugs.js for examples of flaws in its remainder, divide and compareTo methods.
bigtime.js is a Node.js 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
This will time 10000 calls to each, using operands of up to 40 random digits and will check that the results match.
For help
Build
If uglify-js is installed globally
then
will create big.min.js.
TypeScript
The DefinitelyTyped project has a Typescript type definitions file for big.js.
Any questions about the TypeScript type definitions file should be addressed to the DefinitelyTyped project.
Feedback
Bugs/comments/questions?
Open an issue, or email Michael
Licence
Contributors
This project exists thanks to all the people who contribute. [Contribute].
Backers
Thank you to all our backers! 🙏 [Become a backer]
Sponsors
Support this project by becoming a sponsor. Your logo will show up here with a link to your website. [Become a sponsor]
Last updated