Python built-in functions (22) -- float, python built-in 22 float
English document:
Classfloat
([X])
Return a floating point number constructed from a number or stringX.
If the argument is a string, it shoshould contain a decimal number, optionally preceded by a sign, and optionally embedded in whitespace. The optional sign may be'+'
Or'-'
;'+'
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
| nan
numeric_string ::= [sign
] numeric_value
Herefloatnumber
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,OverflowError
Will be raised.
For a general Python objectx
,float(x)
Delegatesx.__float__()
.
If no argument is given,0.0
Is returned.
Note:
1. The function converts a value or character to a floating point value.
>>> float(3)3.0>>> float('3')3.0
2. If no parameter is provided, 0.0 is returned.
>>> float()0.0
3. the string must be correctly converted to a floating point value; otherwise, an error is returned.
>>> 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. The symbols "+" and "-" are allowed in the string. spaces are not allowed between the two symbols and numbers, but spaces are allowed before and after the symbols.
>>> Float ('+ 000000') #3.14 with the positive code >>> float ('-000000') # minus sign-3.14 >>>> float ('-100 ') # space before and after plus and minus signs-3.14> float ('-000000') # space Traceback (most recent call last) cannot exist between plus and minus signs and digits ): file "<pyshell #8>", line 1, in <module> float ('-000000') ValueError: cocould not convert string to float:'-000000'
5. there are several special strings that can be correctly converted. "Infinity" or "inf" (case-insensitive) can be correctly converted, indicating Infinity, it can be used with "+" and "-", and "nan" can also be correctly converted, indicating no value.
>>> Float ('infinity ') inf >>> float ('inf') inf >>> float ('infinity ') # case insensitive inf >>> float ('+ inFinIty') # positive inFinIty inf >>> float ('-inFinIty ') # negative infinity-inf> float ('nan ') # No nan Value
6. If the defined object is correctly converted to a floating point by the float function, the _ float _ function must be defined.
>>> Class X: def _ init _ (self, score): self. score = score >>>> x = X (9.7) >>> float (x) # Traceback (most recent call last) cannot be converted: File "<pyshell #20> ", line 1, in <module> float (x) TypeError: float () argument must be a string or a number, not 'X' >>> class X: # redefine the class, add _ float _ method def _ init _ (self, score): self. score = score def _ float _ (self): return self. score >>> x = X (9.7) >>> float (x) #9.7 convertible