263

I would like to know how to convert a string containing digits to a double.

ZygD
  • 22,092
  • 39
  • 79
  • 102
user46646
  • 153,461
  • 44
  • 78
  • 84
  • 1
    That is not a python double. A python double has unlimited capacity. –  Feb 26 '10 at 20:55

3 Answers3

391
>>> x = "2342.34"
>>> float(x)
2342.3400000000001

There you go. Use float (which behaves like and has the same precision as a C,C++, or Java double).

Byte11
  • 204
  • 4
  • 12
Mongoose
  • 4,555
  • 1
  • 16
  • 7
57

The decimal operator might be more in line with what you are looking for:

>>> from decimal import Decimal
>>> x = "234243.434"
>>> print Decimal(x)
234243.434
foomip
  • 1,395
  • 11
  • 4
5

Be aware that if your string number contains more than 15 significant digits float(s) will round it.In those cases it is better to use Decimal

Here is an explanation and some code samples: https://docs.python.org/3/library/sys.html#sys.float_info

user1767754
  • 23,311
  • 18
  • 141
  • 164