Skip to content
Michael M edited this page Sep 21, 2020 · 14 revisions

What is the difference between big.js, bignumber.js and decimal.js?

big.js is a minimalist arbitrary-precision library. It is the simplest and smallest of the three, less than half the size of bignumber.js and with only half the methods. It doesn't accept NaN or Infinity as legitimate values, it doesn't work with numbers of other bases, and the runtime configuration opions are limited to setting the number of decimal places and rounding mode of operations involving division, and the exponent value at which toString produces exponential notation.

Big.DP = 7;
Big.RM = 4;             // round half-up
var x = new Big(5);
x.div(3).toString();    // '1.6666667' 

All three libraries include the toExponential, toFixed and toPrecision methods of JavaScript's Number type.

bignumber.js and decimal.js store values in a higher base than big.js so they may be faster when performing operations on values with a large number of digits.

bignumber.js and decimal.js can work with values in other bases, and support the prefixes such as '0x' for hexdecimal. decimal.js can also handle binary, octal and hexdecimal values in binary exponential notation, such as found in the C programming language.

var x = new BigNumber('ff.8', 16);
x.toString();          // '255.5'
x.toString(16);        // 'ff.8'

var x = new Decimal('0xff.8');
x.toString();          // '255.5'
x.toHexadecimal();     // '0xff.8'
x.toHex(3);            // '0x1.ffp+7'

decimal.js was orginally developed through adding support for non-integer powers to bignumber.js, but I decided to release it as a separate library. The main difference between them is that precision is specified in terms of significant digits in decimal.js instead of decimal places, and all calculations are rounded to that precision (similar to Python's decimal module) rather than just those involving division.

Bignumber.config({ DECIMAL_PLACES: 3, ROUNDING_MODE; 1 });
var x = new BigNumber('123.456789');
x.plus(1).toString();    // '124.456789'

Decimal.set({ precision: 7, rounding: 4 });
var y = new Decimal('123.456789');
y.plus(1).toString();    // '124.4568'

bignumber.js is perhaps more suitable for financial applications because the user doesn't need to worry about losing precision unless an operation involving division is used.

decimal.js may be better for more scientific applications as it can handle very small or large values more efficiently. For example, it does not have the limitation of bignumber.js that when adding a value with a small exponent to one with a large exponent, bignumber.js will attempt to perform the operation to full precision, which may make the time taken for the operation unviable.

As mentioned above, decimal.js also supports non-integer powers and adds the trigonometric functions and exp, ln, and log methods. These additions make decimal.js significantly larger than bignumber.js.

big.js: minimalist; easy-to-use; precision specified in decimal places; precision applied to division only; 4 rounding modes.

bignumber.js: configuration options; NaN; Infinity; precision specified in decimal places; precision applied to division only; random numbers; base conversion; 9 rounding modes; modulo modes; modular exponentiation.

decimal.js: configuration options; NaN; Infinity; non-integer powers, exp, ln, log; trigonometric functions; precision specified in significant digits; precision always applied; random numbers; 9 rounding modes; modulo modes; binary, octal, and hexadecimal; binary exponential notation.

Clone this wiki locally