0

just wanted to check with you if my thinking is correct and this is the correct way of approaching percentage calculation:

We have the following constants:

userShares = 938186007644982137678069526 
globalShare = 196444905942680952896579643162
percentage = percentage of userShare in relation to globalShares
threshold = 10%

If we divide userShares by globalShare we would get 0.004775822529695566. And if we want to display that in % terms the way we are used to in real world we need to multiply it by 100 so that it give us 0,475%. So the % is very small in the beginning but what I want to accomplish is something that accounts for small percentages like 0.47% in the beginning but then also accounts for 10% in the future.

I want to create a feature where I say:

if (percentage >= threshold) {do something}

So in order to calculate the percentage I did:

function getUserSharesPercentage() external view returns (uint256 percentage) {
        // Get the total user shares 
        uint256 totalUserShares = 938186007644982137678069526;
       
        // Get the global shares
        uint256 totalGlobalShares = 196444905942680952896579643162;

        // avoid dividing by 0
        if (totalGlobalShares == 0) {
            return 0;
        }

        // Use a larger scaling factor first to prevent loss of precision
        uint256 SCALE = 1e6;

        // First multiply userShares by SCALE to prevent truncation
        uint256 scaledShares = totalUserShares * SCALE;

        // Then divide by totalGlobalShares
        percentage = (scaledShares / totalGlobalShares) ;

        return percentage;
    }
  1. The result of this function gives me 4775. So now if I want to compare the result of getUserSharesPercentage that is 4775 to 10%, I need to correctly also represent the 10% in relation to 4775.

If we want to represent % in solidity we use Basis points: 1% is represented by 1_00. 13% is represented by 13_00 and 100% is represented by 100_00. So following this logic I should compare 4775 against ((10_00) * 1e6), correct?

Or should it only be 10 * 1e6 = 10%

  1. Also if we used 1e4 as a scaling factor the result of the function would be 47, so 2 decimal places less so that means we would need to also use ((10_00) * 1e4), correct?

Thank you very much for your help and time reading my case!

Best,

1 Answer 1

1

After applying a scaling factor of 1e6, the result 0.004775 is represented as 4775, which, by definition, is the percentage of userShare in relation to globalShares.

To avoid the confusion, let's say threshold is 10 instead of 10%, as the original result (0.004775) is already in percentage terms (i.e., 0.004775%).

  1. So, the way 0.004775 is scaled to 4775 (by doing 0.004775 * 1e6).

    Similarly, the threshold 10 should be scaled to 10000000 (by doing 10 * 1e6).

    0.004775 * 1e6 = 4775       =>   0.004775%
    0.01     * 1e6 = 10000      =>   0.01%
    0.1      * 1e6 = 100000     =>   0.1%
    1        * 1e6 = 1000000    =>   1%
    10       * 1e6 = 10000000   =>   10%
    100      * 1e6 = 100000000  =>   100%
    
  2. And, if you use 1e4 as the scaling factor, then the result 0.004775 would be represented as 47 (by doing 0.004775 * 1e4).

    So, in this case, the threshold 10 should be scaled to 100000 (by doing 10 * 1e4).

    0.004775 * 1e4 = 47       =>   0.0047%
    0.01     * 1e4 = 100      =>   0.01%
    0.1      * 1e4 = 1000     =>   0.1%
    1        * 1e4 = 10000    =>   1%
    10       * 1e4 = 100000   =>   10%
    100      * 1e4 = 1000000  =>   100%
    

Your Answer

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

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