如何将字符串解析为float或int? [英] How do I parse a string to a float or int?
本文介绍了如何将字符串解析为float或int?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在Python中,如何将类似"545.2222"
的数字字符串解析为其对应的浮点值545.2222
?还是将字符串"31"
解析为整数31
?
In Python, how can I parse a numeric string like "545.2222"
to its corresponding float value, 545.2222
? Or parse the string "31"
to an integer, 31
?
我只想知道如何将 float str
解析为float
,并将( int str
)解析为int
I just want to know how to parse a float str
to a float
, and (separately) an int str
to an int
.
推荐答案
>>> a = "545.2222"
>>> float(a)
545.22220000000004
>>> int(float(a))
545
这篇关于如何将字符串解析为float或int?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文