A JavaScript library for arbitrary-precision decimal and non-decimal arithmetic.
The library is the single JavaScript file bignumber.umd.js (or minified, bignumber.umd.min.js).
Browser:
<script src='path/to/bignumber.js'></script>
$ npm install @waves/bignumber
const { BigNumber } = require('@waves/bignumber');
ES6 module:
import { BigNumber } from "@waves/bignumber"
AMD loader libraries such as requireJS:
require(['@waves/bignumber'], function(BigNumber) {
// Use BigNumber here in local scope. No global BigNumber.
});
let x = new BigNumber(123.4567);
let y = BigNumber('123456.7e-3');
let z = new BigNumber(x);
x.eq(y) && y.eq(z) && x.eq(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.
let x = new BigNumber('1111222233334444555566');
x.toString(); // "1111222233334444555566"
x.toFixed(); // "1111222233334444555566"
ΠΠ»ΠΎΠ½ΠΈΡΡΠ΅Ρ ΠΎΠ±ΡΠ΅ΠΊΡ
const some = new BigNumber(1);
const clone = some.clone();
ΠΡΠΏΠΎΠ»Π½ΡΠ΅Ρ ΡΠ»ΠΎΠΆΠ΅Π½ΠΈΠ΅
const bigNum = new BigNumber('100');
const result = bigNum.add('50'); // with method toFixed '150'
ΠΡΡΠΈΡΠ°Π½ΠΈΠ΅
const bigNum = new BigNumber('100');
const result = bigNum.sub('50'); // with method toFixed '50'
Π£ΠΌΠ½ΠΎΠΆΠ΅Π½ΠΈΠ΅
const bigNum = new BigNumber('100');
const result = bigNum.mul(2); // with method toFixed '200'
ΠΠ΅Π»Π΅Π½ΠΈΠ΅
const bigNum = new BigNumber('100');
const result = bigNum.div(2); // with method toFixed '50'
ΠΠΎΠ·Π²Π΅Π΄Π΅Π½ΠΈΠ΅ Π² ΡΡΠ΅ΠΏΠ΅Π½Ρ
const bigNum = new BigNumber('100');
const result = bigNum.pow(2); // with method toFixed '10000'
ΠΠ²Π°Π΄ΡΠ°ΡΠ½ΡΠΉ ΠΊΠΎΡΠ΅Π½Ρ
const bigNum = new BigNumber('100');
const result = bigNum.sqrt(); // with method toFixed '10'
ΠΠΎΠ΄ΡΠ»Ρ
const bigNum = new BigNumber('-100');
const result = bigNum.abs(); // with method toFixed 100
ΠΡΡΠ°ΡΠΎΠΊ ΠΎΡ Π΄Π΅Π»Π΅Π½ΠΈΡ
const bigNum = new BigNumber('100');
const result = bigNum.mod(10); // with method toFixed '0'
ΠΠΊΡΡΠ³Π»ΡΠ΅Ρ. ΠΡΠΈΠ½ΠΈΠΌΠ°Π΅Ρ ΠΊΠΎΠ»ΠΈΡΠ΅ΡΡΠ²ΠΎ Π·Π½Π°ΠΊΠΎΠ² ΠΏΠΎΡΠ»Π΅ Π·Π°ΠΏΡΡΠΎΠΉ ΠΏΠΎΡΠ»Π΅ ΠΎΠΊΡΡΠ³Π»Π΅Π½ΠΈ ΠΈ ΡΠ΅ΠΆΠΈΠΌ ΠΎΠΊΡΡΠ³Π»Π΅Π½ΠΈΡ ΡΠΌ ΡΡΡ: http://mikemcl.github.io/bignumber.js/#constructor-properties
const bigNum = new BigNumber('100');
const result = bigNum.roundTo(); //
Π Π°Π²Π΅Π½ΡΡΠ²ΠΎ
const bigNum = new BigNumber('100');
const result = bigNum.eq(100); // true
ΠΠ΅Π½ΡΡΠ΅
const bigNum = new BigNumber('100');
const result = bigNum.lt(); //
ΠΠΎΠ»ΡΡΠ΅
const bigNum = new BigNumber('100');
const result = bigNum.gt(); //
ΠΠ΅Π½ΡΡΠ΅ ΠΈΠ»ΠΈ ΡΠ°Π²Π½ΠΎ
const bigNum = new BigNumber('100');
const result = bigNum.lte(); //
ΠΠΎΠ»ΡΡΠ΅ ΠΈΠ»ΠΈ ΡΠ°Π²Π½ΠΎ
const bigNum = new BigNumber('100');
const result = bigNum.gte(); //
ΠΡΠΎΠ²Π΅ΡΡΠ΅Ρ Π½Π° NaN
const bigNum = new BigNumber('100');
const result = bigNum.isNaN(); // false
ΠΡΠΎΠ²Π΅ΡΡΠ΅Ρ Π½Π° Infinity (ΠΏΠΎΠ»ΠΎΠΆΠΈΡΠ΅Π»ΡΠ½ΡΠΉ ΠΈ ΠΎΡΡΠΈΡΠ°ΡΠ΅Π»ΡΠ½ΡΠΉ)
const bigNum = new BigNumber('100');
const result = bigNum.isFinite(); //
ΠΡΠΎΠ²Π΅ΡΡΠ΅Ρ Π½Π° ΡΠ°Π²Π΅Π½ΡΡΠ²ΠΎ Π½ΡΠ»Ρ
const bigNum = new BigNumber('100');
const result = bigNum.isZero(); // false
ΠΠΎΠ»ΡΡΠ΅ Π½ΡΠ»Ρ
const bigNum = new BigNumber('100');
const result = bigNum.isPositive(); // true
ΠΠ΅Π½ΡΡΠ΅ Π½ΡΠ»Ρ
const bigNum = new BigNumber('100');
const result = bigNum.isNegative(); // false
ΠΡΠΎΠ²Π΅ΡΡΠ΅Ρ ΡΠ΅Π»ΠΎΠ΅ Π»ΠΈ ΡΠΈΡΠ»ΠΎ
const bigNum = new BigNumber('100');
const result = bigNum.isInt(); //
ΠΠΎΠ»ΡΡΠ°Π΅ΠΌ ΠΊΠΎΠ»ΠΈΡΠ΅ΡΡΠ²ΠΎ Π·Π°Π½ΠΊΠΎΠ² ΠΏΠΎΡΠ»Π΅ Π·Π°ΠΏΡΡΠΎΠΉ Ρ ΡΠΈΡΠ»Π°
const bigNum = new BigNumber('100');
const result = bigNum.getDecimalsCount(); // 0
Π§Π΅ΡΠ½ΠΎΠ΅
const bigNum = new BigNumber('100');
const result = bigNum.isEven(); // true
ΠΠ΅ ΡΡΡΠ½ΠΎΠ΅
const bigNum = new BigNumber('100');
const result = bigNum.isOdd(); // false
ΠΠ΅ΡΠ΅Π²ΠΎΠ΄ΠΈΠΌ ΡΠΈΡΠ»ΠΎ Π² Π±Π°ΠΉΡΡ ΡΠΎ Π·Π½Π°ΠΊΠΎΠΌ (8 Π±Π°ΠΉΡ). Π Π°Π±ΠΎΡΠ°Π΅Ρ ΡΠΎΠ»ΡΠΊΠΎ Ρ ΡΠ΅Π»ΡΠΌΠΈ ΡΠΈΡΠ»Π°ΠΌΠΈ.
const bigNum = new BigNumber('100');
const result = bigNum.toBytes();
ΠΡΠ²ΠΎΠ΄ΠΈΠΌ ΡΠΈΡΠ»ΠΎ Π² ΡΡΡΠΎΠΊΠΎΠ²ΠΎΠΌ ΡΠΊΠ²ΠΈΠ²Π°Π»Π΅Π½ΡΠ΅ c Ρ ΡΡΡΡΠΎΠΌ Π½Π°ΡΡΡΠΎΠ΅ΠΊ ΡΠΎΡΠΌΠ°ΡΠΈΡΠΎΠ²Π°Π½ΠΈΡ. ΠΠΏΡΠΈΠΎΠ½Π°Π»ΡΠ½ΠΎ ΠΏΡΠΈΠ½ΠΈΠΌΠ°Π΅Ρ ΠΊΠΎΠ»ΠΈΡΠ΅ΡΡΠ²ΠΎ Π·Π½Π°ΠΊΠΎΠ² ΠΎΠΊΡΡΠ³Π»Π΅Π½ΠΈΡ, ΡΠ΅ΠΆΠΈΠΌ ΠΎΠΊΡΡΠ³Π»Π΅Π½ΠΈΡ (ΠΊΠ°ΠΊ Π² roundTo) ΠΈ Π½Π°ΡΡΡΠΎΠΉΠΊΠΈ ΡΠΎΡΠΌΠ°ΡΠ° Π²ΡΠ²ΠΎΠ΄Π°.
const bigNum = new BigNumber('1000000.12312');
bigNum.toFormat(); // 1,000,000.12312
bigNum.toFormat(2); // 1,000,000.12
bigNum.toFormat(2); // 1,000,000.12
bigNum.toFormat(2, 0); // 1,000,000.13
bigNum.toFormat(2, 0, { groupSeparator: ' ' }); // 1 000 000.13
ΠΡΠ²ΠΎΠ΄ΠΈΠΌ ΡΠΈΡΠ»ΠΎ Π² ΡΡΡΠΎΠΊΠΎΠ²ΠΎΠΌ ΡΠΊΠ²ΠΈΠ²Π°Π»Π΅Π½ΡΠ΅. ΠΠΏΡΠΈΠΎΠ½Π°Π»ΡΠ½ΠΎ ΠΏΡΠΈΠ½ΠΈΠΌΠ°Π΅Ρ ΠΊΠΎΠ»ΠΈΡΠ΅ΡΡΠ²ΠΎ Π·Π½Π°ΠΊΠΎΠ² ΠΎΠΊΡΡΠ³Π»Π΅Π½ΠΈΡ ΠΈ ΡΠ΅ΠΆΠΈΠΌ ΠΎΠΊΡΡΠ³Π»Π΅Π½ΠΈΡ (ΠΊΠ°ΠΊ Π² roundTo)
const bigNum = new BigNumber('100');
const result = bigNum.toFixed(); //
ΠΡΠΈΠ²ΠΎΠ΄ΠΈΡ ΠΊ ΡΠΈΡΠ»Ρ
const bigNum = new BigNumber('100');
const result = bigNum.toNumber(); //
ΠΡΠ²ΠΎΠ΄ΠΈΡ Π·Π½Π°ΠΊΠΎΠ²ΠΎΠ΅ ΡΠΈΡΠ»ΠΎ ΠΈΠ· Π±Π°ΠΉΡ. Π Π°Π±ΠΎΡΠ°Π΅Ρ ΡΠΎΠ»ΡΠΊΠΎ Ρ 8 Π±Π°ΠΉΡΠ°ΠΌΠΈ.
const some = BigNumber.fromBytes(Uint8Array.from([1,2,3,4,5,6,7,8]));
ΠΡΠΈΠ½ΠΈΠΌΠ°Π΅Ρ Π»ΡΠ±ΠΎΠ΅ ΠΊΠΎΠ»ΠΈΡΠ΅ΡΡΠ²ΠΎ Π°ΡΠ³ΡΠΌΠ΅Π½ΡΠΎΠ², Π²ΡΠ±ΠΈΡΠ°Π΅Ρ Π½Π°ΠΈΠ±ΠΎΠ»ΡΡΠ΅Π΅ ΡΠΈΡΠ»ΠΎ ΠΈΠ· Π°ΡΠ³ΡΠΌΠ΅Π½ΡΠΎΠ²
BigNumber.max(1, '2', new BigNumber(4)); // with method toFixed '4'
ΠΡΠΈΠ½ΠΈΠΌΠ°Π΅Ρ Π»ΡΠ±ΠΎΠ΅ ΠΊΠΎΠ»ΠΈΡΠ΅ΡΡΠ²ΠΎ Π°ΡΠ³ΡΠΌΠ΅Π½ΡΠΎΠ², Π²ΡΠ±ΠΈΡΠ°Π΅Ρ Π½Π°ΠΈΠΌΠ΅Π½ΡΡΠ΅Π΅ ΡΠΈΡΠ»ΠΎ ΠΈΠ· Π°ΡΠ³ΡΠΌΠ΅Π½ΡΠΎΠ²
BigNumber.min(1, '2', new BigNumber(4)); // with method toFixed '1'
ΠΡΠΈΠ½ΠΈΠΌΠ°Π΅Ρ Π»ΡΠ±ΠΎΠ΅ ΠΊΠΎΠ»ΠΈΡΠ΅ΡΡΠ²ΠΎ Π°ΡΠ³ΡΠΌΠ΅Π½ΡΠΎΠ², ΡΠΊΠ»Π°Π΄ΡΠ²Π°Π΅Ρ ΡΠΈΡΠ»Π°
BigNumber.min(1, '2', new BigNumber(4)); //with method toFixed '7'