Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Senior Coder
    Join Date
    May 2005
    Posts
    2,137
    Thanks
    96
    Thanked 72 Times in 72 Posts

    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?

  • #2
    Regular Coder
    Join Date
    Nov 2005
    Location
    North Canton, Ohio
    Posts
    118
    Thanks
    11
    Thanked 4 Times in 4 Posts
    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);
    Last edited by Blaher; 08-08-2008 at 03:57 AM.

  • Users who have thanked Blaher for this post:

    masterofollies (08-08-2008)

  • #3
    Senior Coder
    Join Date
    May 2005
    Posts
    2,137
    Thanks
    96
    Thanked 72 Times in 72 Posts
    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.

  • #4
    Senior Coder Len Whistler's Avatar
    Join Date
    Jul 2002
    Location
    Vancouver, BC Canada
    Posts
    1,323
    Thanks
    26
    Thanked 100 Times in 100 Posts
    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;
    }
    ?>
    Leonard Whistler

  • #5
    Regular Coder
    Join Date
    Oct 2004
    Posts
    104
    Thanks
    7
    Thanked 2 Times in 2 Posts
    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
    •