你能解释一下如何将我的小数列表格式化为两位小数并且总数应该是 100.00?
static void Main(string[] args)
{
string decimalFormat = "0.00";
decimal[] individuals = { 10, 10, 10 };
decimal total = 30;
List<decimal> percents = new List<decimal>();
foreach (decimal t in individuals)
{
decimal percent = (t * 100) / total;
percents.Add(percent);
}
List<decimal> roundToTwoDecimalPercent = new List<decimal>();
foreach (decimal portfolio in percents)
{
roundToTwoDecimalPercent.Add(Math.Round(portfolio, 2));
}
decimal percentTotal = decimal.Zero;
foreach (decimal final in roundToTwoDecimalPercent)
{
percentTotal += final;
}
Console.WriteLine(percentTotal.ToString(decimalFormat)); // 99.99 but the EXPECTED OUTPUT IS 100.00
Console.ReadLine();
}
谢谢, S.Venkatesh