title: C# double to real
body: <p>i am trying to convert double to pascal real but when i convert 0.23 to real i got 0.23999999 real how can i truncate all 9999 to 0000.</p>
  
  <pre><code>public static byte[] Double2Real48(double d)
  {
      byte[] r48 = new byte[6];
      byte[] da = BitConverter.GetBytes(d);
  
      for (int i = 0; i &lt; r48.Length; i++)
          r48[i] = 0;
  
      //Copy the negative flag
      r48[5] |= (byte)(da[7] &amp; 0x80);
  
      //Get the expoent
      byte b1 = (byte)(da[7] &amp; 0x7f);
      ushort n = (ushort)(b1 &lt;&lt; 4);
      byte b2 = (byte)(da[6] &amp; 0xf0);
      b2 &gt;&gt;= 4;
      n |= b2;
  
      if (n == 0)
          return r48;
  
      byte ex = (byte)(n - 1023);
      r48[0] = (byte)(ex + 129);
  
      //Copy the Mantissa
      r48[5] |= (byte)((da[6] &amp; 0x0f) &lt;&lt; 3);//Get the last four bits
      r48[5] |= (byte)((da[5] &amp; 0xe0) &gt;&gt; 5);//Get the first three bits
  
      r48[4] = (byte)((da[5] &amp; 0x1f) &lt;&lt; 3);//Get the last 5 bits
      r48[4] |= (byte)((da[4] &amp; 0xe0) &gt;&gt; 5);//Get the first three bits
  
      r48[3] = (byte)((da[4] &amp; 0x1f) &lt;&lt; 3);//Get the last 5 bits
      r48[3] |= (byte)((da[3] &amp; 0xe0) &gt;&gt; 5);//Get the first three bits
  
      r48[2] = (byte)((da[3] &amp; 0x1f) &lt;&lt; 3);//Get the last 5 bits
      r48[2] |= (byte)((da[2] &amp; 0xe0) &gt;&gt; 5);//Get the first three bits
  
      r48[1] = (byte)((da[2] &amp; 0x1f) &lt;&lt; 3);//Get the last 5 bits
      r48[1] |= (byte)((da[1] &amp; 0xe0) &gt;&gt; 5);//Get the first three bits
  
      return r48;
  
  }
  </code></pre>