NPM Star
Collections
  1. Home
  2. Compare
  3. big.js vs bignumber.js vs decimal.js
NPM Compare

Compare NPM packages statistics, trends, and features

CollectionsVS Code extensionChrome extensionTermsPrivacyLinkTreeIndiehackersBig Frontendqiuyumi

JavaScript Libraries for Accurate Decimal Calculations

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.

Math and Arithmeticdecimalarithmeticmathfinancialprecision

Unable to load comparison data. Please try again later.

Similar Packages

javascript-lattice

90%

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 Arithmetic

mathjs

80%

Mathjs 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 Library

precision

70%

Precision 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 Arithmetic

arbitrary-precision

60%

Arbitrary-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 Arithmetic

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

<a href="graphs/contributors"><img src="https://opencollective.com/bigjs/contributors.svg?width=890&button=false" /></a>

Financial supporters

Thank you to all who have supported this project via Open Collective, particularly Coinbase.

<img src="https://opencollective.com/bigjs/sponsor/0/avatar.svg">

bignumber.js

A JavaScript library for arbitrary-precision decimal and non-decimal arithmetic.

npm version npm downloads CI

<br />

Features

  • Integers and decimals
  • Simple API but full-featured
  • Faster, smaller, and perhaps easier to use than JavaScript versions of Java's BigDecimal
  • 8 KB minified and gzipped
  • Replicates the toExponential, toFixed, toPrecision and toString methods of JavaScript's Number type
  • Includes a toFraction and a correctly-rounded squareRoot method
  • Supports cryptographically-secure pseudo-random number generation
  • No dependencies
  • Wide platform compatibility: uses JavaScript 1.5 (ECMAScript 3) features only
  • Comprehensive documentation and test set

API

If a smaller and simpler library is required see big.js. It's less than half the size but only works with decimal numbers and only has half the methods. It also has fewer configuration options than this library, and does not allow NaN or Infinity.

See also decimal.js, which among other things adds support for non-integer powers, and performs all operations to a specified number of significant digits.

Load

The library is the single JavaScript file bignumber.js or ES module bignumber.mjs.

Browser

<script src='path/to/bignumber.js'></script>

ES module

<script type="module"> import BigNumber from './path/to/bignumber.mjs';

Get a minified version from a CDN:

<script src='https://cdn.jsdelivr.net/npm/bignumber.js@9.3.1/bignumber.min.js'></script>

Node.js

npm install bignumber.js
const BigNumber = require('bignumber.js');

ES module

import BigNumber from "bignumber.js";

Deno

// @deno-types="https://raw.githubusercontent.com/mikemcl/bignumber.js/v9.3.1/bignumber.d.mts" import BigNumber from 'https://raw.githubusercontent.com/mikemcl/bignumber.js/v9.3.1/bignumber.mjs'; // @deno-types="https://unpkg.com/bignumber.js@latest/bignumber.d.mts" import { BigNumber } from 'https://unpkg.com/bignumber.js@latest/bignumber.mjs';

Use

The library exports a single constructor function, BigNumber, which accepts a value of type Number, String or BigNumber,

let x = new BigNumber(123.4567); let y = BigNumber('123456.7e-3'); let z = new BigNumber(x); x.isEqualTo(y) && y.isEqualTo(z) && x.isEqualTo(z); // true

To get the string value of a BigNumber use toString() or toFixed(). Using toFixed() prevents exponential notation being returned, no matter how large or small the value.

let x = new BigNumber('1111222233334444555566'); x.toString(); // "1.111222233334444555566e+21" x.toFixed(); // "1111222233334444555566"

If the limited precision of Number values is not well understood, it is recommended to create BigNumbers from String values rather than Number values to avoid a potential loss of precision.

In all further examples below, let, semicolons and toString calls are not shown. If a commented-out value is in quotes it means toString has been called on the preceding expression.

// Precision loss from using numeric literals with more than 15 significant digits. new BigNumber(1.0000000000000001) // '1' new BigNumber(88259496234518.57) // '88259496234518.56' new BigNumber(99999999999999999999) // '100000000000000000000' // Precision loss from using numeric literals outside the range of Number values. new BigNumber(2e+308) // 'Infinity' new BigNumber(1e-324) // '0' // Precision loss from the unexpected result of arithmetic with Number values. new BigNumber(0.7 + 0.1) // '0.7999999999999999'

