Gathering detailed insights and metrics for bignumber.js
Gathering detailed insights and metrics for bignumber.js
Gathering detailed insights and metrics for bignumber.js
Gathering detailed insights and metrics for bignumber.js
number-to-bn
A simple method that will convert numbers, hex, BN or bignumber.js object into a BN.js object.
chai-bn
Chai assertions for comparing arbitrary-precision integers using the bignumber.js library
chai-bignumber
Chai assertions for comparing arbitrary-precision decimals using the bignumber.js library
json-bignumber
JSONBigNumber.parse/stringify handling **all** JSON numbers using BigNumber. Based on Douglas Crockford [JSON.js](https://github.com/douglascrockford/JSON-js) package and [bignumber.js](https://github.com/MikeMcl/bignumber.js) library.
A JavaScript library for arbitrary-precision decimal and non-decimal arithmetic
npm install bignumber.js
100
Supply Chain
99.6
Quality
76.1
Maintenance
100
Vulnerability
100
License
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
6,709 Stars
256 Commits
741 Forks
83 Watching
6 Branches
35 Contributors
Updated on 27 Nov 2024
JavaScript (98.91%)
HTML (1.06%)
Java (0.03%)
Cumulative downloads
Total Downloads
Last day
-10%
2,305,364
Compared to previous day
Last week
3.1%
13,822,969
Compared to previous week
Last month
7.8%
57,662,454
Compared to previous month
Last year
22.9%
561,906,437
Compared to previous year
A JavaScript library for arbitrary-precision decimal and non-decimal arithmetic.
toExponential
, toFixed
, toPrecision
and toString
methods of JavaScript's Number typetoFraction
and a correctly-rounded squareRoot
methodIf a smaller and simpler library is required see big.js.
It's less than half the size but only works with decimal numbers and only has half the methods.
It also has fewer configuration options than this library, and does not allow NaN
or Infinity
.
See also decimal.js, which among other things adds support for non-integer powers, and performs all operations to a specified number of significant digits.
The library is the single JavaScript file bignumber.js or ES module bignumber.mjs.
1<script src='path/to/bignumber.js'></script>
ES module
1<script type="module"> 2import BigNumber from './path/to/bignumber.mjs';
Get a minified version from a CDN:
1<script src='https://cdn.jsdelivr.net/npm/bignumber.js@9.1.2/bignumber.min.js'></script>
1npm install bignumber.js
1const BigNumber = require('bignumber.js');
ES module
1import BigNumber from "bignumber.js"; 2import { BigNumber } from "./node_modules/bignumber.js/bignumber.mjs";
1import BigNumber from 'https://raw.githubusercontent.com/mikemcl/bignumber.js/v9.1.2/bignumber.mjs'; 2import BigNumber from 'https://unpkg.com/bignumber.js@latest/bignumber.mjs';
The library exports a single constructor function, BigNumber
, which accepts a value of type Number, String or BigNumber,
1let x = new BigNumber(123.4567); 2let y = BigNumber('123456.7e-3'); 3let z = new BigNumber(x); 4x.isEqualTo(y) && y.isEqualTo(z) && x.isEqualTo(z); // true
To get the string value of a BigNumber use toString()
or toFixed()
. Using toFixed()
prevents exponential notation being returned, no matter how large or small the value.
1let x = new BigNumber('1111222233334444555566'); 2x.toString(); // "1.111222233334444555566e+21" 3x.toFixed(); // "1111222233334444555566"
If the limited precision of Number values is not well understood, it is recommended to create BigNumbers from String values rather than Number values to avoid a potential loss of precision.
In all further examples below, let
, semicolons and toString
calls are not shown. If a commented-out value is in quotes it means toString
has been called on the preceding expression.
1// Precision loss from using numeric literals with more than 15 significant digits. 2new BigNumber(1.0000000000000001) // '1' 3new BigNumber(88259496234518.57) // '88259496234518.56' 4new BigNumber(99999999999999999999) // '100000000000000000000' 5 6// Precision loss from using numeric literals outside the range of Number values. 7new BigNumber(2e+308) // 'Infinity' 8new BigNumber(1e-324) // '0' 9 10// Precision loss from the unexpected result of arithmetic with Number values. 11new BigNumber(0.7 + 0.1) // '0.7999999999999999'
When creating a BigNumber from a Number, note that a BigNumber is created from a Number's decimal toString()
value not from its underlying binary value. If the latter is required, then pass the Number's toString(2)
value and specify base 2.
1new BigNumber(Number.MAX_VALUE.toString(2), 2)
BigNumbers can be created from values in bases from 2 to 36. See ALPHABET
to extend this range.
1a = new BigNumber(1011, 2) // "11" 2b = new BigNumber('zz.9', 36) // "1295.25" 3c = a.plus(b) // "1306.25"
Performance is better if base 10 is NOT specified for decimal values. Only specify base 10 when you want to limit the number of decimal places of the input value to the current DECIMAL_PLACES
setting.
A BigNumber is immutable in the sense that it is not changed by its methods.
10.3 - 0.1 // 0.19999999999999998 2x = new BigNumber(0.3) 3x.minus(0.1) // "0.2" 4x // "0.3"
The methods that return a BigNumber can be chained.
1x.dividedBy(y).plus(z).times(9) 2x.times('1.23456780123456789e+9').plus(9876.5432321).dividedBy('4444562598.111772').integerValue()
Some of the longer method names have a shorter alias.
1x.squareRoot().dividedBy(y).exponentiatedBy(3).isEqualTo(x.sqrt().div(y).pow(3)) // true 2x.modulo(y).multipliedBy(z).eq(x.mod(y).times(z)) // true
As with JavaScript's Number type, there are toExponential
, toFixed
and toPrecision
methods.
1x = new BigNumber(255.5) 2x.toExponential(5) // "2.55500e+2" 3x.toFixed(5) // "255.50000" 4x.toPrecision(5) // "255.50" 5x.toNumber() // 255.5
A base can be specified for toString
.
Performance is better if base 10 is NOT specified, i.e. use toString()
not toString(10)
. Only specify base 10 when you want to limit the number of decimal places of the string to the current DECIMAL_PLACES
setting.
1x.toString(16) // "ff.8"
There is a toFormat
method which may be useful for internationalisation.
1y = new BigNumber('1234567.898765') 2y.toFormat(2) // "1,234,567.90"
The maximum number of decimal places of the result of an operation involving division (i.e. a division, square root, base conversion or negative power operation) is set using the set
or config
method of the BigNumber
constructor.
The other arithmetic operations always give the exact result.
1BigNumber.set({ DECIMAL_PLACES: 10, ROUNDING_MODE: 4 }) 2 3x = new BigNumber(2) 4y = new BigNumber(3) 5z = x.dividedBy(y) // "0.6666666667" 6z.squareRoot() // "0.8164965809" 7z.exponentiatedBy(-3) // "3.3749999995" 8z.toString(2) // "0.1010101011" 9z.multipliedBy(z) // "0.44444444448888888889" 10z.multipliedBy(z).decimalPlaces(10) // "0.4444444445"
There is a toFraction
method with an optional maximum denominator argument
1y = new BigNumber(355) 2pi = y.dividedBy(113) // "3.1415929204" 3pi.toFraction() // [ "7853982301", "2500000000" ] 4pi.toFraction(1000) // [ "355", "113" ]
and isNaN
and isFinite
methods, as NaN
and Infinity
are valid BigNumber
values.
1x = new BigNumber(NaN) // "NaN" 2y = new BigNumber(Infinity) // "Infinity" 3x.isNaN() && !y.isNaN() && !x.isFinite() && !y.isFinite() // true
The value of a BigNumber is stored in a decimal floating point format in terms of a coefficient, exponent and sign.
1x = new BigNumber(-123.456); 2x.c // [ 123, 45600000000000 ] coefficient (i.e. significand) 3x.e // 2 exponent 4x.s // -1 sign
For advanced usage, multiple BigNumber constructors can be created, each with its own independent configuration.
1// Set DECIMAL_PLACES for the original BigNumber constructor 2BigNumber.set({ DECIMAL_PLACES: 10 }) 3 4// Create another BigNumber constructor, optionally passing in a configuration object 5BN = BigNumber.clone({ DECIMAL_PLACES: 5 }) 6 7x = new BigNumber(1) 8y = new BN(1) 9 10x.div(3) // '0.3333333333' 11y.div(3) // '0.33333'
To avoid having to call toString
or valueOf
on a BigNumber to get its value in the Node.js REPL or when using console.log
use
1BigNumber.prototype[require('util').inspect.custom] = BigNumber.prototype.valueOf;
For further information see the API reference in the doc directory.
The test/modules directory contains the test scripts for each method.
The tests can be run with Node.js or a browser. For Node.js use
1npm test
or
1node test/test
To test a single method, use, for example
1node test/methods/toFraction
For the browser, open test/test.html.
To minify using, for example, terser
1npm install -g terser
1terser big.js -c -m -o big.min.js
The MIT Licence.
See LICENCE.
No vulnerabilities found.
Reason
0 existing vulnerabilities detected
Reason
license file detected
Details
Reason
binaries present in source code
Details
Reason
2 commit(s) and 2 issue activity found in the last 90 days -- score normalized to 3
Reason
Found 7/30 approved changesets -- score normalized to 2
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
project is not fuzzed
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Score
Last Scanned on 2024-11-25
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More