imgrooot Posted July 2, 2017 Share Posted July 2, 2017 Say I have 5 users. Each user will receive their payment based on their individual percentage. For eg. User 1: 5% User 2: 10% User 3: 15% User 4: 15% User 5: 5% Now say I have a total of $100. I want to divide the $100 among the 5 users. But the users with higher % will receive a higher amount than the the ones with lower %. How would I calculate this? Quote Link to comment https://forums.phpfreaks.com/topic/304250-how-would-you-do-these-calculations/ Share on other sites More sharing options...
imgrooot Posted July 2, 2017 Author Share Posted July 2, 2017 Say I have 5 users. Each user will receive their payment based on their individual percentage. For eg. User 1: 5% User 2: 10% User 3: 15% User 4: 15% User 5: 5% Now say I have a total of $100. I want to divide the $100 among the 5 users. But the users with higher % will receive a higher amount than the the ones with lower %. How would I calculate this? My question may be a little confusing so I will give you another example. Above I gave you an example of 5 users only. This could easily be 100 or 1000 users. So If I have 100 users with individual percentage of 5%,10%,15% and the total give away amount is $100, how would you divide the $100 among the 100 users? Quote Link to comment https://forums.phpfreaks.com/topic/304250-how-would-you-do-these-calculations/#findComment-1547906 Share on other sites More sharing options...
kicken Posted July 2, 2017 Share Posted July 2, 2017 If you have a percentage for each user then you'd just multiple the percentage (in decimal form) against the total to get their share. $userPercentage = 0.05; $total = 100; $userShare = $userPercentage * $total; // 0.05 * 100 = 5 You'll have to decide what you want to do as far as rounding though if necessary. For example with USD you could end up with values less than a cent depending on the breakdown. If you had 1000 users but only $5 to distribute equal distribution would be 0.5% each or 1/2 of a cent. Since you can't give someone half a cent you'd have to find a solution. Possible solutions would be:Add the 1/2 cent to a running total of what is due then distribute when they reach a minimum amount Round to 0 and give them nothing (users would probably hate you) Round to one cent (users would like you, but you'd loose money) Quote Link to comment https://forums.phpfreaks.com/topic/304250-how-would-you-do-these-calculations/#findComment-1547907 Share on other sites More sharing options...
imgrooot Posted July 2, 2017 Author Share Posted July 2, 2017 If you have a percentage for each user then you'd just multiple the percentage (in decimal form) against the total to get their share. $userPercentage = 0.05; $total = 100; $userShare = $userPercentage * $total; // 0.05 * 100 = 5 You'll have to decide what you want to do as far as rounding though if necessary. For example with USD you could end up with values less than a cent depending on the breakdown. If you had 1000 users but only $5 to distribute equal distribution would be 0.5% each or 1/2 of a cent. Since you can't give someone half a cent you'd have to find a solution. Possible solutions would be:Add the 1/2 cent to a running total of what is due then distribute when they reach a minimum amount Round to 0 and give them nothing (users would probably hate you) Round to one cent (users would like you, but you'd loose money) That makes sense. Thank you. Quote Link to comment https://forums.phpfreaks.com/topic/304250-how-would-you-do-these-calculations/#findComment-1547908 Share on other sites More sharing options...
imgrooot Posted July 3, 2017 Author Share Posted July 3, 2017 (edited) Actually I realized something. Your method would normally work but not with what I am trying to do. For eg. Say I have 30 users. 10 users get 10% 10 users get 30% 10 users get 60% Total of 30 users with their own percentages. Using your method, it would look like this. $userPercentage = 0.1; // 10% $total = 100; $userShare = $userPercentage * $total; // 0.05 * 100 = 10 $new_total = $userShare * 10; // 10 being the amount of users. This totals $100. $userPercentage = 0.3; // 30% $total = 100; $userShare = $userPercentage * $total; // 0.3 * 100 = 30 $new_total = $userShare * 10; // 10 being the amount of users. This totals $300. $userPercentage = 0.6; // 60% $total = 100; $userShare = $userPercentage * $total; // 0.6 * 100 = 60 $new_total = $userShare * 10; // 10 being the amount of users. This totals $600. All totaling to $1,000 for 30 users. That's not what I want. I want to have all 30 users' total come to the original amount, which is $100, not $1,000. Edited July 3, 2017 by imgrooot Quote Link to comment https://forums.phpfreaks.com/topic/304250-how-would-you-do-these-calculations/#findComment-1547909 Share on other sites More sharing options...
gizmola Posted July 3, 2017 Share Posted July 3, 2017 You did not account for the 1/10th of each pool allocated, as Kicken did in his example. His was pre-computed for you in his example (ie. .05). In plain english a single user's share is "1/10th of pool% of total" $userPercentage = 0.1 * 0.1; // 10% $total = 100; $userShare = $userPercentage * $total; // 0.05 * 100 = 10 $new_total = $userShare * 10; // 10 being the amount of users. This totals $100. $userPercentage = 0.1 * 0.3; // 30% $total = 100; $userShare = $userPercentage * $total; // 0.3 * 100 = 30 $new_total = $userShare * 10; // 10 being the amount of users. This totals $300. $userPercentage = 0.1 * 0.6; // 60% $total = 100; $userShare = $userPercentage * $total; // 0.6 * 100 = 60 $new_total = $userShare * 10; // 10 being the amount of users. This totals $600. Here's your code with some variable echoing to show you that this works: https://3v4l.org/p5cVX Quote Link to comment https://forums.phpfreaks.com/topic/304250-how-would-you-do-these-calculations/#findComment-1547914 Share on other sites More sharing options...
imgrooot Posted July 3, 2017 Author Share Posted July 3, 2017 (edited) You did not account for the 1/10th of each pool allocated, as Kicken did in his example. His was pre-computed for you in his example (ie. .05). In plain english a single user's share is "1/10th of pool% of total" $userPercentage = 0.1 * 0.1; // 10% $total = 100; $userShare = $userPercentage * $total; // 0.05 * 100 = 10 $new_total = $userShare * 10; // 10 being the amount of users. This totals $100. $userPercentage = 0.1 * 0.3; // 30% $total = 100; $userShare = $userPercentage * $total; // 0.3 * 100 = 30 $new_total = $userShare * 10; // 10 being the amount of users. This totals $300. $userPercentage = 0.1 * 0.6; // 60% $total = 100; $userShare = $userPercentage * $total; // 0.6 * 100 = 60 $new_total = $userShare * 10; // 10 being the amount of users. This totals $600. Here's your code with some variable echoing to show you that this works: https://3v4l.org/p5cVX So what if I have 250 users? What would 0.1 look like in this line? Is it 1/10th of 250, which is 25? $userPercentage = 0.1 * 0.6; Edited July 3, 2017 by imgrooot Quote Link to comment https://forums.phpfreaks.com/topic/304250-how-would-you-do-these-calculations/#findComment-1547916 Share on other sites More sharing options...
imgrooot Posted July 3, 2017 Author Share Posted July 3, 2017 (edited) I should also mention that the example I share and the example you edited have are for 3 users only that equals to $100. I will be using this formula in a foreach loop. So if I have more the 4 users, which I do, their total sum is greater than $100, which is not what I want. For eg. Here's the for each loop. $total_members = 250; foreach($result as $row) { $commission = $row['commission']; // This commission could be 10%, 30%, 60%; $amount = $row['amount']; $new_commission = $commission / 100; $userPercentage = 0.1 * $new_commission; $total = $amount; $userShare = $userPercentage * $total; $new_total = $userShare * $total_members; echo $new_total; } Edited July 3, 2017 by imgrooot Quote Link to comment https://forums.phpfreaks.com/topic/304250-how-would-you-do-these-calculations/#findComment-1547917 Share on other sites More sharing options...
kicken Posted July 3, 2017 Share Posted July 3, 2017 (edited) So what if I have 250 users? What would 0.1 look like in this line?If you had 250 users then each would have 1/250th of the pool, or 0.004. If that 250 were only eligible for 10% of the main pool then it'd be 0.1 * 0.004 = 0.0004 Edited July 3, 2017 by kicken Quote Link to comment https://forums.phpfreaks.com/topic/304250-how-would-you-do-these-calculations/#findComment-1547918 Share on other sites More sharing options...
imgrooot Posted July 3, 2017 Author Share Posted July 3, 2017 (edited) If you had 250 users then each would have 1/250th of the pool, or 0.004. If that 250 were only eligible for 10% of the main pool then it'd be 0.1 * 0.004 = 0.0004 Alright so based on what you said, here is the new example. $total_amount = 100; // 100 dollars $total_members = 250; // 250 members $total_commission = 60; // 60% commission $new_commission = $total_commission / 100; // this would return 0.6 $new_members = 1 / $total_members; // this would return 0.004 $userPercentage = $new_commission * $new_members; // 0.6 * 0.004 = 0.0024 $userShare = $userPercentage * $total_amount; // 0.0024 * 100 = 0.24 $new_total = $userShare * $total_members; // 0.24 x 250 = 60 That returns a total of 60 for each user. That is technically correct. But remember there are total of 250 users. That's $60 x 250 = $15,000. That not what I am looking for. I am looking for all 250 users' amount to sum up to $100 in total. Normally I can 100/250 and it'll return $0.4 for each user. But since each user has a different percentage, each of them is going to have different values that add up to $100. Please let me know if I am making sense. Edited July 3, 2017 by imgrooot Quote Link to comment https://forums.phpfreaks.com/topic/304250-how-would-you-do-these-calculations/#findComment-1547919 Share on other sites More sharing options...
Solution imgrooot Posted July 3, 2017 Author Solution Share Posted July 3, 2017 (edited) Alright so I think I have it figured out. It's rather simple. $total_amount = 100; // 100 dollars $total_members = 250; // 250 members $total_commission = 60; // 60% commission $get_percent = ($total_commission / 100) * $total_amount; $new_total = $get_percent / $total_members; // $60 / 250 = 0.24 per user. This is what I was missing before. // Since above is based on 60%, I still have 30% and 10% left. Below are these examples. $total_amount = 100; // 100 dollars $total_members = 50; // 50 members for this example, just to vary it up. $total_commission = 30; // 30% commission $get_percent = ($total_commission / 100) * $total_amount; $new_total = $get_percent / $total_members; // $30 / 250 = 0.12 per user. $total_amount = 100; // 100 dollars $total_members = 20; // 20 members for this example, just to vary it up. $total_commission = 10; // 10% commission $get_percent = ($total_commission / 100) * $total_amount; $new_total = $get_percent / $total_members; // $10 / 250 = 0.04 per user. $60(60%), $30(30%), $10(10%) sum up to $100. No matter how many users are in each percentage pool, it'll always sum up to that exact total. Edited July 3, 2017 by imgrooot Quote Link to comment https://forums.phpfreaks.com/topic/304250-how-would-you-do-these-calculations/#findComment-1547920 Share on other sites More sharing options...
Psycho Posted July 3, 2017 Share Posted July 3, 2017 (edited) That doesn't work. Your supposed results of the second & third calculations are not correct. For the 30% users the result would be $0.60 and for the 10% users it would be $0.50. In both instances you used 250 as the $total_members from the first calculation. So, with those correct results, a person with a 10% (and only 20 members) would get more than a person with a 60% (and 250 members). The main issue with the problem is the use of the term "percentage". Based on your initial description the "percentage" should really be considered as the number of "shares" the people have. This really sounds like a stock problem - not a commission problem. To determine a persons value in a stock, the formula is as follows: [total value] / [total shares of stock] * [user's shares of stocks] The part in blue in the value of a single share. Example //Define the total amount $total_amount = 100; //Define the members and thier shares $member_shares = array( 60 => 250, //250 members with 60 shares each 30 => 50, // 50 members with 30 shares each 10 => 20 // 20 members with 10 shares each ); //Calculate the total shares $total_shares = 0; foreach($member_shares as $shares => $members) { $total_shares += $shares * $members; } //Calculate the value of a single share $share_value = $total_amount / $total_shares; //Calculate the value of each unique share amount foreach($member_shares as $shares => $members) { $value = $share_value * $shares; //Round/format as needed echo "Shares {$shares}: {$value}<br>\n"; } Edited July 3, 2017 by Psycho Quote Link to comment https://forums.phpfreaks.com/topic/304250-how-would-you-do-these-calculations/#findComment-1547927 Share on other sites More sharing options...
imgrooot Posted July 3, 2017 Author Share Posted July 3, 2017 That doesn't work. Your supposed results of the second & third calculations are not correct. For the 30% users the result would be $0.60 and for the 10% users it would be $0.50. In both instances you used 250 as the $total_members from the first calculation. So, with those correct results, a person with a 10% (and only 20 members) would get more than a person with a 60% (and 250 members). The main issue with the problem is the use of the term "percentage". Based on your initial description the "percentage" should really be considered as the number of "shares" the people have. This really sounds like a stock problem - not a commission problem. To determine a persons value in a stock, the formula is as follows: [total value] / [total shares of stock] * [user's shares of stocks] The part in blue in the value of a single share. Example //Define the total amount $total_amount = 100; //Define the members and thier shares $member_shares = array( 60 => 250, //250 members with 60 shares each 30 => 50, // 50 members with 30 shares each 10 => 20 // 20 members with 10 shares each ); //Calculate the total shares $total_shares = 0; foreach($member_shares as $shares => $members) { $total_shares += $shares * $members; } //Calculate the value of a single share $share_value = $total_amount / $total_shares; //Calculate the value of each unique share amount foreach($member_shares as $shares => $members) { $value = $share_value * $shares; //Round/format as needed echo "Shares {$shares}: {$value}<br>\n"; } Your right. The results for my second and third calculations would be what you wrote. My mistake what I wrote in the comment part of it. But the math works out with the code itself. Your example is an interesting way to solve this problem. And yes now that you mention it, it is more like shares than commissions. I will try it out as well. Quote Link to comment https://forums.phpfreaks.com/topic/304250-how-would-you-do-these-calculations/#findComment-1547929 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.