首页 › 参考手册 › 内置函数 › float()

Python float() 函数

描述

把数字或字符串转换为浮点数。也接受 "inf"、"nan" 等特殊值。

语法

float([x])

参数

参数说明
x数字或数字字符串

返回值

浮点数

实例

print(float("3.14"))
print(float(7))
print(float("1e-3"))
print(0.1 + 0.2, round(0.1 + 0.2, 2))   # 浮点数精度问题
以上实例输出结果为:
3.14
7.0
0.001
0.30000000000000004 0.3