title: How to convert 4 byte IEEE (little endian) float binary representation to float
body: <p>I am decoding a binary file, which has decimal numbers represented by four bytes, little endian. For example, <code>94 53 F0 40</code> represents 7.510202. Unfortunately, Python is giving me 7.51020240784.</p>
  
  <p>When I try to parse this data using <code>unpack("&lt;f",sampledata)[0]</code> I don't get exact representations of the original, due to the way Python stores values (for more information, see <a href="http://bugs.python.org/issue4114" rel="nofollow">http://bugs.python.org/issue4114</a>).</p>
  
  <p>Unfortunately, I <em>do</em> need to get the <em>exact</em> same representation- regardless of discussions about the inaccuray of floats, because I need to write these values to a text file, with the same number of decimal places as they were initially written to the binary file with.</p>
  
  <p>I'd rather stick to Python if possible, but am happy to implement a solution in C if necessary. The reason I cannot simply truncate the return of the unpack function, is that I cannot guarantee how many decimal places the original float had, for example <code>0C 02 0F 41</code> represents 8.938 according to my hex editor, from the original binary file, which only has 3 decimal places.</p>
  
  <p>To be clear, I need to take four hex bytes as my input, and output either a text/ASCII or number representation of the IEEE 32-bit floating point number, that has the same number of decimal places as was intended by the creator of the file. The output I will use to create a CSV of the original binary data file, not for actually performing any calculations.</p>
  
  <p>Any suggestions?</p>
  
  <p>Example:</p>
  
  <pre><code>from __future__ import print_function
  from struct import *
  
  print("Should print 7.510202")
  
  hexbytes = b"\x94\x53\xF0\x40"
  
  # 01101001 11001000 11110001 01000000
  # should print 7.510202
  
  print(unpack("&lt;f",hexbytes)[0])
  </code></pre>