may help you . I have to get the sum of a list of doubles. If the sum is > 100, I have to decrement from the highest number until it's = 100. If the sum is < 100, I have to increment the lowest number until it's = 100. I can do this by looping though the list, assigning the values to placeholder variables and testing which is higher or lower but I'm wondering if any gurus out there could suggest a super cool & efficient way to do this? The code below basically outlines what I'm trying to achieve: , I'd do it something like this:
var splitValues = new List<double?>();
splitValues.Add(Math.Round(assetSplit.EquityTypeSplit() ?? 0));
splitValues.Add(Math.Round(assetSplit.PropertyTypeSplit() ?? 0));
splitValues.Add(Math.Round(assetSplit.FixedInterestTypeSplit() ?? 0));
splitValues.Add(Math.Round(assetSplit.CashTypeSplit() ?? 0));
var listSum = splitValues.Sum(split => split.Value);
while (listSum != 100)
var value = listSum > 100 ? splitValues.Max() : splitValues.Min();
var idx = splitValues.IndexOf(value);
splitValues.Insert(idx, value + (listSum > 100 ? -1 : 1));
listSum = splitValues.Sum(split => split.Value);
How to get the highest and the lowest points in the hash?
With these it helps In the while loop you are mentioning min=max=grade the second time in the program. That is taking the value of -1 from grade which is used to stop the program. Please comment that line and try it should work.