如何将python中的十进制字符串转换为数字?

可能重复:
Python - 将字符串解析为Float或Int

如何将'1.03'(字符串)转换为Python中的数字,最好是小数?


只需使用float()

>>> float('1.03')
1.03

如果您需要实际的Decimal类型,请使用decimal模块:

>>> from decimal import *
>>> Decimal('1.03')
Decimal('1.03')

一般来说,使用浮点数对于你来说会更容易,但是你花费的精度与浮点数不符。


floatintdecimal可以自动转换有效的字符串。

例如:

float('1.03')
链接地址: http://www.djcxy.com/p/48419.html

上一篇: How to convert decimal string in python to a number?

下一篇: How can I check if a string has a numeric value in it in Python?