无法隐式转换类型“double”到“长”

发布于 2024-10-08 10:47:31 字数 1394 浏览 7 评论 0原文

在此代码中,我在评论的行中遇到了上述错误。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

若沐 2024-10-15 10:47:31

Math.Pow 方法返回一个 double,而不是 long,因此您需要更改代码以解决此问题:

x = (long)(u % Math.Pow(10, m));

此代码将强制转换来自 Math.Powdouble 结果并将该值分配给 x。请记住,您将失去十进制(它是浮点类型,可以表示十进制值)提供的所有精度。转换为long 将截断小数点后的所有内容。

The Math.Pow method returns a double, not a long so you will need to change your code to account for this:

x = (long)(u % Math.Pow(10, m));

This code will cast the double result from Math.Pow and assign that value to x. Keep in mind that you will lose all the precision providided by decimal (which is a floating-point type and can represent decimal values). Casting to long will truncate everything after the decimal point.

眼眸里的快感 2024-10-15 10:47:31

将类型更改

long x;
long y;
long w;
long z; 

double x;
double y;
double w;
double z; 

Or make use of

Convert.ToInt64

Change types

long x;
long y;
long w;
long z; 

to

double x;
double y;
double w;
double z; 

Or make use of

Convert.ToInt64
像极了他 2024-10-15 10:47:31

Math.Pow 返回一个双精度值。

% 的右侧 (RHS) 只能是整数类型。

此外

x = u % (long)Math.Pow(10, m);///<----here
y = u / (long)Math.Pow(10, m);///here
w = v % (long)Math.Pow(10, m);///here
z = v / (long)Math.Pow(10, m);///here

,你还有除以零并毁灭宇宙的可能性。

Math.Pow returns a double.

the Right Hand Side (RHS) of % can only be an integer type.

you need

x = u % (long)Math.Pow(10, m);///<----here
y = u / (long)Math.Pow(10, m);///here
w = v % (long)Math.Pow(10, m);///here
z = v / (long)Math.Pow(10, m);///here

Additionally, You have the possibility of dividing by zero and destroying the universe.

一腔孤↑勇 2024-10-15 10:47:31

Math.Pow 返回一个双精度值。例如,您可以显式转换为 long,

x = u % (long)Math.Pow(10, m);

尽管这可能不是正确的解决方案。您确定您所追求的结果可以正确表示为双精度吗?如果不是,则将变量声明为双精度数而不是长整型。

Math.Pow returns a double. You could explicitly cast to long, for example

x = u % (long)Math.Pow(10, m);

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.

下壹個目標 2024-10-15 10:47:31

您不能将 double 隐式转换为 long、使用 (long) 转换或将变量声明的类型更改为 double。

You cant' cast implicitly double to long, use (long) cast or change type of variable declaration to double.

东走西顾 2024-10-15 10:47:31

您也可以使用此:

Convert.ToInt64( u % Math.Pow(10, m) )

来源此处

Also you can use this:

Convert.ToInt64( u % Math.Pow(10, m) )

Source here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文