convert string number to real number - ValueError: invalid literal for int() with base 10: '"2"'
Matthias Bläsing
matthias.blaesing at rwth-aachen.de
Wed May 28 14:07:16 EDT 2008
Am Wed, 28 May 2008 10:41:51 -0700 schrieb davidj411:
> I like the str2num function approach, but then i get left with a float
> that has more than 2 decimal spaces , i.e. 11.50 becomes
> 11.449999999999999 and round will not fix that.
Welcome to the wonderful world of floating point numbers. For your usage
you want 10-based numbers. Have a look at the decimal module:
>>> from decimal import Decimal
>>> a = Decimal("11.45")
>>> a
Decimal("11.45")
>>> str(a)
'11.45'
>>> a + 1
Decimal("12.45")
>>> a + Decimal("1.55")
Decimal("13.00")
HTH
Matthias
More information about the Python-list
mailing list