无法隐式转换类型'双'到'长' [英] Cannot implicitly convert type 'double' to 'long'

查看:173
本文介绍了无法隐式转换类型'双'到'长'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在此代码中,我得到了在线路上面的错误我评论。

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!

推荐答案

Math.Pow 方法返回一个双击,而不是,所以你需要改变你的代码考虑到这一点:

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));

这代码定投双击从结果 Math.Pow 并指定该值 X 。请记住,你将失去通过小数(这是一个浮点类型,并且可以表示十进制数值)providided所有的精度。铸造到将截断小数点后的一切。

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.

这篇关于无法隐式转换类型'双'到'长'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