If you look at this article, the total tokens for crowdsale is 100 tokens and defined in the code as

uint256 public maxTokens = 100000000000000000000; 

how come 100000000000000000000 becomes 100 tokens?


Because Solidity only supports integer numbers, there's no natural way to represent a value like "half a token" (0.5 tokens). To work around this, ERC20 tokens use a multiplier. For US dollars, you might use a multiplier of 100, or 10^2, for 2 decimal places. In code, you'd store the number of pennies rather than the number of dollars. So $1 becomes 100 pennies, and $100 becomes 10,000 pennies. Now, half a dollar is just 50.

ERC20 tokens have a value called decimals which says what power of 10 you should multiply/divide things by. The ERC20 token you're looking at, like most tokens, uses decimals = 18, so everything is multiplied by 10^18 (a 1 with 18 zeros after it). So to represent 100 tokens, you use the value 100 * 10^18 == 10^20 == a 1 with 20 zeros after it, which is the number you see.

It's the responsibility of UI—e.g. tools like Etherscan, MetaMask, and MyEtherWallet—to take decimals into account and multiply and divide values as needed. So if your balance of an ERC20 token with 18 decimal places is 1000000000000000000, Etherscan will tell you you have 1 token. And if you ask MyEtherWallet to transfer 0.5 tokens, it will transfer the amount 500000000000000000.

  • Does decimals = 18 mean the token value can be allowed up to 18 decimal places? Ex: 0.218392749283928378 – bbusdriver Jun 16 '19 at 19:33
  • 1
    Yes, because the value 1 would be 1/10^18 of a token. – user19510 Jun 16 '19 at 20:01
  • From your answer, 10 * 10^18 isn't it supposed to be 100 * 10^18? – bbusdriver Jun 16 '19 at 20:06
  • Yes, thank you! I edited the answer. – user19510 Jun 16 '19 at 20:22
  • OpenZeppelin documentation includes a note on decimals – abcoathup Jul 8 '19 at 9:28

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.