Comment on page
BigNumber
Many operations in Hedera operate on numbers which are outside the range of safe values to use in JavaScript.
A BigNumber is an object which safely allows mathematical operations on numbers of any magnitude.
Most operations which need to return a value will return a BigNumber and parameters which accept values will generally accept them.
The BigNumber class is directly imported from the The Ethers Project. The complete documentation can be found in the official ethers docs.
Many functions and methods in this library take in values which can be non-ambiguously and safely converted to a BigNumber. These values can be specified as:
BigNumber - An existing BigNumber instance.
The constructor of BigNumber cannot be called directly. Instead, Use the static
BigNumber.from
.hethers.BigNumber.from( aBigNumberish ) ⇒ BigNumber
Returns an instance of a BigNumber for aBigNumberish.
// From a decimal string...
BigNumber.from("42")
// { BigNumber: "42" }
// From a HexString...
BigNumber.from("0x2a")
// { BigNumber: "42" }
// From a negative HexString...
BigNumber.from("-0x2a")
// { BigNumber: "-42" }
// From an Array (or Uint8Array)...
BigNumber.from([ 42 ])
// { BigNumber: "42" }
// From an existing BigNumber...
let one1 = constants.One;
let one2 = BigNumber.from(one1)
one2
// { BigNumber: "1" }
// ...which returns the same instance
one1 === one2
// true
// From a (safe) number...
BigNumber.from(42)
// { BigNumber: "42" }
// From a ES2015 BigInt... (only on platforms with BigInt support)
BigNumber.from(42n)
// { BigNumber: "42" }
// Numbers outside the safe range fail:
BigNumber.from(Number.MAX_SAFE_INTEGER);
// [Error: overflow] {
// code: 'NUMERIC_FAULT',
// fault: 'overflow',
// operation: 'BigNumber.from',
// reason: 'overflow',
// value: 9007199254740991
// }
The BigNumber class is immutable, so no operations can change the value it represents.
Returns a BigNumber with the value of BigNumber + otherValue.
Returns a BigNumber with the value of BigNumber - otherValue.
Returns a BigNumber with the value of BigNumber × otherValue.
Returns a BigNumber with the value of BigNumber ÷ divisor.
Returns a BigNumber with the value of the remainder of BigNumber ÷ divisor.
Returns a BigNumber with the value of BigNumber to the power of exponent.
Returns a BigNumber with the absolute value of BigNumber.
Returns a BigNumber with the value of BigNumber with bits beyond the bitcount least significant bits set to zero.
Two's Complement is an elegant method used to encode and decode fixed-width signed values while efficiently preserving mathematical operations. Most users will not need to interact with these.
Returns a BigNumber with the value of BigNumber converted from twos-complement with bitwidth.
Returns a BigNumber with the value of BigNumber converted to twos-complement with bitwidth.
Returns true if and only if the value of BigNumber is equal to otherValue.
Returns true if and only if the value of BigNumber < otherValue.
Returns true if and only if the value of BigNumber ≤ otherValue.
Returns true if and only if the value of BigNumber > otherValue.
Returns true if and only if the value of BigNumber ≥ otherValue.
Returns true if and only if the value of BigNumber is zero.
Returns the value of BigNumber as a JavaScript value.
This will throw an error if the value is greater than or equal to Number.MAX_SAFE_INTEGER or less than or equal to Number.MIN_SAFE_INTEGER.
Returns the value of BigNumber as a base-10 string.
Returns true if and only if the object is a BigNumber object.
let a = BigNumber.from(42);
let b = BigNumber.from("91");
a.mul(b);
// { BigNumber: "3822" }
Last modified 1yr ago