不能隐式地将类型“double”转换为“long”

时间:2022-05-01 16:05:06

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!

问题是什么?谢谢!

6 个解决方案

#1


16  

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

的数学。Pow方法返回一个double,而不是一个long,因此您需要更改代码来解释这一点:

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.

这段代码将会从数学中得到双重结果。将该值赋给x。记住,您将会丢失十进制的所有精度(这是浮点类型,可以表示十进制值)。连铸将会截断小数点后的一切。

#2


5  

Math.Pow returns a double.

数学。战俘返回一个双。

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

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

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.

此外,你有可能除以零和毁灭宇宙。

#3


3  

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

数学。战俘返回一个双。例如,您可以显式地转换为long。

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.

尽管这可能不是正确的解决方案。你确定你所追求的结果可以被正确地表达为一种双重的吗?如果没有,则将变量声明为double,而不是long。

#4


2  

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

#5


2  

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

您不能“隐式地将double转换为long,使用(long) cast或change类型的变量声明来加倍。

#6


1  

Also you can use this:

你也可以用这个:

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

Source here

源在这里

#1


16  

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

的数学。Pow方法返回一个double,而不是一个long,因此您需要更改代码来解释这一点:

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.

这段代码将会从数学中得到双重结果。将该值赋给x。记住,您将会丢失十进制的所有精度(这是浮点类型,可以表示十进制值)。连铸将会截断小数点后的一切。

#2


5  

Math.Pow returns a double.

数学。战俘返回一个双。

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

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

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.

此外,你有可能除以零和毁灭宇宙。

#3


3  

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

数学。战俘返回一个双。例如,您可以显式地转换为long。

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.

尽管这可能不是正确的解决方案。你确定你所追求的结果可以被正确地表达为一种双重的吗?如果没有,则将变量声明为double,而不是long。

#4


2  

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

#5


2  

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

您不能“隐式地将double转换为long,使用(long) cast或change类型的变量声明来加倍。

#6


1  

Also you can use this:

你也可以用这个:

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

Source here

源在这里