如何计算百分比[关闭]

I am really bad at math and i can't calculate the following thing.. I have two columns named experience and level .

If experience is over 200 then level is updating to 1

If experience is over 500 then level is updating to 2

.... I mean I never reset experience.

So, I have a loading bar image and i have to calculate how long to be in percentages. I tried something like this

function lvlbar($xp, $level){

if($level == 0)
    {

         $percent = @round(($xp / 200)*100);

   }

    elseif($level == 1){

$percent = @round(($xp / 500)*100);

    }

    elseif($level == 2){

    $percent = @round(($xp / 900)*100);
} 

    elseif($level == 3){
    $percent = @round(($xp / 1400)*100);
} 

    elseif($level == 4){
    $percent = @round(($xp / 1900)*100);
} 

    elseif($level == 5){
    $percent = @round(($xp / 2500)*100);
} 

    elseif($level == 6){
    $percent = @round(($xp / 3200)*100);
} 

    elseif($level == 7){
    $percent = @round(($xp / 4000)*100);
} 

    elseif($level == 8){
    $percent = @round(($xp / 4900)*100);
} 

    elseif($level == 9){
    $percent = @round(($xp / 5800)*100);
} 

    elseif($level == 10){
    $percent = @round(($xp / 6800)*100);
}

return $percent;

}

It's working fine when you are under level 1 but once you hit level 2 calculations goes wrong and percentages starts at 50, 60 and so.. I guess it's my formula.Help me out

Your concept looks good, but there is a slight mistake. The experience bar needs to reset for each level. For example, when experience is 350, you want to show 50%.

It means, your formula is:

Percent to next level = Experience from current level / Experience required to advance to next level

Again, example:

Percent to level 2 when experience is 350 = (350 - 200) / (500 - 200) = 50%

If you're issue is at level 3, look at your maths for that condition:

elseif($level == 3){
    $percent = @round(($xp / 1400)*100);
}

If $xp = 900 as you say, use a calculator and divide that by 1400 as in your calculation above.

Then times the result of that by 100, and the rounded result is 64% so the maths is returning correctly, it's just the wrong equation.

I don't exactly get the problem and so can't suggest an exact solution. But certainly that is messy repetitive code! I think it would be better for the that number you are dividing by to be incremented at the same time level is. So without knowing exactly what it is your doing, something like:

function goUpLevel(){
  $level++;
  $experienceMax = $level * $experienceLevelMultiplier;
}

And then your function you posted would be way more simple, such as:

function lvlbar($xp, $level){
$percent = @round(($xp / $experienceMax)*100);
}

If you can't figure out a multiplier for $experienceLevelMultiplier, then maybe an array of values for each level, ie

$experienceLevelMultiplierArray = array (
1 => 10
2 => 15.5
);

This looks better, scales better, and is more manageable and understandable! If you describe a bit of the background of the problem I can surely help further!

DISCLAIMER: I haven't done PHP in a long time, so please ignore any errors. I'm communicating an idea, not an exact solution.

If you want the percentage bar to go back down to 0 (not include the previous experience in the bar) you need to minus the amount from the previous level.

Also, I would recommend using a switch() for this.

Try this:

function lvlbar($xp, $level){
  switch($level) {
    case 0:
      $percent = @round(($xp / 200)*100);
    case 1:
      $percent = @round((($xp - 200) / 500)*100);
    case 2:
      $percent = @round((($xp - 500) / 900)*100);
    case 3:
      $percent = @round((($xp - 900) / 1400)*100);
    case 4:
      $percent = @round((($xp - 1400) / 1900)*100);
    case 5:
      $percent = @round((($xp - 1900) / 2500)*100);
    case 6:
      $percent = @round((($xp - 2500) / 3200)*100);
    case 7:
      $percent = @round((($xp - 3200) / 4000)*100);
    case 8:
      $percent = @round((($xp - 4000) / 4900)*100);
    case 9:
      $percent = @round((($xp - 4900) / 5800)*100);
    case 10:
      $percent = @round((($xp - 5800) / 6800)*100);
  }
  return $percent;
}

Here's pretty much what I would do. As previous posters have mentioned you have to adjust the xp when leveling up.

function lvlbar($xp, $level){
  $levels = array(
      0 => 200,
      1 => 500,
      2 => 900,
      3 => 1400,
      4 => 1900,
      5 => 2500,
      6 => 3200,
      7 => 4000,
      8 => 4900,
      9 => 5800,
      10 => 6800
  );
  if (array_key_exists($level, $levels)) {
    $lastlevelXp = !empty($levels[$level-1]) ? $levels[$level-1] : 0;
    $adjustedXp = $xp - $lastlevelXp;
    $adjustedLevelMax = $levels[$level] - $lastlevelXp;
    $percent = round(($adjustedXp / $adjustedLevelMax)*100);
  }
  return !empty($percent) ? $percent : 0;
}

Example output:

Level 0 with 95xp:   48%
Level 1 with 370xp:  57%
Level 2 with 640xp:  35%
Level 3 with 1150xp: 50%
Level 4 with 1405xp: 1%
Level 5 with 2400xp: 83%
Level 6 with 3180xp: 97%
Level 7 with 3999xp: 100%