When creating a BigNumber from a Number, note that a BigNumber is created from a Number's decimal toString() value not from its underlying binary value. If the latter is required, then pass the Number's toString(2) value and specify base 2.

new BigNumber(Number.MAX_VALUE.toString(2), 2)

BigNumbers can be created from values in bases from 2 to 36. See ALPHABET to extend this range.

a = new BigNumber(1011, 2) // "11" b = new BigNumber('zz.9', 36) // "1295.25" c = a.plus(b) // "1306.25"

Performance is better if base 10 is NOT specified for decimal values. Only specify base 10 when you want to limit the number of decimal places of the input value to the current DECIMAL_PLACES setting.

A BigNumber is immutable in the sense that it is not changed by its methods.

0.3 - 0.1 // 0.19999999999999998 x = new BigNumber(0.3) x.minus(0.1) // "0.2" x // "0.3"

The methods that return a BigNumber can be chained.

x.dividedBy(y).plus(z).times(9) x.times('1.23456780123456789e+9').plus(9876.5432321).dividedBy('4444562598.111772').integerValue()

Some of the longer method names have a shorter alias.

x.squareRoot().dividedBy(y).exponentiatedBy(3).isEqualTo(x.sqrt().div(y).pow(3)) // true x.modulo(y).multipliedBy(z).eq(x.mod(y).times(z)) // true

As with JavaScript's Number type, there are toExponential, toFixed and toPrecision methods.

x = new BigNumber(255.5) x.toExponential(5) // "2.55500e+2" x.toFixed(5) // "255.50000" x.toPrecision(5) // "255.50" x.toNumber() // 255.5

A base can be specified for toString.

Performance is better if base 10 is NOT specified, i.e. use toString() not toString(10). Only specify base 10 when you want to limit the number of decimal places of the string to the current DECIMAL_PLACES setting.

x.toString(16) // "ff.8"

There is a toFormat method which may be useful for internationalisation.

y = new BigNumber('1234567.898765') y.toFormat(2) // "1,234,567.90"

The maximum number of decimal places of the result of an operation involving division (i.e. a division, square root, base conversion or negative power operation) is set using the set or config method of the BigNumber constructor.

The other arithmetic operations always give the exact result.

BigNumber.set({ DECIMAL_PLACES: 10, ROUNDING_MODE: 4 }) x = new BigNumber(2) y = new BigNumber(3) z = x.dividedBy(y) // "0.6666666667" z.squareRoot() // "0.8164965809" z.exponentiatedBy(-3) // "3.3749999995" z.toString(2) // "0.1010101011" z.multipliedBy(z) // "0.44444444448888888889" z.multipliedBy(z).decimalPlaces(10) // "0.4444444445"

There is a toFraction method with an optional maximum denominator argument

y = new BigNumber(355) pi = y.dividedBy(113) // "3.1415929204" pi.toFraction() // [ "7853982301", "2500000000" ] pi.toFraction(1000) // [ "355", "113" ]

and isNaN and isFinite methods, as NaN and Infinity are valid BigNumber values.

x = new BigNumber(NaN) // "NaN" y = new BigNumber(Infinity) // "Infinity" x.isNaN() && !y.isNaN() && !x.isFinite() && !y.isFinite() // true

The value of a BigNumber is stored in a decimal floating point format in terms of a coefficient, exponent and sign.

x = new BigNumber(-123.456); x.c // [ 123, 45600000000000 ] coefficient (i.e. significand) x.e // 2 exponent x.s // -1 sign

For advanced usage, multiple BigNumber constructors can be created, each with its own independent configuration.

// Set DECIMAL_PLACES for the original BigNumber constructor BigNumber.set({ DECIMAL_PLACES: 10 }) // Create another BigNumber constructor, optionally passing in a configuration object BN = BigNumber.clone({ DECIMAL_PLACES: 5 }) x = new BigNumber(1) y = new BN(1) x.div(3) // '0.3333333333' y.div(3) // '0.33333'

