big.js vs bignumber.js vs decimal.js-light
JavaScript Libraries for Arbitrary-Precision Decimal Arithmetic
big.jsbignumber.jsdecimal.js-lightSimilar Packages:

JavaScript Libraries for Arbitrary-Precision Decimal Arithmetic

These libraries provide solutions for handling arbitrary-precision decimal arithmetic in JavaScript, which is crucial for applications that require high precision in calculations, such as financial applications, scientific computations, and anywhere floating-point precision errors can lead to significant issues. They help developers avoid the pitfalls of JavaScript's native number type, which can lead to inaccuracies due to its reliance on binary floating-point representation.

Npm Package Weekly Downloads Trend

3 Years

Github Stars Ranking

Stat Detail

Package
Downloads
Stars
Size
Issues
Publish
License
big.js05,15758.9 kB910 months agoMIT
bignumber.js06,985465 kB135 days agoMIT
decimal.js-light0405-65 years agoMIT

Feature Comparison: big.js vs bignumber.js vs decimal.js-light

Precision Handling

  • big.js:

    big.js provides a simple API for arbitrary-precision arithmetic, allowing for precise calculations with a focus on performance. It supports basic operations like addition, subtraction, multiplication, and division without compromising on speed, making it suitable for applications that prioritize efficiency.

  • bignumber.js:

    bignumber.js excels in precision handling with a robust set of features, including support for various rounding modes and the ability to handle very large and very small numbers. This library is particularly useful in financial applications where precision is critical, as it allows for detailed control over calculations.

  • decimal.js-light:

    decimal.js-light offers a flexible approach to precision handling, supporting both decimal and binary floating-point arithmetic. It provides a good balance between performance and functionality, making it suitable for applications that require versatility in numeric operations.

Performance

  • big.js:

    big.js is designed for high performance, making it one of the fastest libraries for arbitrary-precision arithmetic. Its lightweight nature ensures minimal overhead, which is beneficial for applications that require rapid calculations without the need for extensive features.

  • bignumber.js:

    While bignumber.js is slightly heavier due to its comprehensive feature set, it still maintains good performance for most applications. However, it may not be as fast as big.js for simple arithmetic operations, making it less ideal for performance-critical scenarios.

  • decimal.js-light:

    decimal.js-light strikes a balance between performance and features, offering decent speed while providing a rich set of functionalities. It is suitable for applications that need a mix of performance and advanced arithmetic capabilities.

API Complexity

  • big.js:

    big.js has a straightforward and minimalistic API, making it easy to learn and use for developers who need basic arbitrary-precision arithmetic. This simplicity is advantageous for quick implementations and for developers who prefer a less complex library.

  • bignumber.js:

    bignumber.js has a more complex API due to its extensive feature set, which may require a steeper learning curve for developers. However, this complexity allows for greater flexibility and control over numerical operations, which can be beneficial for advanced use cases.

  • decimal.js-light:

    decimal.js-light offers a moderately complex API that balances ease of use with advanced features. It is user-friendly enough for beginners while still providing the necessary tools for more complex arithmetic operations.

Community and Maintenance

  • big.js:

    big.js has a smaller community compared to the other libraries, but it is actively maintained and updated. This makes it a reliable choice for developers looking for a lightweight solution without the need for extensive community support.

  • bignumber.js:

    bignumber.js has a larger community and is widely used in various applications, ensuring that it is well-maintained and regularly updated. This community support can be beneficial for developers seeking help or resources.

  • decimal.js-light:

    decimal.js-light also has a growing community and is actively maintained. Its balance of features and performance has garnered interest, making it a viable option for developers looking for a modern library with community backing.

Use Cases

  • big.js:

    big.js is ideal for applications that require fast and efficient calculations with basic arbitrary-precision needs, such as simple financial calculations or mathematical operations where performance is critical.

  • bignumber.js:

    bignumber.js is best suited for complex applications that require extensive numerical operations, such as financial software, scientific calculations, and any application where precision and rounding control are paramount.

  • decimal.js-light:

    decimal.js-light is versatile enough for applications that need both decimal and binary floating-point arithmetic, making it suitable for a wide range of use cases, including financial applications and data analysis.

How to Choose: big.js vs bignumber.js vs decimal.js-light

  • big.js:

    Choose big.js if you need a lightweight library focused on performance and simplicity for basic arbitrary-precision arithmetic without the overhead of additional features. It's ideal for applications that require fast calculations and minimal footprint.

  • bignumber.js:

    Opt for bignumber.js if you need a comprehensive solution that supports a wide range of mathematical operations, including complex calculations and rounding modes. This library is suitable for applications that require extensive numerical operations and precision handling.

  • decimal.js-light:

    Select decimal.js-light for a balance between performance and feature set, especially if you need a library that supports both decimal and binary floating-point arithmetic. It's a good choice for applications that require both precision and flexibility in handling different numeric types.

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.