📅  最后修改于: 2020-09-20 04:03:34             🧑  作者: Mango
float()
的语法为:
float([x])
float()
方法采用一个参数:
Parameter Type | Usage |
---|---|
Float number | Use as a floating number |
Integer | Use as an integer |
String | Must contain decimal numbers. Leading and trailing whitespaces are removed. Optional use of “+”, “-” signs. Could contain NaN , Infinity , inf (lowercase or uppercase). |
float()
方法返回:
OverflowError
异常# for integers
print(float(10))
# for floats
print(float(11.22))
# for string floats
print(float("-13.33"))
# for string floats with whitespaces
print(float(" -24.45\n"))
# string float error
print(float("abc"))
输出
10.0
11.22
-13.33
-24.45
ValueError: could not convert string to float: 'abc'
# for NaN
print(float("nan"))
print(float("NaN"))
# for inf/infinity
print(float("inf"))
print(float("InF"))
print(float("InFiNiTy"))
print(float("infinity"))
输出
nan
nan
inf
inf
inf
inf