bignumber.js offers a BigNumber type for precise decimal and non-decimal math in JavaScript with formatting, fractions, and configurable rounding.
Overview
bignumber.js is a JavaScript library for arbitrary-precision decimal and non-decimal arithmetic. It exports a single BigNumber constructor that accepts Number, String, BigInt, or BigNumber values and performs exact arithmetic except where division-related operations honor configured decimal places.
The API mirrors familiar Number methods such as toExponential, toFixed, and toPrecision, and adds utilities like toFraction, squareRoot, and internationalization via toFormat. Values are immutable: methods return new BigNumbers and can be chained. Multiple independent constructors are supported with separate configuration.
Build output targets ES modules, CommonJS, and browser globals, with TypeScript declarations. The authors note it is faster and smaller than a JavaScript BigDecimal port while remaining broader than the companion big.js library. Optional cryptographic pseudo-random generation and bases from 2 to 36 are supported.
Documentation and tests ship with the package for Node and browser environments. Related decimal.js extends capabilities such as non-integer powers and significant-digit rounding if those constraints matter.
Key features
- Immutable arbitrary-precision decimals
- Configurable decimal places and rounding
- Fraction and square root helpers
- Bases 2 through 36 parsing
- ESM, CommonJS, and browser builds
- TypeScript declaration files
- toFormat for localization
- Independent constructor configurations
Use cases
- Financial calculations without float error
- Cryptographic-quality random BigNumbers
- High-precision division and roots
- String-safe literal construction
- Cross-environment decimal formatting