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.

Edward Karak
  • 10,658
  • 8
  • 44
  • 70
  • 3
    You can fix this error by passing a IFormatProvider instead of 2? Did you even read the error message? – Pierre-Luc Pineault Aug 04 '13 at 23:06
  • 1
    What do you want to see as binary form of *double* for ex 2.5 – I4V Aug 04 '13 at 23:08
  • 1
    @Pierre-LucPineault - I thought same at first but [appears valid](http://stackoverflow.com/questions/923771/quickest-way-to-convert-a-base-10-number-to-any-base-in-net), it may only work for integers for the base conversion here – Sayse Aug 04 '13 at 23:09
  • Yep. It only works with `int`s. – Edward Karak Aug 05 '13 at 21:40

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,225
  • 4
  • 65
  • 78