float()方法从给定的数字或字符串中返回相应的浮点数。

 **float([x])**#Where **x** can be a number or string that needs to convert 
它接受需要返回浮点数的单个参数、数字或字符串
| 参数 | 描述 | 必需/可选 | 
|---|---|---|
| 浮动 | 用作浮点数 | 可选择的 | 
| 整数 | 用作整数 | 可选择的 | 
| 线 | 它包含十进制数。前导空格和尾随空格被删除。可选使用“+”、“-”符号。可以包含 NaN、Infinity、inf(小写或大写)。 | 可选择的 | 
| 投入 | 返回值 | | 如果一个论点 | 等效浮点数 | | 如果没有争论 | 0.0 | | 该参数超出了 Python 浮点的范围 | OverflowError 异常 |
float()方法的示例float()的工作原理? # for integers
print(float(20))
# for floats
print(float(12.33))
# for string floats
print(float("-15.34"))
# for string floats with whitespaces
print(float("     -32.25\n"))
# string float error
print(float("abcd")) 
输出:
20.0
13.33
-15.34
-32.25
ValueError: could not convert string to float: 'abcd' 
float()表示无穷大,Nan(不是数字)? # 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             
                新闻名称:创新互联Python教程:Pythonfloat()
                
                浏览地址:http://www.csdahua.cn/qtweb/news14/432464.html
            
网站建设、网络推广公司-快上网,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 快上网