Reversing a Percentage

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    I want to add x% (let’s say 20) to a value then bring the value back to what it would be without the percent ( so like 100 goes up to 120 then down to 100 again)
  2. What is the issue? Include screenshots / videos if possible!
    I can’t just subtract the percentage like I would when I add the percentage because it wont return the same percentage. There could be a possible equation but I cant figure it out
  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    note; I can not just return it to the orginal variable by making it a separte variable before the increase then bringing it back. Hard to explain but just can’t
    note2; I could just find the exchange ( adding 20% = subtracting 16.6…7%) but Im just curious to see if there are any other solutions
-- Results in different numbers, In this case Val = 100
Val = Val + (Val / 100 * 20) --  Val = 120
wait(time)
Val = Val - (Val / 100 * 20) -- Val = 96

Please do not ask people to write entire scripts or design entire systems for you. If you can’t answer the three questions above, you should probably pick a different category.

You could have a BeforeVal and AfterVal, the AfterVal being the only one to change by 20%.

I explained that I could not do that in the first note.

So if I’m understanding correctly you’re trying to figure out what percent of x is equal to y?

If so your formula would be percent = y * 100 / x

So for example if you’re looking for what percent of 120 is equal to 100 you can do:

local startNum = 120
local goalNum = 100
local percent = goalNum * 100 / startNum
print(percent) --> 0.8333...

Now you can multiply percent / 100 by goalNum and your output will be goalNum

print(percent / 100 * startNum) --> 100

So to reiterate in your case,

local startNum = val
val += val / 100 * 20
task.wait(time)
-- now to "reverse" or find what percent of val is 100, you'd do:
local percent = startNum * 100 / goal-- remember equal to 83.3333...
local reversed = val * percent / 100 -- 'reversed' should now be 100 again, remember we need to divide 'percent' by 100 because percentages are really decimals (83% is equal to 0.83)

You want to get the original value without any modification, right? If so then this equation could help you:

X + X * 0.2 = 120 -- X is the original value
X * 1.2 = 120
X = 120 / 1.2
X = 100

The equation X + X * 0.2 = X * 1.2 is true due to multiplication’s distributive property

X + X * 0.2 = X * 1.2
X * (1 + 0.2) = X * 1.2

So you just have to add 100% to the used percentage (20% + 100%) and divide the modified value by that sum (120 / 120%) then you have the original value (100)

Do you understand? I’m not good at teaching math :dotted_line_face:

local value = 500
local percent = 20
local a, b = 0, 0

a = value + percent * value / 100 
print(percent .. "% added to " .. value .. " = " .. a)

b = a / (percent / 100 + 1)
print(percent .. "% subtracted from " .. a .. " = " .. b)

I used 500, so you wouldn’t confuse where the 100’s came from.
And I posted to the wrong person again😒

I guess I did use a as a vaule I saved … but, I didn’t need to.
All I needed was what is the value and what percent are we dealing with.

print(600 / (20 / 100 + 1))

What 100’s? Can you explain me what am I confusing?

a = value + percent * value / 100 ← that one
b = a / (percent / 100 + 1)
this one -------------^
Both are needed to do these calculations

This is every day math, I’m just showing you how to code that.
A yelds the added percentage
B yelds the percentage subtracted

The 100 is the default percentage ratio. You know, when you want a % of something, you always have 100 as the maximum %.
" In mathematics, a percentage is a number or ratio expressed as a fraction of 100"

So do you mean that instead of writing percentages like 50% or 50 / 100, I’m writing them in decimals like 0.5? If so, I’m used to do that instead of writing it like it should be lol.

You need to drop to a decimal to figure the percent subtracted.
Why are you even talking to me. Am I somehow offending you.

1 Like

Yeah, I know that I shouldn’t write it in decimals if I want to get the percent.
I just did that since Astereaux wants to figure the original value, not the percentage

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.