# Solved Convert XP to Level

Discussion in 'Plugin Development' started by padili, Jun 16, 2014.

Not open for further replies.
1. Offline

Hey Guys, what's the formula to convert the given xp to the minecraft level?

#1
2. Offline

padili This isn't Google. I'll bet there aren't many who've memorised the formula, and how do you think we're going to get to it? Or even those who do know it off by heart - how do you think they learned it?

#2
3. Offline

Found it on Minecraft Wiki it works now:
Code:java
`public static int getXPForLevel(int lvl) {        if (lvl <= 15) {            return lvl  * 17;        }else if (lvl > 16 && lvl < 31) {            return (int) (1.5 * Math.pow(lvl, 2) - 29.5 * lvl + 360);        }else if (lvl > 30) {            return (int) (3.5 * Math.pow(lvl, 2) - 151.5 * lvl + 2220);        }        return 0;    }`

But I can't convert it back!

#3
4. Offline

padili What do you mean "convert it back"?

#4
5. Offline

Get Level for xp, the opposite

#5
6. Offline

padili Surely you can figure that out by reading the wiki page you found? They've even got a lovely little table there and everything.

#6
7. Offline

### Necrodoom

padili Since you already have level to XP, simply reverse the math operations to get XP to level.

#7
8. Offline

Necrodoom Who ever said that basic algebra wasn't useful?

#8
9. Offline

`public static int getXPForLevel(int lvl) {        if (lvl <= 15) {            return lvl  * 17;        }else if (lvl > 16 && lvl < 31) {            return (int) (1.5 * Math.pow(lvl, 2) - 29.5 * lvl + 360);        }else if (lvl > 30) {            return (int) (3.5 * Math.pow(lvl, 2) - 151.5 * lvl + 2220);        }        return 0;    }    public static int getLvlForXP(int xp) {        if (xp <= 255) {            return xp  / 17;        }else if (xp > 272 && xp < 887) {            return (int) ((Math.sqrt(24 * xp - 5159) + 59) / 6);        }else if (xp > 825) {            return (int) ((Math.sqrt(56 * xp - 32511) + 303) / 14);        }        return 0;    }`