如何在Python中将字符串解析为float或int? [英] How do I parse a string to a float or int in Python?

查看:132
本文介绍了如何在Python中将字符串解析为float或int?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Python中,如何将545.2222等数字字符串解析为相应的浮点值 542.2222 ?或者将字符串31解析为整数, 31

In Python, how can I parse a numeric string like "545.2222" to its corresponding float value, 542.2222? Or parse the string "31" to an integer, 31?

我只想知道如何将 float 字符串解析为 float 和(单独) int 字符串 int

I just want to know how to parse a float string to a float, and (separately) an int string to an int.

推荐答案

>>> a = "545.2222"
>>> float(a)
545.22220000000004
>>> int(float(a))
545

这篇关于如何在Python中将字符串解析为float或int?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