Python float() function is used to return a floating-point number from a number or a string representation of a numeric value.
Example:
Here is a simple example of the Python float() function which takes an integer as the parameter and returns its float value.
Python3
num = float ( 10 )
print (num)
|
Output:
10.0
Python float() Function Syntax
The float() function in Python has the following syntax.
Syntax: float(x)
Parameter x: x is optional & can be:
- any number or number in form of string, ex,: “10.5”
- inf or infinity, NaN (any cases)
Return: Float Value
Values that the Python float() method can return depending upon the argument passed
- If an argument is passed, then the equivalent floating-point number is returned.
- If no argument is passed then the method returns 0.0.
- If any string is passed that is not a decimal point number or does not match any cases mentioned above then an error will be raised.
- If a number is passed outside the range of Python float then OverflowError is generated.
float() in Python Example
Now, let us see a few examples of float() in Python.
Python float() Working
Let us see how the Python float() function works, In this example, we passed different datatype values to the float() function as parameters to see how it works.
Python3
print ( float ( 21.89 ))
print ( float ( 8 ))
print ( float ( "23" ))
print ( float ( "-16.54" ))
print ( float ( " -24.45 \n" ))
print ( float ( "InF" ))
print ( float ( "InFiNiTy" ))
print ( float ( "nan" ))
print ( float ( "NaN" ))
|
Output:
21.89
8.0
23.0
-16.54
-24.45
inf
inf
nan
nan
Integer Datatype
In this example, we passed an integer type value to the float() function.
Python3
number = 90
result = float (number)
print (result)
|
Output:
90.0
Infinity and Nan
In this example, we passed infinite and NaN values to the float() function and then print their equivalent float values.
Python3
print ( float ( "InF" ))
print ( float ( "InFiNiTy" ))
print ( float ( "nan" ))
print ( float ( "NaN" ))
|
Output:
inf
inf
nan
nan
String Datatype
In this example, we try to print the equivalent float values of the Python String datatype. We will be passing a number as a String value.
Python3
string = "90"
result1 = float (string)
float_string = "-16.54"
result2 = float (float_string)
print (result1)
print (result2)
|
Output:
90.0
-16.54
Python float() Exceptions and Errors
Sometimes the float() function in Python may not be compatible with all the datatypes. In this case, it may raise an exception or generate an error.
Python float() exception
Python float() will raise ValueError if the passed parameter is not a numeric value. In this example, we passed an alphabet string as the parameter to the float() function.
Python3
number = "geeks"
try :
print ( float (number))
except ValueError as e:
print (e)
|
Output:
could not convert string to float: 'geeks'
Python float() OverflowError
float() in Python will raise OverflowError if the passed parameter is too large (ex.: 10**309)
Output:
Traceback (most recent call last):
File "/home/1eb6a2abffa536ccb1cae660db04a162.py", line 1, in <module>
print(float(10**309))
OverflowError: int too large to convert to float