To avoid having to call toString or valueOf on a BigNumber to get its value in the Node.js REPL or when using console.log use

BigNumber.prototype[require('util').inspect.custom] = BigNumber.prototype.valueOf;

For further information see the API reference in the doc directory.

Test

The test/modules directory contains the test scripts for each method.

The tests can be run with Node.js or a browser. For Node.js use

npm test

or

node test/test

To test a single method, use, for example

node test/methods/toFraction

For the browser, open test/test.html.

Minify

To minify using, for example, terser

npm install -g terser
terser big.js -c -m -o big.min.js

Licence

The MIT Licence.

See LICENCE.

decimal.js

An arbitrary-precision Decimal type for JavaScript.

npm version npm downloads CDNJS

<br>

Features

  • Integers and floats
  • Simple but full-featured API
  • Replicates many of the methods of JavaScript's Number.prototype and Math objects
  • Also handles hexadecimal, binary and octal values
  • Faster, smaller, and perhaps easier to use than JavaScript versions of Java's BigDecimal
  • No dependencies
  • Wide platform compatibility: uses JavaScript 1.5 (ECMAScript 3) features only
  • Comprehensive documentation and test set
  • Used under the hood by math.js
  • Includes a TypeScript declaration file: decimal.d.ts

API

The library is similar to bignumber.js, but here precision is specified in terms of significant digits rather than decimal places, and all calculations are rounded to the precision (similar to Python's decimal module) rather than just those involving division.

This library also adds the trigonometric functions, among others, and supports non-integer powers, which makes it a significantly larger library than bignumber.js and the even smaller big.js.

For a lighter version of this library without the trigonometric functions see decimal.js-light.

Load

The library is the single JavaScript file decimal.js or ES module decimal.mjs.

Browser:

<script src='path/to/decimal.js'></script> <script type="module"> import Decimal from './path/to/decimal.mjs'; ... </script>

Node.js:

npm install decimal.js
const Decimal = require('decimal.js'); import Decimal from 'decimal.js'; import {Decimal} from 'decimal.js';

Use

In all examples below, semicolons and toString calls are not shown. If a commented-out value is in quotes it means toString has been called on the preceding expression.

The library exports a single constructor function, Decimal, which expects a single argument that is a number, string or Decimal instance.

x = new Decimal(123.4567) y = new Decimal('123456.7e-3') z = new Decimal(x) x.equals(y) && y.equals(z) && x.equals(z) // true

If using values with more than a few digits, it is recommended to pass strings rather than numbers to avoid a potential loss of precision.

// Precision loss from using numeric literals with more than 15 significant digits. new Decimal(1.0000000000000001) // '1' new Decimal(88259496234518.57) // '88259496234518.56' new Decimal(99999999999999999999) // '100000000000000000000' // Precision loss from using numeric literals outside the range of Number values. new Decimal(2e+308) // 'Infinity' new Decimal(1e-324) // '0' // Precision loss from the unexpected result of arithmetic with Number values. new Decimal(0.7 + 0.1) // '0.7999999999999999'

As with JavaScript numbers, strings can contain underscores as separators to improve readability.

x = new Decimal('2_147_483_647')

String values in binary, hexadecimal or octal notation are also accepted if the appropriate prefix is included.

x = new Decimal('0xff.f') // '255.9375' y = new Decimal('0b10101100') // '172' z = x.plus(y) // '427.9375' z.toBinary() // '0b110101011.1111' z.toBinary(13) // '0b1.101010111111p+8' // Using binary exponential notation to create a Decimal with the value of `Number.MAX_VALUE`. x = new Decimal('0b1.1111111111111111111111111111111111111111111111111111p+1023') // '1.7976931348623157081e+308'

Decimal instances are immutable in the sense that they are not changed by their methods.

0.3 - 0.1 // 0.19999999999999998 x = new Decimal(0.3) x.minus(0.1) // '0.2' x // '0.3'

The methods that return a Decimal can be chained.

x.dividedBy(y).plus(z).times(9).floor() x.times('1.23456780123456789e+9').plus(9876.5432321).dividedBy('4444562598.111772').ceil()

Many method names have a shorter alias.

x.squareRoot().dividedBy(y).toPower(3).equals(x.sqrt().div(y).pow(3)) // true x.comparedTo(y.modulo(z).negated() === x.cmp(y.mod(z).neg()) // true

Most of the methods of JavaScript's Number.prototype and Math objects are replicated.

x = new Decimal(255.5) x.toExponential(5) // '2.55500e+2' x.toFixed(5) // '255.50000' x.toPrecision(5) // '255.50' Decimal.sqrt('6.98372465832e+9823') // '8.3568682281821340204e+4911' Decimal.pow(2, 0.0979843) // '1.0702770511687781839' // Using `toFixed()` to avoid exponential notation: x = new Decimal('0.0000001') x.toString() // '1e-7' x.toFixed() // '0.0000001'

And there are isNaN and isFinite methods, as NaN and Infinity are valid Decimal values.

x = new Decimal(NaN) // 'NaN' y = new Decimal(Infinity) // 'Infinity' x.isNaN() && !y.isNaN() && !x.isFinite() && !y.isFinite() // true

There is also a toFraction method with an optional maximum denominator argument.

z = new Decimal(355) pi = z.dividedBy(113) // '3.1415929204' pi.toFraction() // [ '7853982301', '2500000000' ] pi.toFraction(1000) // [ '355', '113' ]

All calculations are rounded according to the number of significant digits and rounding mode specified by the precision and rounding properties of the Decimal constructor.

For advanced usage, multiple Decimal constructors can be created, each with their own independent configuration which applies to all Decimal numbers created from it.

// Set the precision and rounding of the default Decimal constructor Decimal.set({ precision: 5, rounding: 4 }) // Create another Decimal constructor, optionally passing in a configuration object Dec = Decimal.clone({ precision: 9, rounding: 1 }) x = new Decimal(5) y = new Dec(5) x.div(3) // '1.6667' y.div(3) // '1.66666666'

The value of a Decimal is stored in a floating point format in terms of its digits, exponent and sign, but these properties should be considered read-only.

x = new Decimal(-12345.67); x.d // [ 12345, 6700000 ] digits (base 10000000) x.e // 4 exponent (base 10) x.s // -1 sign

For further information see the API reference in the doc directory.

Test

To run the tests using Node.js from the root directory:

npm test

Each separate test module can also be executed individually, for example:

node test/modules/toFraction

To run the tests in a browser, open test/test.html.

Minify

Two minification examples:

Using uglify-js to minify the decimal.js file:

npm install uglify-js -g uglifyjs decimal.js --source-map url=decimal.min.js.map -c -m -o decimal.min.js

Using terser to minify the ES module version, decimal.mjs:

npm install terser -g terser decimal.mjs --source-map url=decimal.min.mjs.map -c -m --toplevel -o decimal.min.mjs
import Decimal from './decimal.min.mjs';

Licence

The MIT Licence

Dependencies Comparison

big.js

Dependencies

Dev Dependencies

Peer Dependencies

bignumber.js

Dependencies

Dev Dependencies

Peer Dependencies

decimal.js

Dependencies

Dev Dependencies

Peer Dependencies

StarsIssuesVersionUpdatedⓘLast publish dateCreatedⓘPackage creation dateSizeⓘMinified + Gzipped size
B
big.js
00N/AN/AN/Ainstall size N/A
B
bignumber.js
00N/AN/AN/Ainstall size N/A
D
decimal.js
00N/AN/AN/Ainstall size N/A

Who's Using These Packages

big.js

dinero.js
dinero.js

Create, calculate, and format money in JavaScript and TypeScript.

mall4j
mall4j

⭐️⭐️⭐️ 电商商城 小程序电商商城系统 PC商城 H5商城 APP商城 Java商城 O2O商城 跨境商城

BitcoinCacheMachine
BitcoinCacheMachine

Run privacy-preserving Bitcoin payment infrastructure at your home or office. Deploy on commodity x64_86.

mxg
mxg

MESMER XML GUI (MXG)

bignumber.js

decimal.js