Float value behaviour in Python 2.6 and Python 2.7
Question: 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.

I want same behaviour in Python 2.6

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

**Python 2.6**

    
    
    >>> a
    '60.000,494.100,361.600,553.494'
    >>> eval(a)
    (60.0, 494.10000000000002, 361.60000000000002, 553.49400000000003)
    >>> import ast
    >>> ast.literal_eval(a)
    (60.0, 494.10000000000002, 361.60000000000002, 553.49400000000003)
    >>> 
    
    >>> for i in a.split(","):
    ...   float(i)
    ... 
    60.0
    494.10000000000002
    361.60000000000002
    553.49400000000003
    >>> 
    

**Python 2.7**

    
    
    >>> a
    '60.000,494.100,361.600,553.494'
    >>> eval(a)
    (60.0, 494.1, 361.6, 553.494)
    >>> import ast
    >>> ast.literal_eval(a)
    (60.0, 494.1, 361.6, 553.494)
    >>> 
    
    >>> for i in a.split(","):
    ...   float(i)
    ... 
    60.0
    494.1
    361.6
    553.494
    

Its not look good

**[Edit 2]**

**I just print value and condition**

    
    
    print fGalleyTopRightOddX, ">=", tLinetextBbox[2], fGalleyTopRightOddX>=tLinetextBbox[2]
    
    361.6 >= 361.6 False
    

I calculate `tLinetextBbox` value from string and which is
`361.60000000000002` and `fGalleyTopRightOddX` value is `361.6`

I am working on **Python Django** project where **apache** is server.

  1. `fGalleyTopRightOddX` i.e. `361.6` is calculated in apache environment
  2. `tLinetextBbox` i.e. `361.60000000000002` is calculated on cmd means I pass `fGalleyTopRightOddX` to program which run by command `line os.system`

**[Edit 3]** Just one more information,

when I log diction in text file then i get `tLinetextBbox` vale as
`361.59999999999997`


Answer: In order to get the same result in Python 2.6, you have to explicitly do:

    
    
    '%.12g' % float_variable
    

Better to create a custom function to do this as:

    
    
    def convert_to_my_float(float_value):
        return float('%.12g' % float_value)
    

* * *

As per [Python's Decimal
Objects](https://docs.python.org/2/library/decimal.html#decimal-objects)
Document:

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

The answer to _Why they are behaving differently?_ is, because
`float.__repr__()` and `float.__str__()` methods in Python 2.7 changed.

