What is big.js?
The big.js npm package is a library for arbitrary-precision decimal arithmetic. It allows you to perform calculations on numbers with a large number of digits without losing precision, which is a common issue with JavaScript's native Number type.
What are big.js's main functionalities?
Arithmetic Operations
Perform precise addition, subtraction, multiplication, and division operations.
{"addition": "new Big('0.1').plus(new Big('0.2')).toString()", "subtraction": "new Big('0.3').minus(new Big('0.1')).toString()", "multiplication": "new Big('0.2').times(new Big('0.3')).toString()", "division": "new Big('0.3').div(new Big('0.2')).toString()"}
Comparison Operations
Compare two Big numbers to determine equality, or whether one is greater than or less than the other.
{"equals": "new Big('1.0').eq(new Big('1.00'))", "greaterThan": "new Big('2').gt(new Big('1.9999'))", "lessThan": "new Big('0.1').lt(new Big('0.2'))"}
Rounding
Round Big numbers to a specified number of decimal places, or convert a Big number to a fixed-point notation string.
{"round": "new Big('0.12345').round(2).toString()", "toFixed": "new Big('0.12345').toFixed(2)"}
Configuration
Configure the number of decimal places for rounding and the rounding mode (e.g., round half up, round down, etc.).
{"setDP": "Big.DP = 10", "setRM": "Big.RM = 1"}
Other packages similar to big.js
bignumber.js
bignumber.js is another arbitrary-precision decimal and non-decimal arithmetic library with similar functionality to big.js. It provides more features, such as support for non-decimal bases, but it might be slower for some operations due to its broader scope.
decimal.js
decimal.js is a library for arbitrary-precision arithmetic that is similar to big.js but with additional features like trigonometric and logarithmic functions, which big.js does not have. It also allows for immutable Decimal instances.
fraction.js
fraction.js is focused on rational numbers (fractions) and provides arithmetic operations for them. It is different from big.js, which deals with decimal numbers, but it can be used for high-precision calculations where representing numbers as fractions is more appropriate.
big.js
A small, fast JavaScript library for arbitrary-precision decimal arithmetic.
Features
- Simple API
- Faster, smaller and easier-to-use than JavaScript versions of Java's BigDecimal
- Only 6 KB minified
- Replicates the
toExponential
, toFixed
and toPrecision
methods of JavaScript Numbers - Stores values in an accessible decimal floating point format
- Comprehensive documentation and test set
- No dependencies
- Uses ECMAScript 3 only, so works in all browsers
The little sister to bignumber.js and decimal.js. See here for some notes on the difference between them.
Install
The library is the single JavaScript file big.js or the ES module big.mjs.
Browsers
Add Big to global scope:
<script src='path/to/big.js'></script>
ES module:
<script type='module'>
import Big from './path/to/big.mjs';
Get a minified version from a CDN:
<script src='https://cdn.jsdelivr.net/npm/big.js@6.2.0/big.min.js'></script>
$ npm install big.js
CommonJS:
const Big = require('big.js');
ES module:
import Big from 'big.js';
import Big from 'https://raw.githubusercontent.com/mikemcl/big.js/v6.2.0/big.mjs';
import Big from 'https://unpkg.com/big.js@6.2.0/big.mjs';
Use
In the code examples below, semicolons and toString
calls are not shown.
The library exports a single constructor function, Big
.
A Big number is created from a primitive number, string, or other Big number.
x = new Big(123.4567)
y = Big('123456.7e-3')
z = new Big(x)
x.eq(y) && x.eq(z) && y.eq(z)
In Big strict mode, creating a Big number from a primitive number is disallowed.
Big.strict = true
x = new Big(1)
y = new Big('1.0000000000000001')
y.toNumber()
A Big number is immutable in the sense that it is not changed by its methods.
0.3 - 0.1
x = new Big(0.3)
x.minus(0.1)
x
The methods that return a Big number can be chained.
x.div(y).plus(z).times(9).minus('1.234567801234567e+8').plus(976.54321).div('2598.11772')
x.sqrt().div(y).pow(3).gt(y.mod(z))
Like JavaScript's Number type, there are toExponential
, toFixed
and toPrecision
methods.
x = new Big(255.5)
x.toExponential(5)
x.toFixed(5)
x.toPrecision(5)
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.
Big.DP = 10
Big.RM = Big.roundHalfUp
x = new Big(2);
y = new Big(3);
z = x.div(y)
z.sqrt()
z.pow(-3)
z.times(z)
z.times(z).round(10)
The value of a Big number is stored in a decimal floating point format in terms of a coefficient, exponent and sign.
x = new Big(-123.456);
x.c
x.e
x.s
For advanced usage, multiple Big number constructors can be created, each with an independent configuration.
For further information see the API reference documentation.
Minify
To minify using, for example, npm and terser
$ npm install -g terser
$ terser big.js -c -m -o big.min.js
Test
The test directory contains the test scripts for each Big number method.
The tests can be run with Node.js or a browser.
Run all the tests:
$ npm test
Test a single method:
$ node test/toFixed
For the browser, see runner.html and test.html in the test/browser directory.
big-vs-number.html is a old application that enables some of the methods of big.js to be compared with those of JavaScript's Number type.
TypeScript
The DefinitelyTyped project has a Typescript type definitions file for big.js.
$ npm install --save-dev @types/big.js
Any questions about the TypeScript type definitions file should be addressed to the DefinitelyTyped project.
Licence
MIT
Contributors
Financial supporters
Thank you to all who have supported this project via Open Collective, particularly Coinbase.