In Python, the float() function is used to convert numbers or numeric strings into floating-point numbers. A floating-point number is simply a number with a decimal point (for example, 3.14, -0.5, or 10.0). This function is especially useful when dealing with user input or when precise decimal calculations are needed.
Example:
Input: 10
Output: 10.0
Syntax
float() function in Python has following syntax:
float(x)
Parameter: x (optional) can be a number (integer or float), a string representing a number, like "10.5" or "inf", "infinity" or "nan".
Return Value:
- Returns a floating-point number corresponding to x.
- If no argument is passed, it returns 0.0.
- If a string does not represent a valid number, it raises a ValueError.
- If a number is too large, it raises an OverflowError.
Converting Integers to Float
One can easily turn an integer into a float by wrapping it with float().
Example: In this example, we passed an integer type value to the float() function.
Python
number = 90
result = float(number)
print(result)
Converting Strings to Float
If a string contains numeric values, float() can convert it into a floating-point number.
Example: This code converts numeric strings (both integer-like and decimal-like) into floats.
Python
string = "90" # String representing an integer
result1 = float(string)
float_string = "-16.54" # String representing a float
result2 = float(float_string)
print(result1)
print(result2)
Infinity and Nan
Python allows infinite and undefined numbers using float().
Example: In this example, we passed infinite and NaN values to float() function and then print their equivalent float values.
Python
print(float("inf")) # Infinity
print(float("infinity"))
print(float("nan")) # Not-a-Number
print(float("NaN"))
Explanation:
- "inf" or "infinity": represents positive infinity.
- "-inf": represents negative infinity.
- "nan" (Not a Number): represents undefined values, like results of invalid math operations.
- These strings are case-insensitive
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.
1. ValueError
Python float() will raise ValueError if the passed parameter is not a numeric value.
Example: In this example, we passed an alphabet string as a parameter to float() function.
Python
number = "geeks"
try:
print(float(number))
except ValueError as e:
print(e)
Outputcould not convert string to float: 'geeks'
2. OverflowError
float() in Python will raise OverflowError if the passed parameter is too large.
Example: This code raises an OverflowError because 10**309 exceeds Python’s floating-point range.
Python
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
Explore
Python Fundamentals
Python Data Structures
Advanced Python
Data Science with Python
Web Development with Python
Python Practice