...

View Full Version : Subtracting percent from a number



masterofollies
08-08-2008, 04:39 AM
Ok I hope this isn't too much extreme math to figure out. I will make it as easy to understand as possible.

Based on the number of members you have in your group, I want a set percent go to each member. Such as 50%, 10%, 3%, 1%.

So I set it up like this.


if ($row["members"] < "3")
{
$amount = "50%";
}
elseif (($row["members"] > "2") && $row["members"] < "11")
{
$amount = "10%";
}
elseif (($row["members"] > "10") && $row["members"] < "31")
{
$amount = "3%";
}
elseif ($row["members"] > "30")
{
$amount = "1%";
}

Nothing wrong right? So then I want to subtract the percent from the amount of experience the group has gained.

Such as if they have 100,000 experience and they only have 1 or 2 members, they should both get 50,000 (50% each).

I setup this string.


$add = $row["exper"] - $amount;

Didn't work, so I tried


$add = $row["exper"]/$amount;

And then it gave like 60207.27 which wouldn't exactly work, but I know you can do the round up or round down code for that. But anyways it's incredible the wrong number, 60,000 from over 3,000,000 experience at 50% each? Big error. Am I doing this all wrong?

Blaher
08-08-2008, 04:46 AM
I'm not 100&#37; on this, but I don't think you can throw in a percent sign into a variable and expect it to do math. It might be storing it as a string instead. In fact the percent sign is a operator for modulus. Not only that, but you're covering your numbers with quotes which will make it a string either way.

Just have it has a decimal.

$amount= 0.5;
$add = $row["exper"] - ($row["exper"]*$amount);

masterofollies
08-08-2008, 05:20 AM
Ok that part works fine. I had to do some other altering, but ran into another problem. But I'd hate to fill up the forum with help questions, so I will attempt to figure it out.

Len Whistler
08-08-2008, 07:05 AM
You can simplify the if statement.



<?php
$num_members = $row[members];
if ($num_members >=0) {
$amount = 50;
}
if ($num_members >=3) {
$amount = 10;
}
if ($num_members >=10) {
$amount = 3;
}
if ($num_members >=30) {
$amount = 1;
}
?>

NeoPuma
08-08-2008, 11:03 PM
Generally, if you wish to take 10&#37; off something, just times it by 0.9:

100 * 0.9 = 90 (10% taken off)
100 * 0.5 = 50 (50% taken off)

etc.

<?php
$add = $row["exper"]*(1-$amount); // 100000 * (1-0.5)
?>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum