7

When I tried to transfer more then 1000 tokens, it's throwing an error like as mentioned below (I'm using web3 js 1.0.0 34 beta version).

Error: [number-to-bn] while converting number 1e+23 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+23

Find code examples which I tried.

//var tokens = amount * 1e18;
//var tokens = amount * (10 ** 18);
//var tokens = parseInt(amount) * 1e18;
//var tokens = web3.utils.toBN(amount);
//var tokens = amount * Math.pow(10, decimal)
var tokens = web3.utils.toBN(amount * 1e18) ;

Does anyone faced this issue?

10

If you are using Web3.js and your token is using the standard 1018 decimal, then you can also use web3.utils.toWei() and web3.utils.fromWei() to get to and from the big numbers you want pretty easy.

In this situation you can try this:

var amount = 1000
var tokens = web3.utils.toWei(amount.toString(), 'ether')

"1000000000000000000000"

You can then easily convert that to a BN:

var bntokens = web3.utils.toBN(tokens)
  • Thanks for the reply. when i try the above code. im getting following error. UnhandledPromiseRejectionWarning: Error: Please pass numbers as strings or BigNumber objects to avoid precision errors. code: var amount="10000000"; var token = SmartContractHelper.web3Provider.utils.toWei(parseInt(amount), 'ether'); var tokens = SmartContractHelper.web3Provider.utils.toBN(token) – BlockHasher Sep 20 '18 at 7:11
  • when i hardcoded the token amount, its working fine. when i pass it from the variable, its not working.. is anything am i missing? – BlockHasher Sep 20 '18 at 7:20
  • 1
    Check out my edit. Basically you need to use numbers represented as strings. – Shawn Tabrizi Sep 20 '18 at 7:23
  • I am glad it worked out. Feel free to mark this answer as correct so the question is correctly indexed as 'answered'. – Shawn Tabrizi Sep 20 '18 at 18:10

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.