I am trying to write a presale contract as an exercise that would allow the deployer to collect ether from buyers, then redistribute the amount of tokens proportionately to the amount spent by the buyers over the totalAmount of ether spent during the presale.

the function to calculate the token amounts to be sent to each buyer looks like this :

function _calculateClaim(address _claimer) public view returns(uint256) {
//amount paid in wei by user
uint256 amountPurchased = presalePayment[_claimer];
//trying to obtain a ratio number 0.xxx to the total amount of eth received
uint256 totalShare = amountPurchased.div(totalPaymentReceived);
//this would give me the number of tokens to send to the address relative to the total of tokens that need to be redistributed
uint256 allowedAmount = totalShare.mul(presaleAmount);


obviously totalShare canno't store a decimal value and is returned as a 0. Is there a simple workaround for this issue? what type of variable should i assign to totalShare?


  • Solidity doesn't handle floating points. You could multiply the ratio by 100 and use it as a percentage value
    – pbsh
    Jan 25, 2022 at 18:50
  • yes, that's the idea. A way out of this is to multiply the amount purchased by 10**18 and set a max totalpaymentReceived, in order to never get a number under 1 for the division result, then divide allowed amount by the same 10^18.
    – ian3111
    Jan 25, 2022 at 21:25


Browse other questions tagged or ask your own question.