0

I am writing a Console application that outputs the binary average of inputted base-10 numbers:

        Console.WriteLine("Enter numbers to find the average of. Seperate each  number with a pound sign(#)");
        string[] n = Console.ReadLine().Split('#');
        List<string> final = new List<string>();
        final.AddRange(n);
        double t = 0;
        for (int i = 0; i < final.Count; i++)
        {
            t = t + Convert.ToDouble(final[i]);
        }
        int ct = final.Count;
        double average = t / ct;
        string binAv = Convert.ToString(average, 2);

However, the compiler generates a build-time error on the "Convert.ToString(average,2)" line. The error:

The best overloaded method match for 'System.Convert.ToString(double, System.IFormatProvider)' has some invalid arguments

How can I fix this error? Thanks.

lost_in_the_source
  • 10,998
  • 9
  • 46
  • 75

1 Answers1

0
double d = 2.2;
var bin =  String.Join("", BitConverter.GetBytes(d)
                          .Select(x => Convert.ToString(x, 2).PadLeft(8,'0')));
I4V
  • 34,891
  • 6
  • 67
  • 79