1

I'm trying to calculate commission, but having a hard time doing a very easy math in Solidity.

Ex:

Commission = 5%

Sales = 100 ETH

100 x 5% (0.05) = 5 ETH

and in Solidity:

uint sales = 100;
uint afterCommission = sales * 0.05;

When I do this, I get

TypeError: Operator * not compatible with types uint256 and rational_const 1/2

Solidity can't assign a decimal points to a variable? How can I make this work?

1

Right, Solidity only supports integers. Try this instead:

uint sales = 100;
uint afterCommission = sales * 5 / 100; // 5%

If it's a variable amount but a whole number percent:

uint commissionPercentage = 7;
uint afterCommission = sales * commissionPercentage / 100; // 7%

If you need more decimal places:

uint commissionNumerator = 75;
uint commissionDenominator = 1000;
uint afterCommission = sales * commissionNumerator / commissionDenominator; // 7.5%
  • If you need to store percentage ownership, like one person had 79.5 % and the other person had 21.5%, would you store those percentages as two variables with the whole and the fraction? Or would use use some math library and store 795 and 215? – NealWalters Nov 7 at 2:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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