Problem Caluclating Percentage Difference

62 Views Asked by At

I am using the following to calculate the percentage difference.

priceDifference= newPrice - oldPrice;
priceDifferences= priceDifference/ oldPrice;

But When I use the caulation on two numbers Old Price £72.50 New Pirce £150.I only get a difference of 1.07% which is totally incorrect can anyone help me its to show in a program for example if a supplier has put their prices up or down by 20% to highlight it.

Sorry if I tagged the question wrong as new to the maths forum.

Edit 1 Sorry I forgot renamed the functions for the post

Edit 2 If I used this form with the values 72.50 and 150 it in-correctly shows its an increase of 10,689.66 when should be 106 %

percentage = (newPrice - oldPrice) / oldPrice * 100;
1

There are 1 best solutions below

2
On BEST ANSWER

You just forget to multiply by $100$ to converge your code to percentage. It should be

priceDifferences = priceDifference/ oldPrice * 100

or better still for clearer naming of variable:

percentageChange = priceDifference/ oldPrice * 100

We can indeed obtain the answer of $106.896551724 \%$.

To detect drastic changes, you can set a threshold and use an if statement to check

if abs(percentageChange) >= threshold:
     print("drastic change detected")

I am including a Python code to illustrate that it works.