I am trying to convert amount in wei using web3js function called toWei().

Here is my code:

 var etherwithdrawamount1=web3Infura.utils.toWei(etherwithdrawamount, 'wei'); 

Now, the problem is that the token amount is not calculating correct, because the token has 2 decimals.

In web3 documentation: https://web3js.readthedocs.io/en/1.0/web3-utils.html#towei there is no places for 2 decimals.

number - String|Number|BN: The value.

unit - String (optional, defaults to "ether"): The ether to convert from.

Possible units are :

noether: ‘0’
wei: ‘1’
kwei: ‘1000’
Kwei: ‘1000’
babbage: ‘1000’
femtoether: ‘1000’
mwei: ‘1000000’
Mwei: ‘1000000’
lovelace: ‘1000000’
picoether: ‘1000000’
gwei: ‘1000000000’
Gwei: ‘1000000000’
nanoether: ‘1000000000’
nano: ‘1000000000’
szabo: ‘1000000000000’
microether: ‘1000000000000’
micro: ‘1000000000000’
finney: ‘1000000000000000’
milliether: ‘1000000000000000’
milli: ‘1000000000000000’
ether: ‘1000000000000000000’

So, how to get the correct value in toWei() where the token decimals are 2?


Try this:

function strtodec(amount,dec){
stringf = "";
for(var i=0;i<dec;i++){
stringf = stringf+"0";
return amount+stringf;

You need to call this function using this method:

strtodec(amount,'your token decimal here')

The logic is this: The function adds zeros in the amount as many as decimals you want.

| improve this answer | |
  • 1
    what is token decimal amount or string? – Sachin Lathiya Nov 27 '18 at 12:14
  • You need to use the amount (integer) for whatever decimals you want :) – Yogesh - EtherAuthority.io Nov 27 '18 at 12:15

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.