Understanding decimals for ERC20 token creation?

Hi @manolingam,

Please see the OpenZeppelin documentation on decimals which should explain the math:
https://docs.openzeppelin.com/contracts/2.x/tokens#a-note-on-decimals

TLDR: Decimals are for display purposes, calculations are done in the base units for a token. So you need 10000 * 10 ** 18 (token base units) for 10000 tokens.

See an example below:

SimpleToken

import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
import "@openzeppelin/contracts/token/ERC20/ERC20Detailed.sol";

/**
 * @title SimpleToken
 * @dev Very simple ERC20 Token example, where all tokens are pre-assigned to the creator.
 * Note they can later distribute these tokens as they wish using `transfer` and other
 * `ERC20` functions.
 */
contract SimpleToken is Context, ERC20, ERC20Detailed {

    /**
     * @dev Constructor that gives _msgSender() all of existing tokens.
     */
    constructor () public ERC20Detailed("SimpleToken", "SIM", 18) {
        _mint(_msgSender(), 10000 * (10 ** uint256(decimals())));
    }
}
3 Likes