I'm trying to calculate a percentage of a number in solidity with the following code.
The percentage is technically basis points (10000ths, instead of 100ths) because I want decimal percentage values.
uint128 is enough for expressing the normal numbers, but to try to avoid precision errors, I cast the values to int256 for the calculation.
pragma solidity 0.4.24;
contract PercentageCalc {
uint128 public bp = 185; // 1.85% in basis points (parts per 10,000)
function calculatePercentage(
uint128 theNumber
)
public
view
returns (uint128) {
return uint128(int256(theNumber) / int256(10000) * int256(bp));
}
}
The problem is if I input 111111111111111111
as theNumber
, the output is 2055555555555535
instead of the desired output of 2055555555555555
(what I get by doing the calculation with for example BigNumber.js).
I'm guessing it is some precision error I have overlooked. How do it get the desired output of 2055555555555555
?