2012-07-25 85 views
2

請問如何格式化我的小數點到小數點後兩位小數位&總數應該是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

回答

5

基本上,

的圓形圖中的總和不一定等於的相同數字的圓形總和。

您只需輪到一次即可。

這會給你期望的輸出:

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); 
    } 

    decimal percentTotal = decimal.Zero; 
    foreach (decimal percent in percents) 
    { 
     percentTotal += percent; 
    } 

    Console.WriteLine(string.Format("{0:N2}", percentTotal)); 
    Console.ReadLine(); 
} 

OR:如果你是一個LINQ風扇像我一樣,這會給你同樣的結果:

static void Main(string[] args) 
{ 
    decimal[] individuals = { 10, 10, 10 }; 

    decimal total = individuals.Sum(); 

    decimal[] percentages = individuals.Select(i => i * 100/total).ToArray(); 

    decimal percentageTotal = percentages.Sum(); 

    Console.WriteLine(string.Format("{0:N2}", percentageTotal)); 

    Console.ReadLine(); 
} 

其他示例:使用以下測試應用程序:

static void Main(string[] args) 
{ 
    decimal[] individuals = { 10, 10, 10 }; 

    Console.WriteLine("Unrounded figures"); 

    var percentages = individuals.Select(i => i * 100/individuals.Sum()).ToList(); 

    percentages.ForEach(p => Console.WriteLine(p.ToString())); 

    decimal percentageTotal = percentages.Sum(); 
    Console.WriteLine("Their unrounded sum = {0}", percentageTotal); 
    Console.WriteLine("Their rounded sum = {0:N2}", percentageTotal); 

    Console.WriteLine(); 
    Console.WriteLine("Rounded figures"); 

    var roundedPercentages = individuals.Select(i => Math.Round(i * 100/individuals.Sum(), 2)).ToList(); 
    roundedPercentages.ForEach(p => Console.WriteLine(p.ToString())); 

    decimal roundedPercentageTotal = roundedPercentages.Sum(); 

    Console.WriteLine("Their unrounded sum = {0}", roundedPercentageTotal); 
    Console.WriteLine("Their rounded sum = {0:N2}", roundedPercentageTotal); 

    Console.ReadLine(); 
} 

我得到以下輸出:

output

+0

難道我要顯示小數點列表[百分之每33.33即作爲小數點後兩位,那麼它不會等於100% – user1531248 2012-07-25 07:27:16

+0

顯然不是。當您計算總和時,使用** unrounded **數字 - 然後當您將結果舍入時,它將被正確計算。經驗法則是,在計算時不要使用四捨五入的數字。當你**向用戶呈現**時,只有圓形數字。 – tobias86 2012-07-25 07:32:21

+0

@ user1531248:查看我更新的答案。我發佈了一個例子,展示了當您處理圓形和未被遮擋的數字時的不同之處。 – tobias86 2012-07-25 07:39:19