We know that solidity doesn't support the float values. But here (https://www.ethereum.org/token#understanding-the-code) they accept one input parameter 'decimal places' for calculation purpose. How this action has been executed when this token is created using the contract written using solidity?
2 Answers
What is actually happening in sub-dividable token contracts (and ETH itself) is that the actual unit is tiny. Really, really tiny. A single ether is actually just 10^18 wei. For the sake of the users, it's shown on the various frontends as a decimal number of ETH.
The same principle applies in token contracts. Internally it's just a standard uint holding gargantuan numbers, but when displayed it's converted by the frontend into a small, human-readable number.
This is so that the token can be divided in nearly any amount, and if it suddenly needs to be divided even more then all it takes is adjusting the frontend to handle it.
Here are some examples from @Matthew's answer.
If there's a token with decimals = 2
, a user needs to have a balanceOf
100 to see 1.00 in user interfaces (such as Mist or Ethereum Wallet). If the user's balanceOf
is only 1, in the UI they will see 0.01.
Code-wise, setting decimals
to 16 is an easy way to assign 100 tokens for every 1 ETH. When a contract receives 1 ETH (which is msg.value
of 10^18), and assigns tokens to a user, the user's balanceOf
would be 10^18, but 100 would be displayed (10^18 / 10^16).
-
What about the contract side? how the total supply will be managed? suppose if 10 ether = 1 token & someone invested 15 ether, then how will it manage the issuance of tokens, because if we can only show the 1.5 on UI, it should also be reflected in contract which is not possible. Contract will issue 1 token so i can't display the wrong data.– AniketOct 14, 2016 at 7:50
-
On the contract side 15 ETH will be
balanceOf
15*10^18;decimals
will be 19. The UI will show the correct number of 1.5 (from 15*10^18 / 10^19) tokens for the user. Recall, contracts hold a gargantuan number as @Matthew mentioned.– eth ♦Oct 14, 2016 at 8:04