bignumber.js
Arbitrary-precision decimal arithmetic library for JavaScript, avoiding the rounding errors of binary floating point.
Official Documentation ↗v11.1.5. Check the official docs for the latest API surface.Installation
npm install bignumber.jspnpm add bignumber.jsMethods
No methods match this filter.
Overview
bignumber.js is an arbitrary-precision decimal arithmetic library that avoids binary floating point rounding errors for money and measurements. This reference lists the most frequently used APIs with accurate signatures and runnable snippets.
Core APIs
new BigNumber— Constructs a precise decimal from a string, number, base, or another BigNumber instance.plus— Adds values with configurable precision instead of binary floating point.toFixed— Formats the number to a fixed number of decimal places with the chosen rounding mode.
Example
const BigNumber = require('bignumber.js');
const price = new BigNumber('0.1');
console.log(price.toFixed(1));
Notes
Methods are immutable and return new instances. Default precision and rounding are global, so configure them once at startup.
Verification
Add 0.1 and 0.2 and confirm the result formatted to two places is 0.30 rather than a long binary fraction.
Errors
The following points are quoted verbatim from the official documentation. 来源:https://mikemcl.github.io/bignumber.js/ · 取证方式:HTTP GET 官方 README/文档页(HTML 去标签) · 抓取时间:2026-11-07T13:48:26.120Z. Any error type, throw condition or recovery step not stated there is left unspecified; this page does not infer exception classes or return values.
- Default value: true The value that determines whether an exception is thrown or a BigNumber with value NaN is returned when an invalid value is passed to the constructor or a method as the value of a BigNumber. (https://mikemcl.github.io/bignumber.js/)
- If STRICT is true then a BigNumber Error is thrown if the value is not of type BigNumber , number , string , or BigInt , or if the value is a string that is not a valid numeric value. (https://mikemcl.github.io/bignumber.js/)
- Throws if object is not an object, or if an invalid value is assigned to one or more of the above properties. (https://mikemcl.github.io/bignumber.js/)
FAQ
- Why is 0.1 + 0.2 not 0.3 in JavaScript?
- Binary floating point cannot represent those decimals exactly, so results carry small errors; bignumber.js works in decimal.
- How do I change default precision and rounding?
- Set BigNumber.config({ DECIMAL_PLACES, ROUNDING_MODE, EXPONENTIAL_AT }) globally before performing arithmetic.
- Are BigNumber methods immutable?
- Yes. Arithmetic methods return new instances, so the receiver is never mutated; assign the result.
Report a problem with this API docOpens a pre-filled issue on GitHub.