0

I am trying to get the value of (75 % 100) * 638

when the transaction is called the return value is 0 ?

is there any way to divide smaller numbers against larger ones, the purpose of the function is to return a percentage of the tokenAmount (638)

the code is below

pragma solidity ^0.4.21;

contract math {

  uint tokenAmount;

  function doo(uint _nominator) public returns (uint) {
      tokenAmount = 638;
      uint stPrize = (_nominator / 100) * tokenAmount;
      return stPrize;
  }
}

thanks in advance !

2 Answers 2

1

As Solidity only supports integers at this moment, you should use a multiplier. When you receive the output of the division, you can then divide it my the multiplier later, when needed.

Also, you can only get return values from smart contracts functions that are either declared as view, constant or pure, when you are calling them from an off-chain application.

So, here's how I would do it:

pragma solidity ^0.4.21;

contract math {
    // A very large multiplier means you can support many decimals
    uint256 public constant MULTIPLIER = 10e18;

    uint tokenAmount = 638;

    function setTokenAmount(uint256 _tokenAmount) public {
        tokenAmount = _tokenAmount;
    }

    function doo(uint _nominator) public view returns (uint) { 
        uint stPrize = (MULTIPLIER * _nominator / 100) * tokenAmount;
        return stPrize; 
    } 
}

This contract's doo function now returns the result multiplied by MULTIPLIER. Now, when you get the output in an environment which does support decimal number values, you can divide the output of doo by MULTIPLIER and there's your answer.

0
0

Simply replace this:

(_nominator / 100) * tokenAmount

With this:

(_nominator * tokenAmount) / 100
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.