Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.

# Thread: Subtracting percent from a number

1. ## Subtracting percent from a number

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.

Code:
```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.

Code:
`\$add = \$row["exper"] - \$amount;`
Didn't work, so I tried

Code:
`\$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?

• 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;

• ## Users who have thanked Blaher for this post:

masterofollies (08-08-2008)

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

• You can simplify the if statement.

PHP Code:
``` <?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; } ?> ```

• 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 Code:
``` <?php\$add = \$row["exper"]*(1-\$amount); // 100000 * (1-0.5)?> ```

•

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•