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?
Ethereum Stack Exchange is a question and answer site for users of Ethereum, the decentralized application platform and smart contract enabled blockchain. It only takes a minute to sign up.Sign up to join this community
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.