I want the amount of tokens to be a decimal number. but i heard that toBn is not supported decimals number.

When I put in a decimal number, I faced the following error: while converting number 0.15 to BN.js instance, error: invalid number value. Value must be an integer, hex string, BN or BigNumber instance

How can I send a decimals amount of tokens to my wallet? I am a beginner and save me. plz help!

i used code:

const val = 0.15; // this guy
let decimals = web3.utils.toBN(info.decimal);
let amount   =  web3.utils.toHex(web3.utils.toBN(val).mul(web3.utils.toBN(10).pow(decimals)));

Just don't use BN. You need to convert ETH to WEI.

This is a very simple proccess. Web3 has some buildt in functions for this:

const val = 0.15; // this guy
var weiAmount = web3.toWei(val);
  • 1
    This doesn't work for example with custom tokens that have less than 18 decimals. Ex If I want to calculate 0.2 of a BTC or 0,5 of USDC Feb 28 at 11:30

From the official documentation of BN:

Note: decimals are not supported in this library.

Here, the term decimals refers to numeric values with digits after the decimal point (i.e., non-integers).

When creating a BN object, you can pass either an integer or a string which represents an integer.

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.