B. Clay Shannon - 9 months ago 57
C# Question

# Why is my percentage calculation failing?

I tried to calculate the percentage of one value (total sales for one item) compared to another (total sales for all items) based on what it says here and came up with this code:

private double GetPercentageOfItem(decimal totPrice, decimal grandTotalPrice)
{
if ((totPrice <= 0.0M) || (grandTotalPrice <= 0.0M))
{
return 0.0;
}
if (totPrice == grandTotalPrice)
{
return 100.0;
}
//First: work out the difference (increase) between the two numbers you are comparing.
//Increase = New Number - Original Number.
double diff = Convert.ToDouble(grandTotalPrice) - Convert.ToDouble(totPrice);
//Then: divide the increase by the original number and multiply the answer by 100.
double prcntg = diff / Convert.ToDouble(totPrice);
//% increase = Increase ÷ Original Number × 100.
return prcntg*100;
}


grandTotalPrice is the sum of all totalSales values in a dataset:

decimal grandTotalPrice = 0.0M;
. . .
foreach (DataRow productUsageByMonthDataRow in dtUsage.Rows)
{
grandTotalPrice = grandTotalPrice + Convert.ToDecimal(productUsageByMonthDataRow["TotalPrice"]);
}


totPrice are the individual TotalPrice values.

Instead of getting values I expect, though, such as 3.something percent (or 2.N, or 1.N, or 0.N), I'm getting outrageous values such as "318940.70340793" in each Percentage member, which is assigned like so:

foreach (DataRow productUsageByMonthDataRow in dtUsage.Rows)
{
. . .
Decimal totPrice
Convert.ToDecimal(productUsageByMonthDataRow["TotalPrice"]);
. . .
var pupd = new ProduceUsagePivotData
{
ItemCode = itemCode,
ItemDescription = desc,
Unit = unit,
MonthYear = monthYear,
Quantity = qty,
TotalPrice = totPrice,
IsContractItem = contractItem,
Percentage = GetPercentageOfItem(totPrice, grandTotalPrice)
};
. . .


In the data I am testing, grandTotalPrice is 687149.867, and the first Total Sales value is 215.38; so how does that equate to 318940.70340793?

## UPDATE

Thanks to the answers of several (I accepted the first one), as well as my own inimitable flourishes, curlicues, and baroque gingerbreading, I ended up with this:

private double GetPercentageOfItem(decimal totPrice, decimal grandTotalPrice)
{
if ((totPrice <= 0.0M) || (grandTotalPrice <= 0.0M))
{
return 0.0;
}
if (totPrice == grandTotalPrice)
{
return 100.0;
}
double d = Convert.ToDouble(totPrice) / Convert.ToDouble(grandTotalPrice) * 100;
return Math.Round(d, 2);
}


Based on what you are saying you expect... It seems as though you are looking for what percent item(1) is of the total.

For example if Item1 = $10 and the totalCost =$100 Then the percent you are looking for is 10%?

In this case you simply need to divide the the itemCost by the totalcost and multiply by 100

 var percent = itemCost / totalCost * 100;


(10 / 100 * 100 ) = 10%

If you are in fact looking for the percent increase then the number you are getting is correct.

Think of when someone says "We have seen a 200% increase" what this really means is the value has doubled... So if we look at the number you got 318940.70340793% If we divide this by 100, we get 3189.407.

3189 * 215 = 687149.867 (approx)

So if you are looking for percent increase than the values you are getting are correct, however if you are looking for the percent cost of Item1 compared with the GrandTotal then use my above formula.

Hope this helps!