Python內建函數(22)——float,python內建22float

來源:互聯網
上載者:User

Python內建函數(22)——float,python內建22float

英文文檔:

class float([x])

Return a floating point number constructed from a number or string x.

If the argument is a string, it should contain a decimal number, optionally preceded by a sign, and optionally embedded in whitespace. The optional sign may be '+' or '-'; a '+' sign has no effect on the value produced. The argument may also be a string representing a NaN (not-a-number), or a positive or negative infinity. More precisely, the input must conform to the following grammar after leading and trailing whitespace characters are removed:

sign           ::=  "+" | "-"infinity       ::=  "Infinity" | "inf"nan            ::=  "nan"numeric_value  ::=  floatnumber | infinity | nannumeric_string ::=  [sign] numeric_value

Here floatnumber is the form of a Python floating-point literal, described in Floating point literals. Case is not significant, so, for example, “inf”, “Inf”, “INFINITY” and “iNfINity” are all acceptable spellings for positive infinity.

Otherwise, if the argument is an integer or a floating point number, a floating point number with the same value (within Python’s floating point precision) is returned. If the argument is outside the range of a Python float, an OverflowError will be raised.

For a general Python object x, float(x) delegates to x.__float__().

If no argument is given, 0.0 is returned.

 

說明:

  1. 函數功能將一個數值或者字元轉換成浮點型數值。

>>> float(3)3.0>>> float('3')3.0

  2. 不提供參數的時候,返回0.0。

>>> float()0.0

  3. 字串必須能正確轉換成浮點型數值的,否則報錯。

>>> float('3.14.15926')Traceback (most recent call last):  File "<pyshell#3>", line 1, in <module>    float('3.14.15926')ValueError: could not convert string to float: '3.14.15926'

  4. 字串中允許出現“+”、“-”兩個符號,兩個符號和數字之間不能出現空格,但是符號前面和數字後面允許出現空格。

>>> float('+3.14') #帶正號3.14>>> float('-3.14') #帶負號-3.14>>> float('  -3.14  ') #加號或減號前、數字後可以有空格-3.14>>> float('- 3.14') #加號或減號與數字間不可以有空格Traceback (most recent call last):  File "<pyshell#8>", line 1, in <module>    float('- 3.14')ValueError: could not convert string to float: '- 3.14'

  5. 有幾個特殊的字串能正確轉換,"Infinity"或者“inf”(不區分大小寫),能正確轉換,表示無窮大,可以和“+”、“-”一起使用;“nan”也能正確轉換,表示沒有值。

>>> float('Infinity')inf>>> float('inf')inf>>> float('inFinIty') #不區分大小寫inf>>> float('+inFinIty') #正無窮inf>>> float('-inFinIty') #負無窮-inf>>> float('nan') #沒有值nan

  6. 定義的對象如果要被float函數正確轉換成浮點數,需要定義__float__函數。

>>> class X:    def __init__(self,score):        self.score = score>>> x = X(9.7)>>> float(x) #不能轉換Traceback (most recent call last):  File "<pyshell#20>", line 1, in <module>    float(x)TypeError: float() argument must be a string or a number, not 'X'>>> class X: #重新定義類,加入__float__方法    def __init__(self,score):        self.score = score    def __float__(self):        return self.score>>> x = X(9.7)>>> float(x) #可以轉換9.7

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.