将字符串解析为Float或Int [英] Parse String to Float or Int

查看:136
本文介绍了将字符串解析为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

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

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