Float behavior in Python 2.6 and Python 2.7

I need to convert a string to a float tuple. In Python 2.7, it gives the correct conversion, but in Python this is not the same case.

I want the same behavior in Python 2.6

Can someone help me why this is not the same in Python 2.6 and how to do it 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

Doesn't look very good

[Change 2]

I just type the value and condition

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

361.6 >= 361.6 False

I compute the value tLinetextBboxfrom the string and which 361.60000000000002and the fGalleyTopRightOddXvalue361.6

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

  • fGalleyTopRightOddXie 361.6computed in apache environment
  • tLinetextBboxie 361.60000000000002computed on cmd means that I pass to the fGalleyTopRightOddXprogram that is executed by the commandline os.system

[ 3] ,

diction , tLinetextBbox vale 361.59999999999997

+4
1

Python 2.6, :

'%.12g' % float_variable

:

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

Python :

2.6: Decimal .

2.7: .

, -?, float.__repr__() float.__str__() Python 2.7 .

+4

Source: https://habr.com/ru/post/1656950/


All Articles