How to check change between two values (in percent)?

Question:

I have two values (previous and current) and I want to check change between them (in percent):

(current/previous)*100

But if the previous value is 0 I get Division By Zero, and if value will not change I get 100%.

Asked By: Nips

||

Answers:

You need to divide the change (current-previous) by the previous, not just the current. So, to make a long story short:

change_percent = ((float(current)-previous)/previous)*100

Note that if previous is 0 you cannot calculate the change in percentage (regardless of the python implementation)

Answered By: Mureinik

To cover all cases of zeroes, you could use ternary operators in your statement

(current - previous) / previous * 100.0 if previous != 0 else float("inf") * abs(current) / current if current != 0 else 0.0
Answered By: Brien
def get_change(current, previous):
    if current == previous:
        return 100.0
    try:
        return (abs(current - previous) / previous) * 100.0
    except ZeroDivisionError:
        return 0

Edit: some have commented that OP was describing a problem with the current code, not asking for this behavior, thus here is an example where, “if the current is equal to previous, there is no change. You should return 0”. Also I’ve made the method return Infinity if the previous value was 0, as there can be no real percentage change when the original value is 0.

  def get_change(current, previous):
    if current == previous:
        return 0
    try:
        return (abs(current - previous) / previous) * 100.0
    except ZeroDivisionError:
        return float('inf')
Answered By: Matt

You should divide by the absolute value of the previous number.
If the previous number is negative and the current number is negative you will get an erroneous result if you don’t use the absolute value in the denominator.
For example if your current number is -6 and previous number is -5:

(-6 - (-5)) / -5 = 

(-6 + 5) / -5 =  

-1 / -5 = 20 %

which is clearly false because the percentage change in this case should be negative -6 < -5. So use the function below:

def percentage_change(current, previous):
    if previous != 0 :
        return float(current - previous) / abs(previous) * 100
    else:
        return "undefined"

Keep in mind that if your previous number is zero division by zero is undefined: https://en.wikipedia.org/wiki/Division_by_zero

Also you shouldn’t use the absolute value of the nominator instead of the denominator.
Here is an example why:

previous value: -5

current value: -4

| (-4 - (-5)) | / -5 = 
| (-4 + 5) | / -5 =  
|1| / -5 = 
1 / -5 = -20%

which is false because -4 > -5

Correct:

(-4 - (-5)) / | -5 | = 
(-4 + 5) / | -5 | =  
1 / 5 = 20%

Reference: https://www.calculatorsoup.com/calculators/algebra/percent-change-calculator.php

Answered By: Nikos Tavoularis

I use this

  def pct_change(first, second):
    diff = second - first
    change = 0
    try:
        if diff > 0:
            change = (diff / first) * 100
        elif diff < 0:
            diff = first - second
            change = -((diff / first) * 100)
    except ZeroDivisionError:
        return float('inf')
    return change
Answered By: Caner
def get_percentage_diff(previous, current):
    try:
        percentage = abs(previous - current)/max(previous, current) * 100
    except ZeroDivisionError:
        percentage = float('inf')
    return percentage

This is a better way i discovered.

only covers under 100% diffs. if there is more than 100 percent increase this solution fails

Answered By: ruhi dağdelen

This is to correct answer taking formula from this website:

def get_percentage_diff(previous, current):
    try:
        percentage = abs(previous - current)/((previous + current)/2) * 100
    except ZeroDivisionError:
        percentage = float('inf')
    return percentage
    
difference = get_percentage_diff(500,1750)

Output: 111.111% difference

Answered By: Haseeb Mir

The most optimized way will be this I guess

def percentage_change(current,previous):
    if current and previous !=0:
       return round((current/previous)-1*100,2)
Answered By: Awais khan

My solution:

def find_difference(a, b):
    return ((abs(a - b)) / ((a + b) / 2)) * 100
Answered By: Magomed Shamaev
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.