简体   繁体   中英

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

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%.

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')

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)

要涵盖所有零的情况,您可以在语句中使用三元运算符

(current - previous) / previous * 100.0 if previous != 0 else float("inf") * abs(current) / current if current != 0 else 0.0

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

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.

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

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

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)

My solution:

def find_difference(a, b):
    return ((abs(a - b)) / ((a + b) / 2)) * 100

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM