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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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