# Understanding decimals for ERC20 token creation?

**URL:** <https://forum.openzeppelin.com/t/understanding-decimals-for-erc20-token-creation/1821>\
**Category:** Contracts\
**Tags:** erc20\
**Created:** [November 25, 2019, 7:57am UTC](https://forum.openzeppelin.com/t/understanding-decimals-for-erc20-token-creation/1821 "2019-11-25T07:57:56Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![manolingam](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.openzeppelin.com/manolingam/32/1472_2.png) [@manolingam](https://forum.openzeppelin.com/u/manolingam)\
**Post date:** [November 25, 2019, 7:57am UTC](https://forum.openzeppelin.com/t/understanding-decimals-for-erc20-token-creation/1821/1 "2019-11-25T07:57:57Z")

</div>

Hi people,

I am experimenting with **ERC20** tokens and want to set a supply with 18 decimals. And I couldn’t understand the math behind it. Can someone explain it please?

For example, if I wanna create a supply with **10000 Tokens** , what is the math behind it?

Should I do `10000 / (10 **18)` or `10000 * 10** 18`? And what value should I pass for the `_mint` function to create 10000 tokens?

I know the math is simple but bear with me. I am poor at calculations 😬

Thanks.

---

<div class="post-metadata">

**Author:** ![abcoathup](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.openzeppelin.com/abcoathup/32/415_2.png) [@abcoathup](https://forum.openzeppelin.com/u/abcoathup)\
**Post date:** [November 25, 2019, 8:31am UTC](https://forum.openzeppelin.com/t/understanding-decimals-for-erc20-token-creation/1821/2 "2019-11-25T08:31:59Z")

</div>

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](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

```auto
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())));
    }
}

```

---

<div class="post-metadata">

**Author:** ![manolingam](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.openzeppelin.com/manolingam/32/1472_2.png) [@manolingam](https://forum.openzeppelin.com/u/manolingam)\
**Post date:** [November 25, 2019, 8:46am UTC](https://forum.openzeppelin.com/t/understanding-decimals-for-erc20-token-creation/1821/3 "2019-11-25T08:46:16Z")

</div>

Thanks @abcoathup. Yes, I checked the documentation too.

Okay. If I wanna transfer 100 tokens, then should I call the transfer function with `100 * 10 ** 18`?

---

<div class="post-metadata">

**Author:** ![abcoathup](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.openzeppelin.com/abcoathup/32/415_2.png) [@abcoathup](https://forum.openzeppelin.com/u/abcoathup)\
**Post date:** [November 25, 2019, 8:53am UTC](https://forum.openzeppelin.com/t/understanding-decimals-for-erc20-token-creation/1821/4 "2019-11-25T08:53:28Z")

</div>

Yes. All operations in the smart contract use the token base units, so to transfer 100 tokens you transfer `100 * 10 ** 18` token base units.

---

<div class="post-metadata">

**Author:** ![manolingam](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.openzeppelin.com/manolingam/32/1472_2.png) [@manolingam](https://forum.openzeppelin.com/u/manolingam)\
**Post date:** [November 25, 2019, 10:24am UTC](https://forum.openzeppelin.com/t/understanding-decimals-for-erc20-token-creation/1821/5 "2019-11-25T10:24:17Z")

</div>

Since all operations needs to be multiplied by ten to the power of decimals, should we manually update **amount** from OpenZeppelin contracts to `amount * 10 **18` for the functions in** ERC20.sol**? or Can I send the transactions using `web3.utils.toWei(amount, 'ether')` ?

---

<div class="post-metadata">

**Author:** ![abcoathup](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.openzeppelin.com/abcoathup/32/415_2.png) [@abcoathup](https://forum.openzeppelin.com/u/abcoathup)\
**Post date:** [November 25, 2019, 9:40pm UTC](https://forum.openzeppelin.com/t/understanding-decimals-for-erc20-token-creation/1821/6 "2019-11-25T21:40:32Z")

</div>

Hi @manolingam,

We need to use amounts in token base units when we call ERC20 functions.

If we updated the amount used in the contract to be `amount * 10 ** 18` it would be the equivalent of having decimals of zero, or using the example of dollars and cents, it would be the equivalent of only using dollars.

Whilst you should be able to use `web3.utils.toWei(amount, 'ether')` it is less clear for someone reading your code, so I would suggest not doing that.

---

<div class="post-metadata">

**Author:** ![serdarpinar](https://avatars.discourse-cdn.com/v4/letter/s/e47c2d/32.png) [@serdarpinar](https://forum.openzeppelin.com/u/serdarpinar)\
**Post date:** [December 20, 2021, 2:25pm UTC](https://forum.openzeppelin.com/t/understanding-decimals-for-erc20-token-creation/1821/7 "2021-12-20T14:25:53Z")

</div>

Hi.. i have same problem..  
my erc20 contract with 18 decimals. but i transferred to another account and then im seeing 0.0001 ..

what can i do this ? i want , if i buy the 1000 tokens and then i want see 1000 tokens on my metamask.. i hope understand me

---

<div class="post-metadata">

**Author:** ![Amxx](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.openzeppelin.com/amxx/32/668_2.png) [@Amxx](https://forum.openzeppelin.com/u/Amxx)\
**Post date:** [December 22, 2021, 9:01am UTC](https://forum.openzeppelin.com/t/understanding-decimals-for-erc20-token-creation/1821/8 "2021-12-22T09:01:28Z")

</div>

@serdarpinar please read the message in the thread. Your answer is already there.

---

<div class="post-metadata">

**Author:** ![DoDzilla](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.openzeppelin.com/dodzilla/32/11642_2.png) [@DoDzilla](https://forum.openzeppelin.com/u/DoDzilla)\
**Post date:** [January 3, 2022, 7:38am UTC](https://forum.openzeppelin.com/t/understanding-decimals-for-erc20-token-creation/1821/9 "2022-01-03T07:38:02Z")

</div>

Your ERC20 contract has 18 decimals. If you want to send 1000 tokens then you should multiply 1000 with `10^18` (as described by the @abcoathup `1000 * 10^18`). So you should send `1000000000000000000000` (1000 and 18 zeros)

---

<div class="post-metadata">

**Author:** ![BeratOz01](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.openzeppelin.com/beratoz01/32/12065_2.png) [@BeratOz01](https://forum.openzeppelin.com/u/BeratOz01)\
**Post date:** [March 7, 2022, 11:55am UTC](https://forum.openzeppelin.com/t/understanding-decimals-for-erc20-token-creation/1821/10 "2022-03-07T11:55:47Z")

</div>

I need to increase allowance like 0.3 busdt. How did I need to do that guys ?

---

<div class="post-metadata">

**Author:** ![PaulRBerg](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.openzeppelin.com/paulrberg/32/14286_2.png) [@PaulRBerg](https://forum.openzeppelin.com/u/PaulRBerg)\
**Post date:** [June 12, 2022, 2:14pm UTC](https://forum.openzeppelin.com/t/understanding-decimals-for-erc20-token-creation/1821/11 "2022-06-12T14:14:47Z")

</div>

@BeratOz01 You need a fixed-point math library for that. See this:

[What fixed or float point math libraries are available in solidity?](https://ethereum.stackexchange.com/questions/83785/what-fixed-or-float-point-math-libraries-are-available-in-solidity)

I recommend using [PRBMath](https://github.com/paulrberg/prb-math) - though note that I'm biased since I'm the author of the library.
