0

I am wondering if my idea is correct.

Total shares: 11,000

User A's shares : 10,000

User A's share percentage = User A's share / Total share.

                      = 10,000 / 11,000
                      = 0.90909090909 
                      = 0 in solidity

However, Solidity does not support float, so I rewrite

User A's share percentage = User A's shares * 10,000 / Total shares.

                      = (10,000 * 10,000) / 11,000
                      = 9090.9090909
                      = 9090 in solidity  

User A's shares = total shares * User A's share percentage / 10,000

                      = (11,000 * 9,090 ) / 10,000
                      = 9999

User A's shares is 9999, but it should be 10,000. How do I correct my math?

1
  • use decimals to adjust for float. Aug 16 at 3:39

1 Answer 1

0

One way to achieve this is by utilizing a decimals variable, which defines the maximum number of decimal places the contract can support.

In your case, use a new decimals constant.

decimals = 2;

User A's share percentage = User A's shares * 10,000 / Total shares.

              = (10,000 * 10,000) / 11000) * (10^2)
              = 909090.90909
              = 909090 in solidity  

User A's shares = total shares * User A's share percentage / 10,000

              = (11,000 * 909090 ) / 10000
              = 999999    

Outside solidity, you can do the rounding process
       result = 999999 / decimals
// other languages which support flow has a higher accuracy for rounding
       result = 9999.99  
       result = 10000
   

By specifying an appropriate range for the decimals variable, you can ensure that the rounding process aligns with your specific requirements for accuracy.

Your Answer

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

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