新聞中心
這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
創(chuàng)新互聯(lián)Python教程:Pythonfloat()
float()方法從給定的數(shù)字或字符串中返回相應的浮點數(shù)。

**float([x])**#Where **x** can be a number or string that needs to convert
浮點()參數(shù):
它接受需要返回浮點數(shù)的單個參數(shù)、數(shù)字或字符串
| 參數(shù) | 描述 | 必需/可選 |
|---|---|---|
| 浮動 | 用作浮點數(shù) | 可選擇的 |
| 整數(shù) | 用作整數(shù) | 可選擇的 |
| 線 | 它包含十進制數(shù)。前導空格和尾隨空格被刪除??蛇x使用“+”、“-”符號??梢园?NaN、Infinity、inf(小寫或大寫)。 | 可選擇的 |
浮點()返回值
| 投入 | 返回值 | | 如果一個論點 | 等效浮點數(shù) | | 如果沒有爭論 | 0.0 | | 該參數(shù)超出了 Python 浮點的范圍 | OverflowError 異常 |
Python 中float()方法的示例
示例 Python 中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'
例 2: float()表示無窮大,Nan(不是數(shù)字)?
# 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 網(wǎng)頁名稱:創(chuàng)新互聯(lián)Python教程:Pythonfloat()
分享網(wǎng)址:http://m.fisionsoft.com.cn/article/cohccpg.html


咨詢
建站咨詢
