我正在编写一个控制台应用程序,它输出输入的基数为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
如何修复此错误?感谢。
答案 0 :(得分:0)
double d = 2.2;
var bin = String.Join("", BitConverter.GetBytes(d)
.Select(x => Convert.ToString(x, 2).PadLeft(8,'0')));