'Increase or decrease a number by "x" percentage
How do I increase or decrease an "X" number by a certain amount of percentage say, 2% or 3%?
Case scenario: My value is 5 - I want to increase this number by 2% - so the final value will be 5.1 after 2% percentage increase.
And the same would be if I want to decrease this number by an "X" percentage.
I hope that makes sense, thank you.
Solution 1:[1]
A percent is just a fraction of 100, so multiply by 1 + your percent over 100:
>>> x = 5
>>> x *= (1 + 2/100)
>>> x
5.1
Another way to think of it is incrementing x by x times that percent (these operations are algebraically equivalent):
>>> x = 5
>>> x += (x * 2/100)
>>> x
5.1
Solution 2:[2]
If you want to increase by a percent, multiply the number by (1+{percent}). To decrease by a percent, multiply the number by (1-{percent}). Given your example:
- Increase 5 by 2%:
5*(1+0.02)=5*1.02=5.1 - Decrease 5 by 2%:
5*(1-0.02)=5*0.98=4.9.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Samwise |
| Solution 2 | Rich |
