The float()
function in Python converts a number or a string that represents a floating-point number to a floating-point number. If no argument is passed, it returns 0.0. If the argument is a string that cannot be converted to a float, it raises a ValueError.
Parameter Values
Parameter | Description |
---|---|
x | The value that needs to be converted to a floating-point number. This can be an integer, a string, or another floating-point number. |
Return Values
The float()
function returns a floating-point number or raises ValueError
.
How to Use float()
in Python
Example 1:
The float()
function converts a number or a string that contains a number to a floating-point number.
float(10)
Example 2:
It can also convert a string with a floating-point number to a float.
float('3.14')
Example 3:
If the input cannot be converted to a float, it will raise a ValueError
.
float('abc')