almightyegg Posted September 8, 2007 Share Posted September 8, 2007 in my game there are different groups. members of the groups will donate game money and such. but the groups have three different money accounts. The group leaders decide what percentage of the donated money goes to which fund. EG: Fund 1 - 50% of all money donated goes into this account. Fund 2 - 32% Fund 3 - 18% But if a member wants to be difficult and donates 1 piece of gold, then it obviusly splits into decimal which I don't want, therefore the mysql database is int not decimal. This means that the 1 gold is completely gone.... How would I make it so that it works out how much the decimals add up to and then puts it into the fund with the highest % ??? I hope that all made sense.... Quote Link to comment https://forums.phpfreaks.com/topic/68524-solved-problem-with-working-this-out/ Share on other sites More sharing options...
Barand Posted September 8, 2007 Share Posted September 8, 2007 try <?php $pcArr = array ( 'Fund 1' => 0.50, 'Fund 2' => 0.32, 'Fund 3' => 0.18 ); function splitDonation($amount, &$pcArr) { $maxPC = max($pcArr); $maxFund = array_search($maxPC, $pcArr); $dec = 0; $shares = array(); foreach ($pcArr as $fund => $pc) { $tmp = $amount * $pc; $shares[$fund] = floor($tmp); $dec += $tmp - floor($tmp) ; } $shares[$maxFund] += $dec; return $shares; } $donation = 25; $share = splitDonation($donation, $pcArr); echo '<pre>', print_r($share, true), '</pre>'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/68524-solved-problem-with-working-this-out/#findComment-344439 Share on other sites More sharing options...
almightyegg Posted September 8, 2007 Author Share Posted September 8, 2007 Array ( [Fund 1] => 13 [Fund 2] => 8 [Fund 3] => 4 ) that's with amount = 1,000,000 It's not working out the % of the amount in each fund, it is working out what the remainder is so that none of the money is lost.... Quote Link to comment https://forums.phpfreaks.com/topic/68524-solved-problem-with-working-this-out/#findComment-344443 Share on other sites More sharing options...
Barand Posted September 8, 2007 Share Posted September 8, 2007 with a donation of 1,000,000 it gives [pre] Array ( [Fund 1] => 500000 [Fund 2] => 320000 [Fund 3] => 180000 ) [/pre] the figures you quoted are for my test value of 25 Quote Link to comment https://forums.phpfreaks.com/topic/68524-solved-problem-with-working-this-out/#findComment-344446 Share on other sites More sharing options...
almightyegg Posted September 8, 2007 Author Share Posted September 8, 2007 ah, oops sorry, I've just come accross a problem, the %s are stored 50 32 18 and not 0.50 etc... I tried this: $pcArr = array ( 'Fund 1' => '0.$clutch[fundp1]', 'Fund 2' => '0.$clutch[fundp2]', 'Fund 3' => '0.$clutch[fundp3]' ); but it just prints out 0 at the end... Quote Link to comment https://forums.phpfreaks.com/topic/68524-solved-problem-with-working-this-out/#findComment-344452 Share on other sites More sharing options...
Barand Posted September 8, 2007 Share Posted September 8, 2007 then <?php $pcArr = array ( 'Fund 1' => 50, 'Fund 2' => 32, 'Fund 3' => 18 ); function splitDonation($amount, &$pcArr) { $maxPC = max($pcArr); $maxFund = array_search($maxPC, $pcArr); $dec = 0; $shares = array(); foreach ($pcArr as $fund => $pc) { $tmp = $amount * $pc/100; $shares[$fund] = floor($tmp); $dec += $tmp - floor($tmp) ; } $shares[$maxFund] += $dec; return $shares; } $donation = 25; $share = splitDonation($donation, $pcArr); echo '<pre>', print_r($share, true), '</pre>'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/68524-solved-problem-with-working-this-out/#findComment-344459 Share on other sites More sharing options...
almightyegg Posted September 8, 2007 Author Share Posted September 8, 2007 Ah great...another problem, I can't seem to be able to split $share up... I tried $share[0] $share[1] and $share[2] but there are empty... Quote Link to comment https://forums.phpfreaks.com/topic/68524-solved-problem-with-working-this-out/#findComment-344468 Share on other sites More sharing options...
Barand Posted September 8, 2007 Share Posted September 8, 2007 As you can see from the output, [pre] Array ( [Fund 1] => 500000 [Fund 2] => 320000 [Fund 3] => 180000 )[/pre] the keys of $share are "Fund 1" , "Fund 2", "Fund 3" Quote Link to comment https://forums.phpfreaks.com/topic/68524-solved-problem-with-working-this-out/#findComment-344470 Share on other sites More sharing options...
almightyegg Posted September 8, 2007 Author Share Posted September 8, 2007 doh! I'm blind!! Thanks for all the help Quote Link to comment https://forums.phpfreaks.com/topic/68524-solved-problem-with-working-this-out/#findComment-344471 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.