【问题标题】:Dividing a decoded value, not minus/removing the last digit除以解码值,而不是减去/删除最后一位
【发布时间】:2014-03-29 03:48:04
【问题描述】:

我有一种方法可以从文件中解码一些信息,当我尝试将解码后的值除以 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

我对它为什么不起作用感到困惑......我确信这将是简单的调整,但它让我有点发疯。

【问题讨论】:

  • 您必须转换为正确的数字类型(例如,双精度数),才能考虑小数。

标签: c# winforms visual-studio .net-3.5 decode


【解决方案1】:

我在这里寻找我的解决方案:https://stackoverflow.com/a/661042/2952390

double result = (double)DecodeInt(Rec,20)/(double)10;

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

【讨论】:

    【解决方案2】:

    您将 int 与 int 相除,因此结果将仅在 int 中。你可以做的是:

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

    【讨论】:

      猜你喜欢
      • 2014-02-05
      • 2017-05-13
      • 1970-01-01
      • 1970-01-01
      • 2020-07-04
      • 2014-11-06
      • 1970-01-01
      • 1970-01-01
      • 2012-04-22
      相关资源
      最近更新 更多