1

I am just needing to finish up this program for my assignment and I have completed the task that I want it to perform, yet I cannot for the life of me figure out how to make my output display the number value to the second decimal. (For example: 35.50)

My program is meant to take the average of values, and give the numeric average in decimals. It does do that, but the decimal string is way longer than 2 decimal places. I'm hoping to get some advice on how to clean this up, and please give all answers with the explanation. Thank you so much! (The program I am using is visual studios 2017, and I am creating this code within the console app of C#)

static void Main(string[] args)
    {

        decimal counter = 1;
        decimal sum = 0;
        decimal totalLoops = 3;


        while (counter <= totalLoops)
        {
            Console.WriteLine("Please enter test score here:");
            string scoreInput = Console.ReadLine();
            decimal score;
            decimal.TryParse(scoreInput, out score);
            sum += score;
            counter++;

        }

        Console.WriteLine("Your average is {0}", decimal.Round(sum, 2) / decimal.Round(totalLoops, 2));
        Console.ReadKey();

    }

}
sticky bit
  • 36,626
  • 12
  • 31
  • 42
brainpain
  • 23
  • 4
  • 2
    `"Your average is {0:N2}"` – mjwills Sep 18 '19 at 23:32
  • Visit https://stackoverflow.com/questions/15288134/how-to-display-values-only-upto-2-decimal-places *value.ToString("0.00"); – owais shahab Sep 18 '19 at 23:37
  • 1
    I'd also strongly recommend **not** doing rounding when doing mathematical calculations **until the last possible calculation**. In other words, don't round two numbers and then divide them. Instead, do the division and round **once** at the end to get your output (or use the technique I suggested to avoid the need for explicit rounding). – mjwills Sep 18 '19 at 23:40

3 Answers3

2

{0:N2} to get 2 decimals based on your locale. (The standard way)

{0:0.00} to always get 2 decimals, for ex: 2.00 will show 2.00.

{0:0.##} to show 2 decimals if they are non zero, for ex: 2.00 will show 2.

Please read these for your reference:

kurakura88
  • 2,185
  • 2
  • 12
  • 18
0

You want to force the string to show the decimals.

Also, you probably only want to round the result of of the average.

Console.WriteLine("Your average is {0:N2}", sum/totalLoops);
Terrence
  • 131
  • 5
-1

You can use Math.Round

Console.WriteLine("Your average is {0}", Math.Round(decimal.Round(sum, 2) / decimal.Round(totalLoops, 2), 2, MidpointRounding.AwayFromZero));
Kei
  • 1,026
  • 8
  • 15
  • This worked marvelously. Can you please explain why the math.round goes where it does and what the MidpointRounding.AwayFromZero does? I am wanting to understand. :) Thank you. – brainpain Sep 18 '19 at 23:37
  • You can Google what `MidpointRounding.AwayFromZero` does @brainpain. Basically it rounds from 0.5 upwards rather than doing bankers rounding. – mjwills Sep 18 '19 at 23:38
  • You should really only round if the rounded number is to be used in further calculations, not least because the logging code becomes less idiomatic and readable. Also, if `sum` is defined to only one decimal place (e.g., 35.5), then this prints `35.5`, not `35.50` as desired in the original question. – Arseniy Banayev Sep 19 '19 at 00:30