For example when I want to send ERC-20 tokens to a contract, I specify the amount in Wei
So I have to convert 34678393 tokens to Wei. But it only works ok with tokens that have 18 decimals

I'm using web3's toWei() function like this

  const sendTokens = async (amount)  => {
    const contract = new web3.eth.Contract("ADDRESS",ABI)
    await contract.sendTokens(web3.utils.toWei(amount))

How can I convert tokens amount with different decimals to Wei?


1 Answer 1


Actually, yes, you can.

Just make sure that you send the correct weis to the token contract. The token contract should have a function to get how many decimals it uses.

If you know that the token contract uses 3 decimals, then you can convert your amount toWei passing a kwei param. If it uses 6 decimals, then mwei, etc. Check the following code and read the comments:

// 34678393_000 the last 3 zeros could be used as decimals. If your token uses 3 decimals, then use `kwei`.
const result2 = web3.utils.toWei("34678393", "kwei");
console.log("34678393 to wei (kwei): ", result2);

// 34678393_000000 the last 6 zeros could be used as decimals. If your token uses 6 decimals, then use `mwei`.
const result3 = web3.utils.toWei("34678393", "mwei");

console.log("34678393 to wei (mwei): ", result3);

// 34678393_000000000 the last 9 zeros could be used as decimals. If your token uses 9 decimals, then use `gwei`.
const result4 = web3.utils.toWei("34678393", "gwei");

console.log("34678393 to wei (gwei): ", result4);

// 34678393_000000000000 the last 12 zeros could be used as decimals. If your token uses 12 decimals, then use `szabo`.
const result5 = web3.utils.toWei("34678393", "szabo");

console.log("34678393 to wei (szabo): ", result5);

// 34678393_000000000000000 the last 15 zeros could be used as decimals. If your token uses 15 decimals, then use `finney`.
const result6 = web3.utils.toWei("34678393", "finney");

console.log("34678393 to wei (finney): ", result6);

// 34678393_000000000000000000 the last 18 zeros are the decimals
const result = web3.utils.toWei("34678393");

console.log("34678393 to wei (ether): ", result);

// 34678393_000000000000000000000 the last 21 zeros could be used as decimals. If your token uses 21 decimals, then use `kether`.
const result7 = web3.utils.toWei("34678393", "kether");

console.log("34678393 to wei (kether): ", result7);

// Use `mether` for 25 decimals
// Use `gether` fo 26 28 decimals
// Use tether for 31 decimals

// For other decimals, you will probably need to implement your own logic or find a library that handles other decimals.

It prints something like:

34678393 to wei (kwei):  34678393000
34678393 to wei (mwei):  34678393000000
34678393 to wei (gwei):  34678393000000000
34678393 to wei (szabo):  34678393000000000000
34678393 to wei (finney):  34678393000000000000000
34678393 to wei (ether):  34678393000000000000000000
34678393 to wei (kether):  34678393000000000000000000000

Here is the docs: https://web3js.readthedocs.io/en/v1.7.5/web3-utils.html?highlight=toWei#towei

If you are calling a token that uses 3 decimals, then the last 3 digits of the number you send will be treated as the decimals.

So, if you want to send 34678393 tokens with no decimals, then you need to append 3 zeros, because if you don't, then the token will treat the number 34678393 as 34678.393. Which may not be what you want.

That's you you need to either append the 3 zeros or use the web3.utils.toWei("34678393", "kwei") function, so it returns 34678393000 and we can send it to the token, indicating that the amount does not have any decimal values.

  • 1
    Thanks that's a great answer. I have a question though, What about tokens that have different decimals like 5 or one and 7? according to this answer ethereum.stackexchange.com/questions/118896/…. we could use any decimals as long as it is less than 77
    – Yaya
    Commented Sep 13, 2022 at 21:05
  • 1
    Simple add 5 ir 7 zeros at the end of it. Commented Sep 13, 2022 at 21:11
  • But what about double values like 0.3?
    – Yaya
    Commented Sep 13, 2022 at 21:23
  • When I add zeros according to decimals in 0.3, if I'm not wrong it throws an error like wrong input value
    – Yaya
    Commented Sep 13, 2022 at 21:25

Your Answer

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

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