These packages help with precise decimal calculations in JavaScript, which is important because JavaScript has limitations with decimal numbers. They provide a way to perform mathematical operations with high accuracy, making them useful for financial, scientific, or other applications that require precision. Comparing them helps developers choose the best fit for their project.
Unable to load comparison data. Please try again later.
JavaScript Lattice is a library for arbitrary-precision arithmetic. It provides support for big numbers, fractions, and complex numbers, and is designed for high-performance calculations.
JavaScript Lattice is a good alternative to big.js, bignumber.js, and decimal.js because it's designed for high-performance calculations and provides support for fractions and complex numbers. It's a better choice when you need to perform fast and accurate calculations, especially in scientific or financial applications.
Arbitrary-Precision ArithmeticMathjs is a comprehensive math library that provides support for big numbers, complex numbers, and matrices. It allows you to perform advanced mathematical operations and has a simple and intuitive API.
Mathjs is a good alternative to big.js, bignumber.js, and decimal.js because it provides a more extensive set of mathematical functions and is actively maintained. It's a better choice when you need to perform complex mathematical operations beyond just big numbers. Mathjs has a steeper learning curve, but it's worth it for its advanced features.
Math LibraryPrecision is a lightweight library for arbitrary-precision arithmetic. It provides support for big numbers and is designed for simplicity and ease of use.
Precision is a good alternative to big.js, bignumber.js, and decimal.js because it's lightweight and easy to use. It's a better choice when you need a simple and straightforward solution for big numbers, and don't require advanced mathematical functions.
Arbitrary-Precision ArithmeticArbitrary-Precision is a library for big number arithmetic. It provides support for big integers and big decimals, and is designed for simplicity and ease of use.
Arbitrary-Precision is a good alternative to big.js, bignumber.js, and decimal.js because it's simple and easy to use. It's a better choice when you need a lightweight solution for big numbers, and don't require advanced mathematical functions.
Arbitrary-Precision ArithmeticA small, fast JavaScript library for arbitrary-precision decimal arithmetic.
toExponential
, toFixed
and toPrecision
methods of JavaScript NumbersThe little sister to bignumber.js and decimal.js. See here for some notes on the difference between them.
The library is the single JavaScript file big.js or the ES module big.mjs.
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@7.0.1/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/v7.0.1/big.mjs'; import Big from 'https://unpkg.com/big.js@latest/big.mjs';
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') // 'new' is optional z = new Big(x) x.eq(y) && x.eq(z) && y.eq(z) // true
In Big strict mode, creating a Big number from a primitive number is disallowed.
Big.strict = true x = new Big(1) // TypeError: [big.js] Invalid number y = new Big('1.0000000000000001') y.toNumber() // Error: [big.js] Imprecise conversion
A Big number is immutable in the sense that it is not changed by its methods.
0.3 - 0.1 // 0.19999999999999998 x = new Big(0.3) x.minus(0.1) // "0.2" x // "0.3"
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)) // true
Like JavaScript's Number type, there are toExponential
, toFixed
and toPrecision
methods.
x = new Big(255.5) x.toExponential(5) // "2.55500e+2" x.toFixed(5) // "255.50000" x.toPrecision(5) // "255.50"
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) // "0.6666666667" z.sqrt() // "0.8164965809" z.pow(-3) // "3.3749999995" z.times(z) // "0.44444444448888888889" z.times(z).round(10) // "0.4444444445"
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 // [1,2,3,4,5,6] coefficient (i.e. significand) x.e // 2 exponent x.s // -1 sign
For advanced usage, multiple Big number constructors can be created, each with an independent configuration.
For further information see the API reference documentation.
To minify using, for example, npm and terser
$ npm install -g terser
$ terser big.js -c -m -o big.min.js
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.
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.
<a href="graphs/contributors"><img src="https://opencollective.com/bigjs/contributors.svg?width=890&button=false" /></a>
Thank you to all who have supported this project via Open Collective, particularly Coinbase.
<img src="https://opencollective.com/bigjs/sponsor/0/avatar.svg">