JavaScript numbers follow the IEEE 754 standard, which causes rounding errors in decimal math β like adding 0.1 and 0.2. These libraries replace native numbers with objects that handle precise decimal arithmetic. They are essential for financial apps, scientific calculations, and any scenario where exact values matter more than raw speed.
JavaScript uses floating-point numbers for all math, which leads to errors like 0.1 + 0.2 === 0.30000000000000004. For money or science, this is unacceptable. The big.js, bignumber.js, and decimal.js-light packages solve this by using arbitrary-precision decimal arithmetic. They look similar but differ in scope, configuration, and maintenance. Let's break down how they handle real-world tasks.
All three libraries require importing a class and creating instances from strings or numbers. Using strings is safer to avoid initial floating-point errors.
big.js uses a simple constructor with global configuration options.
import Big from 'big.js';
// Set global decimal places
Big.DP = 2;
Big.RM = 1;
const price = new Big('10.50');
bignumber.js offers a configurable constructor with a wider range of settings.
import BigNumber from 'bignumber.js';
// Configure precision and rounding
BigNumber.config({ DECIMAL_PLACES: 2, ROUNDING_MODE: 1 });
const price = new BigNumber('10.50');
decimal.js-light mimics the main decimal.js API with a similar setup.
import Decimal from 'decimal.js-light';
// Set precision globally
Decimal.set({ precision: 20 });
const price = new Decimal('10.50');
Performing math is the core use case. All three return new instances instead of changing the original value, which prevents side effects.
big.js uses short method names like times and div.
import Big from 'big.js';
const a = new Big('10');
const b = new Big('3');
const result = a.times(b).div(2);
// Returns Big object representing 15
bignumber.js prefers descriptive names like multipliedBy but supports aliases.
import BigNumber from 'bignumber.js';
const a = new BigNumber('10');
const b = new BigNumber('3');
const result = a.multipliedBy(b).dividedBy(2);
// Returns BigNumber object representing 15
decimal.js-light follows the decimal.js convention with times and div.
import Decimal from 'decimal.js-light';
const a = new Decimal('10');
const b = new Decimal('3');
const result = a.times(b).div(2);
// Returns Decimal object representing 15
Handling decimal places and rounding modes is critical for financial reports. Each library handles this slightly differently.
big.js relies on global settings or method arguments for fixed output.
import Big from 'big.js';
const val = new Big('10.555');
// Uses global Big.DP setting
const fixed = val.toFixed();
// Or specify locally: val.toFixed(2)
bignumber.js allows per-operation rounding control with extensive options.
import BigNumber from 'bignumber.js';
const val = new BigNumber('10.555');
// Specify rounding mode directly
const fixed = val.decimalPlaces(2, BigNumber.ROUND_HALF_UP);
decimal.js-light supports similar per-operation control as the main library.
import Decimal from 'decimal.js-light';
const val = new Decimal('10.555');
// Specify rounding mode directly
const fixed = val.toDecimalPlaces(2, Decimal.ROUND_HALF_UP);
The libraries differ in what math they support beyond basic decimals. This affects whether they fit your project long-term.
big.js is strictly decimal. It does not support hex, binary, or complex exponents.
import Big from 'big.js';
// This works
const a = new Big('10.5');
// Non-decimal bases are NOT supported
// const b = new Big('ff', 16); // Throws error
bignumber.js supports multiple bases and advanced math functions.
import BigNumber from 'bignumber.js';
// Base 10 works
const a = new BigNumber('10.5');
// Base 16 (hex) also works
const b = new BigNumber('ff', 16);
decimal.js-light is decimal-focused like big.js but retains some decimal.js structure.
import Decimal from 'decimal.js-light';
// This works
const a = new Decimal('10.5');
// Non-decimal bases are generally NOT supported in light version
// const b = new Decimal('ff', 16); // Not supported
Knowing who maintains the code helps you judge long-term reliability for production apps.
big.js is maintained by Michael Mcl, who also maintains bignumber.js. It is stable and receives security updates.
// Stable API, rarely changes
import Big from 'big.js';
// No major breaking changes in recent years
bignumber.js is also maintained by Michael Mcl. It is the most robust option with active development.
// Active development
import BigNumber from 'bignumber.js';
// Regular updates for edge cases
decimal.js-light is a community fork of decimal.js. It is less actively maintained than the main decimal.js project.
// Community maintained
import Decimal from 'decimal.js-light';
// Updates may lag behind main decimal.js project
While the differences matter, these libraries share core goals and behaviors.
// All handle this without losing precision
// 0.1 + 0.2 === 0.3
const a = new Big('10');
const b = a.plus('5');
// 'a' is still 10, 'b' is 15
// Recommended
new Big('0.1');
// Risky
new Big(0.1);
val.toFixed(2);
val.toString();
| Feature | big.js | bignumber.js | decimal.js-light |
|---|---|---|---|
| Maintainer | π€ Michael Mcl | π€ Michael Mcl | π₯ Community Fork |
| Base Support | π’ Decimal Only | π’ Decimal, Hex, Binary, etc. | π’ Decimal Only |
| Method Names | β‘ Short (times, div) | π Descriptive (multipliedBy) | β‘ Short (times, div) |
| Configurability | π§ Basic | π§ Advanced | π§ Moderate |
| Bundle Footprint | π Small | π Larger | π Medium |
| Best Use Case | π° Currency, Simple Forms | π¬ Science, Crypto, Complex Math | π± Existing decimal.js Users |
big.js is like a precision screwdriver πͺ β small, focused, and perfect for specific jobs like currency math. Use it when you want reliability without extra weight.
bignumber.js is like a full mechanic's kit π§° β heavy but capable of handling any math problem you throw at it. Use it when you need flexibility or non-decimal bases.
decimal.js-light is like a compact travel kit π β designed for those who know the main decimal.js tools but need to save space. Use it if you are already committed to that API style.
Final Thought: All three solve the floating-point problem effectively. For most frontend financial apps, big.js offers the best balance of size and stability. Choose bignumber.js only if you need its advanced features, and treat decimal.js-light as a niche option for specific bundle constraints.
Choose big.js if you need strict decimal arithmetic for currency or fixed-point math with a minimal footprint. It is stable, maintained by the same author as bignumber.js, and focuses solely on base-10 operations without extra features. This makes it ideal for frontend forms or simple financial calculations where bundle size is a concern.
Choose bignumber.js if you need advanced features like non-decimal bases (hex, binary), complex configuration, or support for very large numbers beyond standard decimals. It is the most flexible option and handles edge cases well, making it suitable for scientific apps or cryptography where math requirements vary widely.
Choose decimal.js-light if you need API compatibility with the full decimal.js library but require a smaller bundle size. Be aware that it is a community fork with less active development than the main decimal.js project. It works well for projects already standardized on the decimal.js API that need to optimize load times.
A small, fast JavaScript library for arbitrary-precision decimal arithmetic.
toExponential, toFixed and toPrecision methods of JavaScript NumbersThe little sister to bignumber.js and decimal.js. See here for some notes on the difference between them.
The library is the single JavaScript file big.js or the ES module big.mjs.
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/[email protected]/big.min.js'></script>
$ npm install big.js
CommonJS:
const Big = require('big.js');
ES module:
import Big from 'big.js';
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';
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.
To minify using, for example, npm and terser
$ npm install -g terser
$ terser big.js -c -m -o big.min.js
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.
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.
Thank you to all who have supported this project via Open Collective, particularly Coinbase.