# ERC20 decimals, specifying a big number

**URL:** <https://forum.openzeppelin.com/t/erc20-decimals-specifying-a-big-number/2047>\
**Category:** Contracts\
**Created:** [January 7, 2020, 8:18pm UTC](https://forum.openzeppelin.com/t/erc20-decimals-specifying-a-big-number/2047 "2020-01-07T20:18:18Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![sirphemmiey](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.openzeppelin.com/sirphemmiey/32/1479_2.png) [@sirphemmiey](https://forum.openzeppelin.com/u/sirphemmiey)\
**Post date:** [January 7, 2020, 8:18pm UTC](https://forum.openzeppelin.com/t/erc20-decimals-specifying-a-big-number/2047/1 "2020-01-07T20:18:18Z")

</div>

I followed the conversation [here](https://forum.openzeppelin.com/t/understanding-decimals-for-erc20-token-creation/1821) to see if it would answer my question, but no it didn’t.

I understand that i have to use amount \*= (amount \*\* tokenDecimal) before sending transactions with ERC20 transfer method, but when i do that i get an error.

 ![Screen Shot 2020-01-08 at 12.16.58 AM](https://us1.discourse-cdn.com/flex022/uploads/zeppelin/original/2X/9/91d5c63797e63d804e728fcd993aa02dc8605e38.png)

I’m not sure what’s wrong because alot of articles have used this for examples.  
Thanks for your help.

---

<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:** [January 8, 2020, 4:46am UTC](https://forum.openzeppelin.com/t/erc20-decimals-specifying-a-big-number/2047/2 "2020-01-08T04:46:17Z")

</div>

Hi @sirphemmiey,

You can use a Big Number library.  
The test below uses BN from `@openzeppelin/test-helpers`

## SimpleToken.test.js

```auto
const { accounts, contract } = require('@openzeppelin/test-environment');

const {
  BN, // Big Number support
  constants, // Common constants, like the zero address and largest integers
  expectEvent, // Assertions for emitted events
  expectRevert, // Assertions for transactions that should fail
} = require('@openzeppelin/test-helpers');

const [creator, other] = accounts;

const { expect } = require('chai');

const SimpleToken = contract.fromArtifact('SimpleToken'); // Loads a compiled contract

describe('SimpleToken', function () {
  it('transfer', async function () {
    const token = await SimpleToken.new({ from: creator });
    const amount = new BN("1000000000000000000000");
    await token.transfer(other, amount, {from: creator});
    expect(await token.balanceOf(other)).to.be.bignumber.equal(amount);
  });
});

```

## SimpleToken.sol

```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:** ![sirphemmiey](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.openzeppelin.com/sirphemmiey/32/1479_2.png) [@sirphemmiey](https://forum.openzeppelin.com/u/sirphemmiey)\
**Post date:** [January 8, 2020, 6:18pm UTC](https://forum.openzeppelin.com/t/erc20-decimals-specifying-a-big-number/2047/3 "2020-01-08T18:18:40Z")

</div>

Hi @abcoathup, thanks for your reply.

I have tried a number of different big number libraries, the problem still persist.  
This is what i’m trying to do;

`let amount = withdrawObj.amount; `  
`const TokenDecimals = await this.getDecimal(symbol); // get the decimal of a token`  
`amount *= (10 ** TokenDecimals);`  
`Let's say the token decimal function returns 18 obviously, and i pass`2`as the amount to transfer, it gives me`2000000000000000000` which appears too large for the ERC20 transfer function to process. I have tried the big number library, OZ test helper, bing interger, ethers, web3 etc all to no avail.

Thanks for your help once again.

---

<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:** [January 9, 2020, 5:25am UTC](https://forum.openzeppelin.com/t/erc20-decimals-specifying-a-big-number/2047/4 "2020-01-09T05:25:09Z")

</div>

Hi @sirphemmiey,

We need to use the big number libraries multiply and power functions to do the calculation as the number is too big for JavaScript.

```auto
    const tokenbits = (new BN(10)).pow(decimals); 
    const amount = (new BN(100)).mul(tokenbits); 

```

See the following test for the SimpleToken contract above:

## SimpleToken.test.js

```auto
const { accounts, contract } = require('@openzeppelin/test-environment');

const {
  BN, // Big Number support
  constants, // Common constants, like the zero address and largest integers
  expectEvent, // Assertions for emitted events
  expectRevert, // Assertions for transactions that should fail
} = require('@openzeppelin/test-helpers');

const [creator, other] = accounts;

const { expect } = require('chai');

const SimpleToken = contract.fromArtifact('SimpleToken'); // Loads a compiled contract

describe('SimpleToken', function () {
  it('transfer', async function () {
    const token = await SimpleToken.new({ from: creator });
    const decimals = await token.decimals.call();
    const tokenbits = (new BN(10)).pow(decimals);
    const amount = (new BN(100)).mul(tokenbits);

    console.log(`Amount: ${amount}`);

    await token.transfer(other, amount, {from: creator});
    expect(await token.balanceOf(other)).to.be.bignumber.equal(amount);
  });
});

```

---

<div class="post-metadata">

**Author:** ![sirphemmiey](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.openzeppelin.com/sirphemmiey/32/1479_2.png) [@sirphemmiey](https://forum.openzeppelin.com/u/sirphemmiey)\
**Post date:** [January 12, 2020, 3:47am UTC](https://forum.openzeppelin.com/t/erc20-decimals-specifying-a-big-number/2047/5 "2020-01-12T03:47:21Z")

</div>

Thanks for your reply.  
It still displays the same error “(arg=”\_value", coderType=“uint256”, value=10000000000000000)"  
But when i changed the amount value after the bigNumber conversions to `amount.toString()`, the transaction went successfully.  
Could there be any issue with this way at the long run?

---

<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:** [January 12, 2020, 10:53pm UTC](https://forum.openzeppelin.com/t/erc20-decimals-specifying-a-big-number/2047/6 "2020-01-12T22:53:21Z")

</div>

Hi @sirphemmiey,

I assume it depends on the JavaScript library that you use to handle big numbers.  
Just ensure that you have appropriate testing that you are transferring the required amount of tokens.
