TransWikia.com

How to use erc20 token in integer, not decimal

Ethereum Asked by gnxvw on August 26, 2021

I want to use number as normal integer, not in decimal.

For example, when I use uniswap function IUniswapV2Router02Instance.methods.swapETHForExactTokens(1, , , ).send() in my project, an argument integer (1) is recognized as decimal (0.000000000000000001) .

This is the transaction on etherscan.
https://ropsten.etherscan.io/tx/0x14d16174a7b12c1a77356b60b1fca7385c7cad583991d70b8a4423faf6198e43

Does anyone know why this problem happens and how to solve, please?

One Answer

In wallets and other Dapp interfaces different tokens and assets are displayed as having decimals. Since the EVM doesn't really support normal decimal datatypes such as floats for example what most ERC20 tokens do is they use integers and instead define a number of decimals (usually 18).

So when you receive 1 BAT for example the smart contract will really register it as you having 1 * 10^18 BAT just that the wallet displays it as 1 since the BAT smart contract defines its decimals as being 18. The same goes for Ether.

With Ether the base unit even has a name: wei, which is equivalent to 1 * 10^-18 ETH or 0.000000000000000001 ETH. So on a low level when you send tokens or ETH using code it treats the value as if it were in the base unit, in the case of ETH wei so when you send 1 unit of ether you are really sending 1 wei. Now 1 wei displayed in your wallet appears as a tiny decimal fraction. Hope that cleared things up!

Correct answer by MrClottom on August 26, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP