Float value behaviour in Python 2.6 and Python 2.7
<p>I have to convert string to tuple of float.
In Python 2.7, it gives correct conversion, but in Python it is not same case.</p>

<p>I want same behaviour in Python 2.6 </p>

<p>Can anyone help me why this is not same in Python 2.6 and how to do in Python 2.6.</p>

<p><strong>Python 2.6</strong></p>

<pre><code>&gt;&gt;&gt; a
'60.000,494.100,361.600,553.494'
&gt;&gt;&gt; eval(a)
(60.0, 494.10000000000002, 361.60000000000002, 553.49400000000003)
&gt;&gt;&gt; import ast
&gt;&gt;&gt; ast.literal_eval(a)
(60.0, 494.10000000000002, 361.60000000000002, 553.49400000000003)
&gt;&gt;&gt; 

&gt;&gt;&gt; for i in a.split(","):
...   float(i)
... 
60.0
494.10000000000002
361.60000000000002
553.49400000000003
&gt;&gt;&gt; 
</code></pre>

<p><strong>Python 2.7</strong></p>

<pre><code>&gt;&gt;&gt; a
'60.000,494.100,361.600,553.494'
&gt;&gt;&gt; eval(a)
(60.0, 494.1, 361.6, 553.494)
&gt;&gt;&gt; import ast
&gt;&gt;&gt; ast.literal_eval(a)
(60.0, 494.1, 361.6, 553.494)
&gt;&gt;&gt; 

&gt;&gt;&gt; for i in a.split(","):
...   float(i)
... 
60.0
494.1
361.6
553.494
</code></pre>

<p>Its not look good</p>

<p><strong>[Edit 2]</strong></p>

<p><strong>I just print value and condition</strong></p>

<pre><code>print fGalleyTopRightOddX, "&gt;=", tLinetextBbox[2], fGalleyTopRightOddX&gt;=tLinetextBbox[2]

361.6 &gt;= 361.6 False
</code></pre>

<p>I calculate <code>tLinetextBbox</code> value from string and which is <code>361.60000000000002</code> and <code>fGalleyTopRightOddX</code> value is <code>361.6</code></p>

<p>I am working on <strong>Python Django</strong> project where <strong>apache</strong> is server.</p>

<ol>
<li><code>fGalleyTopRightOddX</code> i.e. <code>361.6</code> is calculated in apache environment</li>
<li><code>tLinetextBbox</code> i.e. <code>361.60000000000002</code> is calculated on cmd means I pass <code>fGalleyTopRightOddX</code> to program which run by command <code>line os.system</code></li>
</ol>

<p><strong>[Edit 3]</strong>
Just one more information,</p>

<p>when I log diction in text file then i get <code>tLinetextBbox</code> vale as <code>361.59999999999997</code></p>

<p>In order to get the same result in Python 2.6, you have to explicitly do:</p>

<pre><code>'%.12g' % float_variable
</code></pre>

<p>Better to create a custom function to do this as:</p>

<pre><code>def convert_to_my_float(float_value):
    return float('%.12g' % float_value)
</code></pre>

<hr>

<p>As per <a href="https://docs.python.org/2/library/decimal.html#decimal-objects" rel="nofollow">Python's Decimal Objects</a> Document:</p>

<blockquote>
  <p>Changed in version 2.6: leading and trailing whitespace characters are
  permitted when creating a Decimal instance from a string.</p>
  
  <p>Changed in version 2.7: The argument to the constructor is now
  permitted to be a float instance.</p>
</blockquote>

<p>The answer to <em>Why they are behaving differently?</em> is, because <code>float.__repr__()</code> and <code>float.__str__()</code> methods in Python 2.7 changed.</p>

python python-2.7