Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/320.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 除以解码值,而不是减去/删除最后一个数字_C#_Winforms_Visual Studio_.net 3.5_Decode - Fatal编程技术网

C# 除以解码值,而不是减去/删除最后一个数字

C# 除以解码值,而不是减去/删除最后一个数字,c#,winforms,visual-studio,.net-3.5,decode,C#,Winforms,Visual Studio,.net 3.5,Decode,我有一种方法,当我尝试将解码的值除以10时,我解码文件中的一些信息,比如说,它删除了最后一个数字 private int DecodeInt(byte[] bytes, int start) { int r2 = 0; byte ch1 = bytes[start]; byte ch2 = bytes[start + 1]; int result = ch2 + (ch1 * 256); if (result > 32767) {

我有一种方法,当我尝试将解码的值除以10时,我解码文件中的一些信息,比如说,它删除了最后一个数字

private int DecodeInt(byte[] bytes, int start)
{
    int r2 = 0;
    byte ch1 = bytes[start];
    byte ch2 = bytes[start + 1];
    int result = ch2 + (ch1 * 256);

    if (result > 32767)
    {
        r2 = 0;
    }
    else
    {
        r2 = result;
    }

    return r2;
}
我知道显示的值应该是39.5

Label_1.Text = (DecodeInt(Rec, 22)).ToString(); // Displays 395
Label_1.Text = (DecodeInt(Rec, 22) / 10).ToString(); // Displays 39

我不明白为什么它不起作用。。。我相信这将是一个简单的调整,但这让我有点发疯。

您正在将一个int除以一个int,因此结果将仅为int。您可以做的是:

 Label_1.Text = (DecodeInt(Rec, 22) / 10.0).ToString(); 

我在这里寻找我的解决方案:


当然,这比我想象的要容易得多。

您必须执行到正确数字类型的转换(例如,双精度),才能计算小数。
double result = (double)DecodeInt(Rec,20)/(double)10;