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

**float([x])**#Where **x** can be a number or string that needs to convert
浮點(diǎn)()參數(shù):
它接受需要返回浮點(diǎn)數(shù)的單個(gè)參數(shù)、數(shù)字或字符串
| 參數(shù) | 描述 | 必需/可選 |
|---|---|---|
| 浮動(dòng) | 用作浮點(diǎn)數(shù) | 可選擇的 |
| 整數(shù) | 用作整數(shù) | 可選擇的 |
| 線 | 它包含十進(jìn)制數(shù)。前導(dǎo)空格和尾隨空格被刪除??蛇x使用“+”、“-”符號(hào)??梢园?NaN、Infinity、inf(小寫或大寫)。 | 可選擇的 |
浮點(diǎn)()返回值
| 投入 | 返回值 | | 如果一個(gè)論點(diǎn) | 等效浮點(diǎn)數(shù) | | 如果沒有爭論 | 0.0 | | 該參數(shù)超出了 Python 浮點(diǎn)的范圍 | 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 標(biāo)題名稱:創(chuàng)新互聯(lián)Python教程:Pythonfloat()
文章地址:http://m.5511xx.com/article/cohccpg.html


咨詢
建站咨詢
