首页 > 解决方案 > 你可以在python中将一个浮点数的字符串转换为一个整数吗?(在 1 个步骤中)

问题描述

例子:

string = "12.4"
x = int(string)

只是好奇是否有办法直接做到这一点,或者如果你只需要转换为 float 然后转换为 int 作为 2 个步骤。

标签: pythontype-conversioninteger

解决方案


x="12.4"
x=int(float(x))# this float(x) first converts the string x into float and 
#then int() function converts now newly float x int int x

推荐阅读