in my contract I need to do some calculations to convert some ERC20 tokens value to WEI and I need to do something like:

tokenValueWei(uint) = tokenValue(uint) * multiplier(ufixed)

eg. 100000012 (some token fractions) * 0.0013233 (multiplier) = 132330 (WEI rounded)

while the multiplier is being supplied to the contract by the contract manager. Any way to achieve this?


ufixed is not supported yet in Solidity.

The trick that you always can apply is: scale the numbers by the precision that you need.

For instance, if you need precision 18: you can pass the multiplier as uint as multiplier*(10**18). At the end you simply divide the result by 10**18 and everything is fine.

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.