0

I am trying to create a small voting system in a smart contract where a vote is passed when the majority of admins have voted. I now realise that this is not as easy as I thought, as there are no floats in Solidity, so I cannot simply do (voteCount/adminCount)*100 > 50. I noticed the problem when I tried to do (2/3)*100 and it results in 0.

How is something like this usually solved? Didn't pay much attention in math classes back in school, so the Google searches I've done on the topic didn't help me much.

3
1

Do first the multiplications and then divide: (voteCount*100/adminCount) > 50

In the case of 2 votes and 3 admins you will get 200/3 = 66 > 50

If you need more resolution (decimals) you can add a multiplier to both side of the inequality, for instance:

decimals: 2;

(voteCount*100*(10**decimals)/adminCount) > 50*(10**decimals)

Again in the case of 2 out of 3 votes you will get 20000/3 = 6666 > 5000

Hope this helps

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.