2017-06-05 23 views
0

檢查精確值我有一個列表這個如何在LINQ

Porfolio Value CumulativeWeights 
1246.540179  0.30873% 
1254.380054  0.33031% 
1258.260394  0.57136% 
1262.300797  4.98794% 
1262.710843  5.07213% 

首先:如果該列表包含第一個值大於5%選擇價值和 二:我要檢查列表包含確切5 %選擇該值。
如果不滿足上述兩個條件,我們做一些計算。所以如何選擇。

ConfidenceValue爲5%

private static double CaluculateInterpolation(List<AgeWeightedHistorical> Weights, Double ConfidenceValue, CalaculationSettings oCalcSettings) 
{ 
    double interpolation=0; 

    var CumulativeVal = Weights.Find(n => n.CumulativeWeights > ConfidenceValue); 
    if (CumulativeVal.CumulativeWeights >= ConfidenceValue) 
    { 
     interpolation = CumulativeVal.ProfolioPrices; 
    } 
    else 
    { 
     var LowestValues = Weights.Last(n => n.CumulativeWeights <= ConfidenceValue); 
     if (LowestValues.CumulativeWeights == ConfidenceValue) 
     { 
      interpolation = CumulativeVal.ProfolioPrices; 
     } 
     else 
     { 
      var HightValues = Weights.FirstOrDefault(n => n.CumulativeWeights >= ConfidenceValue); 
      double avgPrices = (LowestValues.ProfolioPrices + HightValues.ProfolioPrices)/2; 
      double avgWeights = (LowestValues.CumulativeWeights + HightValues.CumulativeWeights)/2; 
      if (avgWeights == ConfidenceValue) 
      { 
       interpolation = LowestValues.ProfolioPrices; 
      } 
      else 
      { 
       double Lowest_val = Math.Abs(LowestValues.CumulativeWeights - ConfidenceValue); 
       double Higest_val = Math.Abs(HightValues.CumulativeWeights - ConfidenceValue); 
       var FinalWeight = (Lowest_val < Higest_val) ? LowestValues : HightValues; 
       double minWeight = Math.Min(avgWeights, FinalWeight.CumulativeWeights)/100; 
       double maxWeight = Math.Max(avgWeights, FinalWeight.CumulativeWeights)/100; 
       interpolation = avgPrices - ((avgPrices - FinalWeight.ProfolioPrices) * (((1 - (oCalcSettings.PercentageLevelOfConfidence/100)) - minWeight)/(maxWeight - minWeight))); 
      } 
     } 
    } 
    return interpolation; 

} 

IM歌廳錯誤,請幫我

+0

您究竟遇到什麼錯誤? – PaulF

+0

沒有得到正確的值 – Jyothi

+4

完美的問題描述! 10/10 – pitersmx

回答

1

我建議在C#中,精密的計算,你應該使用

decimal 

這將避免許多的四捨五入問題..

全部替換十進制,檢查你的算法,它應該工作。