How do you display the answer in as a decimal? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

How do you display the answer in as a decimal?

This code calculates the average of an array of integers. I know the answer is 6.88, but I cannot get it to display this way. I have tried using the following data types: integer, double, float, long, and, decimal. I have also tried using memberList.Format(0.00); using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace SoloLearn { class Program { class CalcAverages { } static void Main(string[] args) { int[] memberList = new int[8] { 2, 4, 9, 12, 3, 2, 15, 8}; Console.WriteLine(memberList.Sum()); double average = memberList.Sum() / memberList.Length; Console.WriteLine(average); } } }

7th Aug 2019, 6:14 PM
Emma
2 Answers
+ 3
Emma as you have used int data type that's why precision value is rounded. Use float instead of the int data type float[] memberList = new float[8] { 2, 4, 9, 12, 3, 2, 15, 8};
7th Aug 2019, 6:25 PM
MsJ
MsJ - avatar
0
Thanks Mohit. I thought it would be something obvious 🙂
7th Aug 2019, 6:36 PM
Emma