📖 These are the docs for an older version. View the latest BigDecimal.js documentation →

bigdecimal.js

BigDecimal.js

NPM Version NPM Downloads codecov

BigInt based BigDecimal implementation for Node.js 10.4 and above. This implementation is inspired from java BigDecimal class. This implementation is faster than popular big decimal libraries for most operations. See benchmarks results part below for comparison of each operation.

Advantages of this library

  • Faster than other BigDecimal libraries because of native BigInt
  • Simple API that is almost same with Java's BigDecimal
  • No dependencies
  • Well tested
  • Includes type definition file

Disadvantages

  • This library's minified version is about 5 times larger than big.js's minified version. So the library is not small.

Installation

npm install bigdecimal.js

Usage

  • The example usage is given below:
// Single unified constructor for multiple values
const { Big } = require('bigdecimal.js');

// Construct from a string and clone it
const x = Big('1.1111111111111111111111');
const y = new Big(x); // you can also use 'new'

const z = x.add(y);
console.log(z.toString()); // 2.2222222222222222222222

// You can also construct from a number or BigInt:
const u = Big(1.1);
const v = Big(2n);

console.log(u.toString()); // 1.1
console.log(v.toString()); // 2

You can use MathContext to set precision and rounding mode for a specific operation:

const { Big, MC, RoundingMode } = require('bigdecimal.js');

const x = Big('1');
const y = Big('3');

// MC is MathContext constructor that can be used with or without `new`
const res1 = x.divideWithMathContext(y, MC(3));
console.log(res1.toString()); // 0.333

const res2 = x.divideWithMathContext(y, new MC(3, RoundingMode.UP));
console.log(res2.toString()); // 0.334

try {
x.divide(y);
// throws since full precision is requested but it is not possible
} catch (e) {
console.log(e); // RangeError: Non-terminating decimal expansion; no exact representable decimal result.
}

Documentation

Testing

  • Install dependencies: npm i
  • Compile: npm run compile
  • Run tests: npm test

Running Benchmarks

There is a benchmark suite that compares

To run the benchmark run npm install and then npm run benchmark.

Benchmark Results

Benchmarked against big.js, bigdecimal (GWT-based), bignumber.js and decimal.js.

  • Test Machine:

    • Apple M1
    • 8 GB Ram
    • macOS 26.3
    • Node.js 24
  • Update Date: July 7th 2026

  • Library versions used:

    • big.js 7.0.1
    • (this library) bigdecimal.js 1.3.2
    • bigdecimal 0.6.1
    • bignumber.js: 11.1.5
    • decimal.js: 10.6.0
  • Each operation is run with fixed set of decimal numbers composed of both simple and complex numbers.

  • Micro benchmark framework used is benchmark. Check out benchmarks folder for source code of benchmarks.

  • A green percentage means the other library is faster than bigdecimal.js for that operation; red means it is slower.

  • Operations per second(op/s):

Operation Bigdecimal.js Big.js BigNumber.js decimal.js GWTBased
Constructor 45,887 ( - ) 42,897 (-7%) 48,456 (+6%) 45,833 ( - ) 3,205 (-93%)
Add 118,793 ( - ) 22,569 (-81%) 107,339 (-10%) 86,834 (-27%) 105 (-100%)
Subtract 110,650 ( - ) 22,453 (-80%) 102,303 (-8%) 80,321 (-27%) 108 (-100%)
Multiply 847,935 ( - ) 33,828 (-96%) 91,129 (-89%) 82,154 (-90%) 3,279 (-100%)
Divide 26,041 ( - ) 1,105 (-96%) 12,644 (-51%) 15,206 (-42%) 814 (-97%)
Remainder 13,824 ( - ) 3,878 (-72%) 15,079 (+9%) 23,919 (+73%) 3,091 (-78%)
Positive pow 31,912 ( - ) 26 (-100%) 118 (-100%) 3,626 (-89%) 7 (-100%)
Negative pow 7,139 ( - ) 22 (-100%) 113 (-98%) 2,036 (-71%) 335 (-95%)
Abs 3,555,797 ( - ) 1,820,078 (-49%) 994,210 (-72%) 356,897 (-90%) 17,287 (-100%)
Compare 2,195,934 ( - ) 1,222,181 (-44%) 933,381 (-57%) 423,271 (-81%) 1,197,847 (-45%)