为什么我的百分比计算失败?
Why is my percentage calculation failing?
我试图根据上面的内容计算一个值(一件商品的总销售额)与另一个值(所有商品的总销售额)的百分比 here 并得出以下代码:
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 是数据集中所有 totalSales 值的总和:
decimal grandTotalPrice = 0.0M;
. . .
foreach (DataRow productUsageByMonthDataRow in dtUsage.Rows)
{
grandTotalPrice = grandTotalPrice + Convert.ToDecimal(productUsageByMonthDataRow["TotalPrice"]);
}
totPrice 是单独的 TotalPrice 值。
虽然没有得到我期望的值,例如 3.something 百分比(或 2.N,或 1.N,或 0.N),但我变得离谱每个 Percentage 成员中的值,例如“318940.70340793”,分配如下:
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)
};
. . .
在我测试的数据中,grandTotalPrice为687149.867,第一个Total Sales值为215.38;那怎么等于 318940.70340793?
更新
多亏了几个人的回答(我接受了第一个),以及我自己独特的花饰、花饰和巴洛克式姜饼,我最终得到了这个:
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);
}
根据您所说的,您的期望...您似乎在寻找项目 (1) 占总数的百分比。
例如,如果商品 1 = 10 美元且总成本 = 100 美元
那么你要找的百分比是10%?
在这种情况下,您只需将 itemCost 除以总成本,然后乘以 100
var percent = itemCost / totalCost * 100;
(10 / 100 * 100 ) = 10%
如果您实际上是在寻找增加的百分比,那么您得到的数字是正确的。
想想当有人说 "We have seen a 200% increase" 这真正意味着价值翻了一番......所以如果我们看看你得到的数字 318940.70340793%
如果我们将其除以 100,我们将得到 3189.407。
3189 * 215 = 687149.867(大约)
因此,如果您正在寻找比您得到的值正确的增长百分比,但是如果您正在寻找 Item1 与 GrandTotal 相比的百分比成本,请使用我上面的公式。
希望对您有所帮助!
我试图根据上面的内容计算一个值(一件商品的总销售额)与另一个值(所有商品的总销售额)的百分比 here 并得出以下代码:
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 是数据集中所有 totalSales 值的总和:
decimal grandTotalPrice = 0.0M;
. . .
foreach (DataRow productUsageByMonthDataRow in dtUsage.Rows)
{
grandTotalPrice = grandTotalPrice + Convert.ToDecimal(productUsageByMonthDataRow["TotalPrice"]);
}
totPrice 是单独的 TotalPrice 值。
虽然没有得到我期望的值,例如 3.something 百分比(或 2.N,或 1.N,或 0.N),但我变得离谱每个 Percentage 成员中的值,例如“318940.70340793”,分配如下:
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)
};
. . .
在我测试的数据中,grandTotalPrice为687149.867,第一个Total Sales值为215.38;那怎么等于 318940.70340793?
更新
多亏了几个人的回答(我接受了第一个),以及我自己独特的花饰、花饰和巴洛克式姜饼,我最终得到了这个:
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);
}
根据您所说的,您的期望...您似乎在寻找项目 (1) 占总数的百分比。
例如,如果商品 1 = 10 美元且总成本 = 100 美元 那么你要找的百分比是10%?
在这种情况下,您只需将 itemCost 除以总成本,然后乘以 100
var percent = itemCost / totalCost * 100;
(10 / 100 * 100 ) = 10%
如果您实际上是在寻找增加的百分比,那么您得到的数字是正确的。
想想当有人说 "We have seen a 200% increase" 这真正意味着价值翻了一番......所以如果我们看看你得到的数字 318940.70340793% 如果我们将其除以 100,我们将得到 3189.407。
3189 * 215 = 687149.867(大约)
因此,如果您正在寻找比您得到的值正确的增长百分比,但是如果您正在寻找 Item1 与 GrandTotal 相比的百分比成本,请使用我上面的公式。
希望对您有所帮助!