Jump to content

[SOLVED] problem with working this out


almightyegg

Recommended Posts

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....

Link to comment
https://forums.phpfreaks.com/topic/68524-solved-problem-with-working-this-out/
Share on other sites

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>';
?>

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....

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...

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>';
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.