The Python float()
is a built-in function that returns a floating point number constructed from a number or a string.
Python float() Syntax
float(arg)
Where,
- arg (optional) – is a number or string that is to be converted into floating point type.
If the argument is a string, it should contain a decimal number, optionally preceded by a sign( + or – ), and optionally embedded in whitespace.
float() function return values
The Python float()
function returns:
0.0
if no argument is passedOverflowError
if outside the range of floating point number- Corresponding floating point number for a valid argument
How to check if an object is floating point number?
Well, Python has built-in function type()
which returns the type of object or simply the class to which the object belongs to.
>>> type(1)
<class 'int'>
>>> x = float(1)
>>> type(x)
<class 'float'>
Python float() Function Example
>>> #integer
>>> float(1)
1.0
>>> #strings
>>> float('1')
1.0
>>> float('infinity')
inf
>>> float(1e-0.02)
0.01
>>> float(+2E3)
2000.0