Percentage calculation

Calculate percentage of token user owns relative to total supply, burn it and distribute another token. I’m running into issues trying to calculate percentage and amount that has to be distributed.

function ratio(address _address, uint256 _amount) public {
        uint256 ratio = _amount.div(totalSupply);
        _burn(msg.sender, _amount);
        uint256 rewards = ERC20(token_address).balanceOf(thiscontract);
        uint256 pay = ratio.mul(rewards);
        ERC20(token_address).transfer(msg.sender, pay);
1 Like

The key is at uint256 ratio = _amount.div(totalSupply);, you need to mul at first and then div, cause there is no decimal in contract, that is if you use 10/100, you will get 0, so you can use 10*10000/100 => 1000, and the result with four decimals, so it is equal to 0.1

1 Like

Hi @umidou,

Welcome to the community :wave:

I have marked @Skyge reply as the solution.