How do I convert a string to a double in Python?

匿名 (未验证) 提交于 2019-12-03 02:14:01

问题:

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

回答1:

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

There you go. Use float (which is almost always a C double).



回答2:

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 


回答3:

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



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!