1
const { ethers } = require('ethers');
console.log(ethers.BigNumber.isBigNumber(totalBalance));

Returns true

const { ethers } = require('ethers');
console.log(ethers.BigNumber.toNumber(totalBalance));

ethers.BigNumber.toNumber is not a function

According to the docs

https://docs.ethers.io/v5/api/utils/bignumber/#BigNumber

toNumber() is a function on BigNumber.

Are the docs wrong or is maybe toNumber called wrong?

2 Answers 2

0

The method isBigNumber is a static method of class ethers.BigNumber.

As you can read here:

The static keyword defines a static method or property for a class. Neither static methods nor static properties can be called on instances of the class. Instead, they're called on the class itself.


The method toNumber, on the other hand, is not a static method of class ethers.BigNumber.

Therefore, it should be called on instances of the class rather than on the class itself.

So in short, you need to change this:

ethers.BigNumber.toNumber(totalBalance)

To this:

ethers.BigNumber(totalBalance).toNumber()
0

if the totalBalance is the bignumber or the value format seem like this..

BigNumber {_hex: '0x02', _isBigNumber: true}

you just add toNumber after totalBalance

totalBalance.toNumber() // result = 2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.