What is the best way to deal with decimals as I create a currency?

Let's use an example. I have KARLCOIN (KARL), and I have 1,000,000 of them, and the token has 6 decimals places. This token will also have a crowdfund where 100 KARL = 1 ETH ( 0.01 ETH / KARL).

When I create this contract, I should do something like:

// Constructor
 function token() {
    owner = msg.sender;
   totalSupply = 1000000;
  balances[owner] = totalSupply;
  decimals = 6;
  symbol = "KARL";

And then that token also has a function to transfer.

Now, how am I calculating, transferring, and displaying these amounts? For instance, 1,000,000 KARL is 1,000,000,000,000 of the smallest unit of my coin. Let's call it microKarl, or mk.

Does that mean the totalSupply in my constructor should actually be 1,000,000,000,000? When I transfer amounts to people, am I transferring it in mK? Basically the the amount of Karl * 1,000,000 to get it in to it's microKarl form? How will MyEtherWallet and MetMask and such know how to display the correct amount?

If I was using web3 to display it, I could just do the math and display it myself. Get the amount in mK, times it by 0.000001, and then be on my way. Right? Or am I off track here?

I made a token and did something like this (it's been a long night) and sent myself the tokens and in MetaMask it says the correct amount. On Etherscan it does not, it shows a much smaller amount, and also says the decimals are "18" when MetaMask grabbed them as 6 just by pasting the contract address in.

I'm just a bit confused on the decimalization of tokens and feel like I don't have the foundation of what I'm doing to debug this. Any clarity would be appreciated.


If your token follows ERC20 standard (and it should if you want it to be handled by wallets or client applications) you can include (it is not mandatory but handled by most clients) decimals information.

So your contract should contain something like:

uint8 public constant decimals = 6;

And your code should assume the amounts are in the smallest possible units.

  • Yes, I have the decimals. But on MetaMask, if I put my Token contract ID, it loads the decimals automatically as "6". On Etherscan, it says "18", and thus displays an incorrect balance of the tokens. I believe I've done the rest correctly... do I need to Verify Contract on Etherscan for it to read the decimals correctly? Jul 7 '17 at 16:57
  • Can you show the declaration of decimals in your contract? Jul 7 '17 at 17:07
  • uint8 public decimals = 0; function token() payable { owner = msg.sender; _totalSupply = 1000000000000; balances[owner] = _totalSupply; decimals = 6; symbol = "KARL"; } And MetaMask can read this just fine and shows the value correctly. Jul 7 '17 at 17:17
  • the contract is deployed here with 18 decimals at etherscan and 6 in metamask: ropsten.etherscan.io/token/… Jul 7 '17 at 17:28
  • Well... I've clicked on the link you have provided and it shows Token Decimals: 6 for me. Jul 7 '17 at 18:26

Use it as follows:

uint256 public constant decimals = 6;
uint256 public constant totalSupply = 1000000*10**decimals;
  • 1
    welcome to the community :), make sure you explain your answer a bit, for more readability and understand-ability :) Sep 27 '17 at 11:47
  • in ethereum blockchain the total supply was multiplied by 10^-nbr of decimal that is why in your smart contract you should multiplied it by 10^6 to get your number exact of token
    – riadh zar
    Sep 28 '17 at 8:47

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.