无法隐式转换类型“double”到“长”
在此代码中,我在评论的行中遇到了上述错误。
public double bigzarb(long u, long v)
{
double n;
long x;
long y;
long w;
long z;
string[] i = textBox7.Text.Split(',');
long[] nums = new long[i.Length];
for (int counter = 0; counter < i.Length; counter++)
{
nums[counter] = Convert.ToInt32(i[counter]);
}
u = nums[0];
int firstdigits = Convert.ToInt32(Math.Floor(Math.Log10(u) + 1));
v = nums[1];
int seconddigits = Convert.ToInt32(Math.Floor(Math.Log10(v) + 1));
if (firstdigits >= seconddigits)
{
n = firstdigits;
}
else
{
n = seconddigits;
}
if (u == 0 || v == 0)
{
MessageBox.Show("the Multiply is 0");
}
int intn = Convert.ToInt32(n);
if (intn <= 3)
{
long uv = u * v;
string struv = uv.ToString();
MessageBox.Show(struv);
return uv;
}
else
{
int m =Convert.ToInt32(Math.Floor(n / 2));
x = u % Math.Pow(10, m); // here
y = u / Math.Pow(10, m); // here
w = v % Math.Pow(10, m); // here
z = v / Math.Pow(10, m); // here
long result = bigzarb(x, w) * Math.Pow(10, m) + (bigzarb(x, w) + bigzarb(w, y)) * Math.Pow(10, m) + bigzarb(y, z);///here
textBox1.Text = result.ToString();
return result;
}
}
有什么问题吗?谢谢!
In this code i got the above error in lines i commented.
public double bigzarb(long u, long v)
{
double n;
long x;
long y;
long w;
long z;
string[] i = textBox7.Text.Split(',');
long[] nums = new long[i.Length];
for (int counter = 0; counter < i.Length; counter++)
{
nums[counter] = Convert.ToInt32(i[counter]);
}
u = nums[0];
int firstdigits = Convert.ToInt32(Math.Floor(Math.Log10(u) + 1));
v = nums[1];
int seconddigits = Convert.ToInt32(Math.Floor(Math.Log10(v) + 1));
if (firstdigits >= seconddigits)
{
n = firstdigits;
}
else
{
n = seconddigits;
}
if (u == 0 || v == 0)
{
MessageBox.Show("the Multiply is 0");
}
int intn = Convert.ToInt32(n);
if (intn <= 3)
{
long uv = u * v;
string struv = uv.ToString();
MessageBox.Show(struv);
return uv;
}
else
{
int m =Convert.ToInt32(Math.Floor(n / 2));
x = u % Math.Pow(10, m); // here
y = u / Math.Pow(10, m); // here
w = v % Math.Pow(10, m); // here
z = v / Math.Pow(10, m); // here
long result = bigzarb(x, w) * Math.Pow(10, m) + (bigzarb(x, w) + bigzarb(w, y)) * Math.Pow(10, m) + bigzarb(y, z);///here
textBox1.Text = result.ToString();
return result;
}
}
Whats is the problem? Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
Math.Pow
方法返回一个double
,而不是long
,因此您需要更改代码以解决此问题:此代码将强制转换来自
Math.Pow
的double
结果并将该值分配给x
。请记住,您将失去十进制(它是浮点类型,可以表示十进制值)提供的所有精度。转换为long
将截断小数点后的所有内容。The
Math.Pow
method returns adouble
, not along
so you will need to change your code to account for this:This code will cast the
double
result fromMath.Pow
and assign that value tox
. Keep in mind that you will lose all the precision providided bydecimal
(which is a floating-point type and can represent decimal values). Casting tolong
will truncate everything after the decimal point.将类型更改
为
Or make use of
Change types
to
Or make use of
Math.Pow 返回一个双精度值。
% 的右侧 (RHS) 只能是整数类型。
此外
,你还有除以零并毁灭宇宙的可能性。
Math.Pow returns a double.
the Right Hand Side (RHS) of % can only be an integer type.
you need
Additionally, You have the possibility of dividing by zero and destroying the universe.
Math.Pow 返回一个双精度值。例如,您可以显式转换为 long,
尽管这可能不是正确的解决方案。您确定您所追求的结果可以正确表示为双精度吗?如果不是,则将变量声明为双精度数而不是长整型。
Math.Pow returns a double. You could explicitly cast to long, for example
although that is likely not the correct solution. Are you certain that the results that you are after can be properly expressed as a double? If not then change the variables to be declared as doubles rather than longs.
您不能将 double 隐式转换为 long、使用 (long) 转换或将变量声明的类型更改为 double。
You cant' cast implicitly double to long, use (long) cast or change type of variable declaration to double.
您也可以使用此:
来源此处
Also you can use this:
Source here