Skip to main content

Class: BigNumber

moralis-monorepo / @moralisweb3/common-core / BigNumber

Class: BigNumber

@moralisweb3/common-core.BigNumber

The BigNumber class is a MoralisData that references to a the value of a BigNumber

Table of contents

Methods

Methods

create

Static create(value): BigNumber

Create a new instance of BigNumber from any valid address input.

Example

BigNumber.create(12);

Example

BigNumber.create("20");

Parameters

NameTypeDescription
valueBigNumberInputthe BigNumberish type

Returns

BigNumber

a new BigNumber instance


fromDecimal

Static fromDecimal(value, decimals?): BigNumber

Creates a new BigNumber from given decimals.

Example

BigNumber.fromDecimal("1.23456789", 18);

Parameters

NameTypeDefault valueDescription
valueBigNumberPrimitiveundefined
decimalsnumber0This is optional and defaults to 0

Returns

BigNumber


fromJSON

Static fromJSON(json): BigNumber

Parameters

NameType
jsonstring

Returns

BigNumber


add

add(value): BigNumber

Adds a BigNumber to current BigNumber instance.

Example

BigNumber.create(12).add(7);

Example

BigNumber.create(12).add("1000000000000000000");

Parameters

NameTypeDescription
valueBigNumberInputthe BigNumberish to add

Returns

BigNumber

the result of the addition


div

div(value): BigNumber

Divides a BigNumber with current BigNumber instance.

Example

BigNumber.create(12).div(7);

Example

BigNumber.create(1).div("1000000000000000000");

Parameters

NameTypeDescription
valueBigNumberInputthe BigNumberish to divide

Returns

BigNumber

the result of the division


equals

equals(value): boolean

Checks the equality of the current BigNumber with another BigNumber.

Example

BigNumber.create(12).equals(BigNumber.create(12)); // true

Parameters

NameTypeDescription
valueBigNumberthe BigNumberish to compare

Returns

boolean

true if the BigNumbers are equal


mul

mul(value): BigNumber

Multiplies a BigNumber with current BigNumber instance.

Example

BigNumber.create(12).mul(7);

Example

BigNumber.create(12).mul("1000000000000000000");

Parameters

NameTypeDescription
valueBigNumberInputthe BigNumberish to multiply

Returns

BigNumber

the result of the multiplication


sub

sub(value): BigNumber

Subtracts a BigNumber from current BigNumber instance.

Example

BigNumber.create(12).sub(7);

Example

BigNumber.create("1000000000000000000").sub(20);

Parameters

NameTypeDescription
valueBigNumberInputthe BigNumberish to subtract

Returns

BigNumber

the result of the subtraction


toBigInt

toBigInt(): bigint

Example

BigNumber.create(12).toBigInt();

Returns

bigint

the value of this BigNumber as a BigInt


toDecimal

toDecimal(decimals): string

Converts BigNumber instance to value in given decimals.

Example

BigNumber.create(12).toDecimal(18);

Parameters

NameTypeDescription
decimalsnumberThe decimals to convert to

Returns

string


toHex

toHex(): string

Converts BigNumber instance to hex string.

Example

BigNumber.create(12).toHex();

Returns

string


toJSON

toJSON(): string

Converts BigNumber instance to hex string.

Example

BigNumber.create(12).toJSON();

Returns

string


toString

toString(): string

Converts BigNumber instance to string.

Example

BigNumber.create(12).toString();

Returns

string