[Ethereum] Decimals on ERC20 Tokens

contract-designcontract-developmenterc-20tokens

I'm having an odd problem working with some ERC20 token contracts I've written:

When I specify a decimal value, let's say 4 here for simplicity, the values show up differently in Mist vs etherscan.

For example, if I create a token with a supply of 1 with decimals of 4, the tokens show up as a value of 0.0001 in Mist but the value shows up as 1 in etherscan; however, if I click on the ERC20 icon in etherscan to view details on the token, there it agrees with Mist and states that the token supply is 0.0001.

So, I understand that the contract is going to be working with the smallest unit of the token, as set by the decimals value in the constructor; but I'm not quite sure how to get the value of the token itself, rather than the smaller units, to display in Mist and etherscan. Let me know if that doesn't make sense.

Best Answer

The way I think about this is that the number of tokens is:

tokenSupply = tokensIActuallyWant * (10 ** decimals)

If I want 1 token with the ability to subdivide it with a precision of 2 decimal places, I represent that as 100.

Similarly if I want 1 token with the ability to subdivide that to 18 decimal places, I need to represent that as 1000000000000000000 (1 with 18 zeros).

Hope that helps.

Related Topic