Dictionary を引数として受け取り、Dictionary を返すコードがあります。
このコードは、すべての double 値の合計を計算し、その合計を使用して各値が合計に占める割合を計算します。キーにパーセンテージが連結された新しい Dictionary を返します。
Web サーバーのイベント ビューアにいくつかの OverflowException が記録されています。ログには、次のコードで例外が発生したことが記録されていますDecimal percentage = (Decimal) (pDataPoints[key] / sum * 100);
。値を 10 進数にキャストするときに例外が発生したことが示されています。
どのようなエッジケースが欠落している可能性がありますか?
public static Dictionary<string, double> addPercentagesToDataPointLabels(Dictionary<string, double> pDataPoints)
{
Dictionary<string, double> valuesToReturn = new Dictionary<string, double>();
// First, compute the sum of the data point values
double sum = 0;
foreach (double d in pDataPoints.Values)
{
sum += d;
}
// Now, compute the percentages using the sum and add them to the new labels.
foreach (string key in pDataPoints.Keys)
{
string newKey = key;
Decimal percentage = (Decimal) (pDataPoints[key] / sum * 100);
percentage = Math.Round(percentage, ChartingValues.DIGITS_AFTER_DECIMAL_POINT);
newKey += " " + percentage.ToString() + "%";
valuesToReturn.Add(newKey, pDataPoints[key]);
}
return valuesToReturn;
}