C# Convert.ToString生成时错误
我正在编写一个控制台应用程序,它输出输入的10进制数的二进制平均值:C# Convert.ToString生成时错误,c#,binary,C#,Binary,我正在编写一个控制台应用程序,它输出输入的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<strin
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);
Console.WriteLine(“输入数字以查找平均值。用磅号(#)分隔每个数字”);
字符串[]n=Console.ReadLine().Split('#');
最终列表=新列表();
最终添加范围(n);
双t=0;
for(int i=0;i
但是,编译器在“Convert.ToString(average,2)”行上生成生成时错误。
错误:
与“System.Convert.ToString(double,System.ifformatprovider)”匹配的最佳重载方法具有一些无效参数
如何修复此错误?谢谢。您可以通过传递IFormatProvider而不是2来修复此错误?你看过错误消息了吗?你想看到什么是ex2的二进制形式的double。5@Pierre-LucPineult-我起初也这么认为,但它可能只适用于基数转换的整数。它仅适用于
int
s。
double d = 2.2;
var bin = String.Join("", BitConverter.GetBytes(d)
.Select(x => Convert.ToString(x, 2).PadLeft(8,'0')));