I am building an ERC20 Token. I want to the decimals properly so that people can do transaction in deciamls also. So, I am trying to transfer 10^21 tokens from my address to another.

Minted tokens : 1000000 * (10**decimals) # decimals = 18

So, I should have 10^23 tokens in total.

So I should be able to transfer this amount?

I made a UI and used web3.js where I got this error.

Unhandled Rejection (Error): 
invalid number value (arg="_price", coderType="uint256", value="1e+21")

Then I also tried in Remix for the same values. there also the transaction failed.

Execution is failing for numbers like if I want to transfer 10*20 token. then also the transaction fails.

What am I doing wrong?

Thanks for the help.


You cannot use floating point numbers to interact with cryptocurrency, or any monetary units. You need to use a special helper class called BigNumber.js to do the math. Then you convert those numbers to strings before passing them to functions that go to JSON-RPC interface.

  • 1
    Yeah I am not using floating numbers. That is why I am using decimals to multiply to the floating number and convert to a proper uint. I tried the web3.utils.toBN() I got the error Error: invalid number value (arg="_price", coderType="uint256", value="1e+21") – Abhishek kumar May 27 '20 at 17:40
  • 1
    Error: [number-to-bn] while converting number 1e+21 to BN.js instance, error: invalid number value. Value must be an integer, hex string, BN or BigNumber instance. Note, decimals are not supported. Given value: "1e+21" – Abhishek kumar May 27 '20 at 17:51
  • 2
    1e+21 is clearly a floating point number in scientific format. – Mikko Ohtamaa May 27 '20 at 18:02
  • 1
    @Abhishekkumar In javascript there is a maximum safe integer, larger numbers are converted to floating point. – Ismael May 27 '20 at 19:17
  • 1
    I found the way to do it. . I have posted an answer also. – Abhishek kumar May 27 '20 at 19:18

Solved it using this: https://github.com/ethereum/web3.js/issues/2077#issuecomment-468530879

          const dec = window.web3.utils.toBN(this.props.decimals)
          const price_ =  window.web3.utils.toBN(this.Object.value*(100))
          const price ="0x"+ price_.mul(window.web3.utils.toBN(10).pow(dec)).toString("hex")

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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