## 11 replies

Last post Jul 07, 2017 08:43 AM by david39ni

Member

2 Points

38 Posts

### Calculation to find out price difference in percentage.

I am trying to find the price difference in percentage between two prices new and old price I have tried the following calculations but its not working. I want to be able to highlight prices that have changed by 20%. Its a c# winforms based app but I presume the c# code would be the same.

(current / maximum) * 100. In your case, (76.10 / 10) * 100.

All-Star

28467 Points

7108 Posts

### Re: Calculation to find out price difference in percentage.

Jul 05, 2017 08:13 AM|Rajneesh Verma|LINK

Formula:

Step 1: Calculate the change (subtract old value from the new value)
Step 2: Divide that change by the old value (you will get a decimal number)
Step 3: Convert that to a percentage (by multiplying by 100 and adding a "%" sign)

Code:

public static void Main()
{
long current = 150;
long previous = 100;

var change = current - previous;
Console.WriteLine((((double)change / previous)*100).ToString());
}

Working: https://dotnetfiddle.net/ljrnB8

Member

2 Points

38 Posts

### Re: Calculation to find out price difference in percentage.

Hi All Thanks for the reply so far

Thanks for reply I used something similar as follows.

public string calculatePercentageDiff(decimal oldPrice, decimal newPrice)
{
decimal priceDecrease = newPrice - oldPrice;
decimal percentDecrease = priceDecrease / oldPrice;

return percentDecrease.ToString();
}

https://dotnetfiddle.net/l4oOKS

But for the values of

New Price

 150

Old Price

72.5

But its showing a value of 1.0689  which would not be right but when i do the to p on the string using this

string _percentage = calculatePercentageDiff(Convert.ToDecimal(Row.Cells["Price"].Value), Convert.ToDecimal(Row.Cells["NSterling"].Value));
Row.Cells["Percentage"].Value = Convert.ToDecimal(_percentage).ToString("p");

It inserts 106.90 % which is correct but I need to be able to do an if statment like follows

if (Convert.ToDecimal(_percentage) > Convert.ToDecimal(1.2))
{
Row.DefaultCellStyle.BackColor = Color.OrangeRed;
}

But because the calculation is working out below 1.2 its not highlighting it needs to show if the amount is greater than 20% yield or 20% less than old price. Screen shot as below wonder what could be the problem it should obv highlight it red.

Contributor

3760 Points

1642 Posts

### Re: Calculation to find out price difference in percentage.

Jul 06, 2017 08:46 AM|Eric Du|LINK

Hi david39in,

According to your description, I make a sample to calculate the discount the old price and new price in asp.net. But the code is the same as in the winform. Please check:

Sample Code:

<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />

protected void Button1_Click(object sender, EventArgs e)
{
string NewPrice = "100";
string OldPrice = "150";
var a = 100 - Math.Round((Convert.ToDecimal(NewPrice) / Convert.ToDecimal(OldPrice) * 100), 2) + "%";
Response.Write("Discount: " + a);
}

Best Regards,

Eric Du

MSDN Community Support
If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

Member

2 Points

38 Posts

### Re: Calculation to find out price difference in percentage.

Eric can you see my above edit please and see what could be wrong with my existing caulcation ? And its not the discount its a yeild wanring to our manages to see why a supplier has increased x product by 20%

Regards

Member

2 Points

38 Posts

### Re: Calculation to find out price difference in percentage.

If I use your calcuation on above it gives minus

 Product Code Product Name 72.5 150 98.6 98.6 -10,690.00 %

Member

2 Points

38 Posts

### Re: Calculation to find out price difference in percentage.

It should be same as this form caulatlates it

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

Star

11649 Points

2962 Posts

### Re: Calculation to find out price difference in percentage.

double v1 = 150.0;
double v2 = 72.5;
double perDiff = Math.Abs((v1 - v2)) / ((v1 + v2) / 2) * 100.0;

Member

2 Points

38 Posts

### Re: Calculation to find out price difference in percentage.

That works fantastic mate thank you but its showing the price as  11,409.19  when it should be 114.092% . I am using the following to attach the precentage sign.

Row.Cells["Percentage"].Value = Convert.ToDecimal(_percentage).ToString("p");

public Double  calculatePercentageDiff(double oldPrice, double newPrice)
{
// decimal percentage = (newPrice - oldPrice) / oldPrice;

double v1 = 150.0;
double v2 = 72.5;
double perDiff = Math.Abs((newPrice - oldPrice)) / ((newPrice + oldPrice) / 2) * 100.0;

return perDiff;
}

Contributor

3760 Points

1642 Posts

### Re: Calculation to find out price difference in percentage.

Jul 07, 2017 08:33 AM|Eric Du|LINK

Hi david39ni,

You need add % after the perDiff, then change the type from double to string. Please check the sample Code:

Sample Code:

protected void Button1_Click(object sender, EventArgs e)
{
Response.Write(calculatePercentageDiff(100, 200));
}
public string calculatePercentageDiff(double oldPrice, double newPrice)
{
var perDiff = Math.Abs((newPrice - oldPrice)) / ((newPrice + oldPrice) / 2) * 100.0 + "%";

return perDiff;
}

Best Regards,

Eric Du

MSDN Community Support
If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

Member

2 Points

38 Posts

### Re: Calculation to find out price difference in percentage.

Hi Eric thanks can I ask as to why that is does the Format string mess it up I gather ?

Member

2 Points

38 Posts

### Re: Calculation to find out price difference in percentage.

Eric Its showing for the price difference

Old Price

13.93

New Price

50

Giving me a percentage of 114.091 when it should be 112.842%  according to this form

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

But then according to this one it should be  258.9375

https://www.easycalculation.com/mortgage/price-difference-percentage-calculator.php