0

I have a method where I decode some information from a file, when I attempt to divide the value decoded by 10, let's say, it removes the last digit.

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

I know the value displayed should be 39.5.

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

I'm confused as to why it doesn't function... I'm sure it will be simple adjustment but it's driving me a little mad.

lornasw93
  • 182
  • 4
  • 26
  • 1
    You have to perform the conversion to the right numeric type (e.g., double), to be able to account for decimals. – varocarbas Feb 26 '14 at 11:04

2 Answers2

2

You are dividing an int with an int so result will be in int only. What you can do is :

 Label_1.Text = (DecodeInt(Rec, 22) / 10.0).ToString(); 
M.S.
  • 4,283
  • 1
  • 19
  • 42
0

I looked here for my solution: https://stackoverflow.com/a/661042/2952390

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

Much easier than I thought, of course.

Community
  • 1
  • 1
lornasw93
  • 182
  • 4
  • 26