Home > database >  How to get the percentage of points or the percentage that needs to get to the next level in Java?
How to get the percentage of points or the percentage that needs to get to the next level in Java?

Time:07-17

I have a system that "translates" points of a player to a certain level, for example: If a player has 0-49 points it translates to level 0 and 50-299 points it translates to being at level 1 and so on.. The problem here is that I want to have the percentage of the points of the player or the percentage needed to reach the next level, better if I can have the exact percentage of the points, I would appreciate it if somebody can help me with it, please. Here is how I am "translating" the points to a certain level:

public int translatePointsToLevel(int points) {

        Integer level = (int) (Math.floor(25   Math.sqrt(625   100 * points)) / 100);
        return level;
        
    }

CodePudding user response:

One way to do this is to have another method to translate the level to the minimum number of points, level = floor(25 sqrt(625 100 * points)) / 100, so minPoints = 100 * (level - 25)^2 - 6.

Then you can calculate the amount of points to level up by taking points for level 1 and subtracting the points for the level.

CodePudding user response:

From what I can tell you want to find the number of points needed to get to one level from another level. To do this you could think of your leveling system as an algebraic function where x = points and y = level.

The function for calculating your level is y = (25 √(625 100 * x)) / 100

To calculate the number of points needed for a given level you need to solve this function in terms of x.

When solved for x the function is x = 50y(2y - 1).

When you put this function into code your code for calculating the points needed for a given level is as follows.

    public static int pointsNeededForLevel(int level) {
        return 50 * level * (2 * level - 1);
    }

If you would like to calculate the percent of the way a player is to the next level you could use the following code. The percent will be represented as a double between the values of 0 through 1. If you would like to translate this into a percent just use int percentProgress = percentOfProgressToNextLevel(currentNumberOfPoints) * 100.0.

    public static double percentOfProgressToNextLevel(int currentNumberOfPoints) {
        int currentLevelNeededPoints = pointsNeededForLevel(translateLevel(currentNumberOfPoints));
        return (double) (currentNumberOfPoints - currentLevelNeededPoints) / (double) (pointsNeededForLevel(translateLevel(currentNumberOfPoints)   1) - currentLevelNeededPoints);
    }

If you would like to calculate the number of points needed to reach the next level you could use the following code.

    public static int pointsNeededToReachNextLevel(int currentNumberOfPoints) {
        return pointsNeededForLevel(translateLevel(currentNumberOfPoints)   1) - currentNumberOfPoints;
    }
  • Related