I am trying to get the balance of eth from Metamask account, but I get the following error in the browser console:

Uncaught Error: [number-to-bn] while converting number "0.322778986" to BN.js instance, error: invalid number value. Value must be an integer, hex string, BN or BigNumber instance. Note, decimals are not supported.

This is the code for retrieving the eth value :

  Balance: {this.props.ethBalance ? window.web3.utils.fromWei(this.props.ethBalance.toString(), 'ether') : ''}

How can I convert it to BN in order to display the value? Any help would be appreciated!

1 Answer 1


web3.eth.getBalance() returns a Promise which resolves to a string for balance of given address in wei.

If you want to get the balance in wei, the following code should work.

const ethBalance = await web3.eth.getBalance(this.state.account);

If you want to display the balance in ether, you should convert wei to ether using a big number library. Luckily web3 has a function for this purpose.

const balanceInWei = await web3.eth.getBalance(this.state.account);
const ethBalance = web3.utils.fromWei(balanceInWei, "ether");

Since web3.utils.fromWei(number [, unit]) returns a string if given number parameter is String or Number, you don't need to convert to big number to display it.

When displaying the balance in ethers, you don't need to call web3.utils.fromWei(number [, unit]) again. Following code should display the balance in ethers.

Balance: {{ this.props.ethBalance || "" }}
  • Thank you for your answer.I tried your suggestion but unfortunately I still get the same error as before saying that it is an invalid number value.
    – freezy
    Jun 3, 2021 at 17:25
  • This is probably because 'somehow' web3.eth.getBalance() returns the balance in ether. Also BN.js (which web3 uses internally) does not support for decimal numbers. Is it still gives error without converting to BN? Jun 3, 2021 at 17:33
  • Ok, I spoke too soon. Apparently it took some time for the browser console to refresh, therefore I thought the error was gone. I still get the error with or without converting it.
    – freezy
    Jun 3, 2021 at 17:43
  • The code I posted works correctly in my computer. Is there any chance this error happens somewhere else in your code? I mean, there should be another place where you try to convert "0.322778986" to BN. Jun 3, 2021 at 17:54
  • 1
    Awesome! You're right. Now the error is gone. Please write this as an answer and thank you so much for your help!
    – freezy
    Jun 3, 2021 at 18:15

Your Answer

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

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