bignumber.js is a JavaScript library that solves a precision problem built into the language. JavaScript's built-in number type can only represent about 15 to 17 significant decimal digits accurately, and calculations involving very large numbers, very small numbers, or repeating decimals quietly produce wrong results. This library provides a BigNumber type that can handle numbers of any size or precision without those rounding errors. The classic example is that in plain JavaScript, 0.1 plus 0.2 does not equal 0.3 due to how floating-point arithmetic works at the binary level. With bignumber.js, the same calculation returns the correct result. The library handles integers, decimals, and numbers in bases from 2 to 36, so you can work with binary, hexadecimal, or arbitrary-base values with full precision. Using the library means wrapping numbers in a BigNumber constructor rather than using JavaScript's native number literals. Once wrapped, you call methods like plus, minus, times, dividedBy, and modulo to do arithmetic. The results are also BigNumber objects, so you can chain operations. When you need a plain string or number back, you call toString or toFixed. BigNumber objects are immutable, meaning any operation returns a new value rather than changing the original. The library has no dependencies and weighs about 8 KB after minification and compression. It works in browsers, Node.js, and Deno, and ships in CommonJS, ES module, and browser-global formats. It mirrors several formatting methods from JavaScript's native Number type, including toExponential, toFixed, and toPrecision, which makes it a close drop-in for code that already uses those. The same author maintains two related libraries: big.js, which is smaller and covers only basic decimal arithmetic, and decimal.js, which adds support for things like non-integer exponents and lets you set a global precision limit for all operations.
← mikemcl on gitmyhub — every repo by this author, as a profile.
Verify against the repo before relying on details.