add a percentage to a variable

hi this probably a really stupid question but I want to add a 20% to a variable, how can I do this? tried several things but none worked;

The variable I want to add 20% to is a variable I have for the Maximum exp points of my character, What I want to do is every time I reach the max experience points the current experience level is restarted to 0 and the maximum experience points gets 20% extra so next level is more difficult to reach.

both variables (CurExp(current exp) and MaxExp(Maximum exp)) are Int

Tried the following

if (CurExp >= MaxExp){

MaxExp = MaxExp + (MaxExp * (MaxExp/20))
}

if (CurExp >= MaxExp){
MaxExp += (MaxExp * (MaxExp/20))
}

if (CurExp >= MaxExp){
MaxExp += MaxExp += MaxExp%20;
}

And other ways I don’t remember right now.

Ok, 1:

MaxExp / 20 is 5% of MaxExp, not 20%! Basic maths kid. Try MaxExp / 5 instead.

2: The ‘%’ sign in common c-like languages (and pretty much every language I can think of) does not mean ‘percentage!’ It’s the modulo operator, and it basically gives you the remainder of a division. So say you have the expression

20 / 3

Now, 20 of course does not divide cleanly into 3! Because the standard behaviour is to round down, the result of this expression will be 6.

This is where the modulo operator comes in!

20 % 3

will give you ‘2’- the remainder from the previous expression! This way you can always make sure you are not losing values from your integer divisions.

Of course, the simplest solution is to just use a floating-point number, that can be divided cleanly.

tl;dr, use

MaxExp += (MaxExp / 5);

OR

MaxExp += Mathf.CielToInt(((float)MaxExp) / 5);

MaxExp *= 1.2;

% means divided by 100

5% is 5/100

5/100 * your variable will give you 5% of your variable

Variable += Variable * 5/100 = 105% of your variable.

If you want your final value to be an int just do

Variable += (int)(Variable * 5/100)

replace 5 with any percentage you require and you’ll get your answer.

Anything divided by 100 is also anything multiplied by 0.01 so 0.05 would give you 5% also. It would probably be better to do it that way to save your code one calculation.