Write a C# program to retrieve the values from the text file and print the average of the values,formatted with two decimal places.
So I have already coded and created a text file named "averages.txt" which there are 4 numbers in the file, and my program is already retrieving data in my text file and outputting the values.
What I'm having trouble with is where I need to find the averages of the 4 numbers in the file and then formatting the numbers into 2 decimal places.
My code:
static void Main(string[] args)
{
string innerNum;
string outerNum = "";
if (File.Exists("averages.txt"))
{
try
{
StreamReader inFile = new StreamReader("averages.txt");
while ((innerNum = inFile.ReadLine()) != null)
{
outerNum += innerNum + "\n";
}
Console.WriteLine(outerNum);
}
catch (System.IO.IOException e)
{
Console.WriteLine("IO Exception: " + e.Message);
}
}
else
{
Console.WriteLine("The File Does Not Exist.");
}
Console.Write("Press Enter to Exit.");
Console.ReadKey();
}
}
}