big.js vs bignumber.js vs decimal.js vs mathjs
JavaScript Arbitrary-Precision Libraries
big.jsbignumber.jsdecimal.jsmathjsSimilar Packages:

JavaScript Arbitrary-Precision Libraries

JavaScript arbitrary-precision libraries are designed to handle numerical calculations with high precision, avoiding the pitfalls of floating-point arithmetic in JavaScript. These libraries are essential for applications that require accurate financial calculations, scientific computations, or any domain where precision is critical. They provide various functionalities for arithmetic operations, rounding, and formatting, ensuring that developers can work with numbers accurately without losing precision due to JavaScript's inherent limitations with floating-point numbers.

Npm Package Weekly Downloads Trend

3 Years

Github Stars Ranking

Stat Detail

Package
Downloads
Stars
Size
Issues
Publish
License
big.js05,17058.9 kB9a year agoMIT
bignumber.js06,992465 kB15a month agoMIT
decimal.js07,160284 kB209 months agoMIT
mathjs015,0259.43 MB1816 hours agoApache-2.0

Feature Comparison: big.js vs bignumber.js vs decimal.js vs mathjs

Precision Handling

  • big.js:

    big.js provides basic arbitrary-precision arithmetic, allowing for accurate calculations with large numbers. It is designed to handle numbers as strings to maintain precision, but it does not support advanced features like rounding modes or complex mathematical functions.

  • bignumber.js:

    bignumber.js offers arbitrary-precision arithmetic with a focus on performance. It allows for precise calculations with large numbers and includes features for rounding, formatting, and conversion, making it suitable for various applications.

  • decimal.js:

    decimal.js excels in handling decimal arithmetic with high precision. It supports various rounding modes and ensures that calculations remain accurate, making it ideal for financial applications where decimal representation is crucial.

  • mathjs:

    mathjs provides arbitrary-precision arithmetic along with a wide range of mathematical functions. It allows for precise calculations while also supporting complex numbers, matrices, and statistical functions, making it a versatile choice for mathematical applications.

Performance

  • big.js:

    big.js is lightweight and optimized for performance, making it suitable for applications where speed is essential. It has a smaller footprint compared to other libraries, which can be beneficial in performance-sensitive scenarios.

  • bignumber.js:

    bignumber.js is designed for high performance, especially in scenarios involving large numbers and complex calculations. It balances precision and speed, making it a good choice for applications that require extensive numerical operations.

  • decimal.js:

    decimal.js may have a slightly higher overhead due to its advanced features, but it provides excellent performance for decimal arithmetic. It is optimized for scenarios where precision is paramount, such as financial calculations.

  • mathjs:

    mathjs is more comprehensive and may be less performant than the others due to its extensive feature set. However, it is still efficient for most applications, especially those requiring a mix of precision and advanced mathematical capabilities.

Feature Set

  • big.js:

    big.js focuses on basic arithmetic operations and provides a simple API for handling large numbers. It is straightforward and easy to use, but lacks advanced mathematical functions.

  • bignumber.js:

    bignumber.js offers a rich set of features for arbitrary-precision arithmetic, including rounding, formatting, and conversion functions. It is suitable for applications that require a comprehensive arithmetic solution.

  • decimal.js:

    decimal.js provides advanced decimal arithmetic features, including various rounding modes and precise control over decimal places. It is particularly useful for applications in finance and accounting.

  • mathjs:

    mathjs is a full-featured mathematics library that includes arbitrary-precision arithmetic along with a wide range of mathematical functions, including algebra, calculus, and statistics. It is ideal for applications that require both precision and advanced mathematical capabilities.

Ease of Use

  • big.js:

    big.js is known for its simplicity and ease of use, making it a great choice for developers who need basic arbitrary-precision arithmetic without a steep learning curve.

  • bignumber.js:

    bignumber.js has a user-friendly API that is easy to understand, making it accessible for developers who need to perform precise calculations without extensive setup.

  • decimal.js:

    decimal.js may require a bit more understanding of its features due to its advanced capabilities, but it provides clear documentation to help users navigate its functionalities.

  • mathjs:

    mathjs has a steeper learning curve due to its extensive feature set, but it offers comprehensive documentation and examples, making it easier for developers to leverage its full potential.

Community and Support

  • big.js:

    big.js has a smaller community compared to other libraries, but it is well-maintained and has sufficient documentation for basic use cases.

  • bignumber.js:

    bignumber.js has a moderate community and is widely used in various applications, providing a good amount of resources and support for developers.

  • decimal.js:

    decimal.js has a dedicated user base, especially in financial sectors, and offers good documentation and community support for its advanced features.

  • mathjs:

    mathjs has a large and active community, with extensive documentation, tutorials, and examples available. It is well-supported and frequently updated, making it a reliable choice for complex mathematical applications.

How to Choose: big.js vs bignumber.js vs decimal.js vs mathjs

  • big.js:

    Choose big.js for its simplicity and lightweight nature when you need basic arbitrary-precision arithmetic without additional features. It is ideal for straightforward applications where performance is key and complex mathematical operations are not required.

  • bignumber.js:

    Opt for bignumber.js if you need a comprehensive solution for arbitrary-precision arithmetic with a focus on performance and flexibility. It supports a wide range of mathematical operations and is suitable for applications requiring extensive calculations, such as financial applications.

  • decimal.js:

    Select decimal.js if your application requires advanced decimal arithmetic and features like rounding modes and precision control. It is particularly useful for financial applications where decimal representation is crucial, providing a robust set of features for precise calculations.

  • mathjs:

    Use mathjs when you need a full-featured mathematics library that includes arbitrary-precision arithmetic along with a wide array of mathematical functions. It is perfect for applications that require both numerical precision and advanced mathematical capabilities, such as algebra, calculus, and statistics.

README for big.js

big.js

A small, fast JavaScript library for arbitrary-precision decimal arithmetic.

npm version npm downloads CI

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@7.0.1/big.min.js'></script>

Node.js

$ npm install big.js

CommonJS:

const Big = require('big.js');

ES module:

import Big from 'big.js';

Deno

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';

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')                 // '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.

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.