0

我正在编写一个控制台应用程序,它输出输入的 base-10 数字的二进制平均值:

        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);

但是,编译器会在“Convert.ToString(average,2)”行生成构建时错误。错误:

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

我该如何解决这个错误?谢谢。

4

1 回答 1

0
double d = 2.2;
var bin =  String.Join("", BitConverter.GetBytes(d)
                          .Select(x => Convert.ToString(x, 2).PadLeft(8,'0')));
于 2013-08-04T23:15:20.397 回答