Skip to content
Advertisement

Calculate percent [closed]

I was wondering how to calculate the percentage in a level system, let me explain better: The user has his experience and this represents the level where the user is, i.e. if he has 0 experience he is level 1 and he needs 2000 experience for level 2.

Then I want to calculate the remaining percentage of how much is missing to reach 2000 experience.

Example:

int current_exp = 500;
int required_exp = 1500; // The remaining difference between the current experience and the required experience for level 2.
int level_two_experience = 2000;
float percent = ...

I found an example on the internet where the user has 1,262 experience and needs 5000 for the next level so the percentage is 26.2%.

Advertisement

Answer

When performing calculations with real numbers, I would recommend to use double in case of Java.

If you need to be precise (for instance with money involved), I would handle the numbers after the decimal point myself.

In your case you could calculate it in the following way (remaining percentage):

double percent = (level_two_experience - current_exp) / Double.valueOf(level_two_experience) * 100

If you want to calculate the percentage it currently has achieved it could be calculated this way:

double percent = current_exp / Double.valueOf(level_two_experience) * 100
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement