Skip to content
Related Articles

Related Articles

float() in Python

View Discussion
Improve Article
Save Article
  • Difficulty Level : Basic
  • Last Updated : 26 Jul, 2022
View Discussion
Improve Article
Save Article

Python float() function is used to return a floating-point number from a number or a string representation of a numeric value.

Python float() Function 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

Python float() Function example

Python3




# convert integer value to float
num = float(10)
print(num)

Output:

10.0

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.

Python float() example

Example 1: How Python float() works

Python3




# Python program to illustrate
# Various examples and working of float()
# for integers
print(float(21.89))
 
# for floating point numbers
print(float(8))
 
# for integer type strings
print(float("23"))
 
# for floating type strings
print(float("-16.54"))
 
# for string floats with whitespaces
print(float("     -24.45   \n"))
 
# for inf/infinity
print(float("InF"))
print(float("InFiNiTy"))
 
# for NaN
print(float("nan"))
print(float("NaN"))
 
# Error is generated at last
print(float("Geeks"))

Output: 

21.89
8.0
23.0
-16.54
-24.45
inf
inf
nan
nan

All lines are executed properly but the last one will return an error: 

Traceback (most recent call last):
  File "/home/21499f1e9ca207f0052f13d64cb6be31.py", line 25, in 
    print(float("Geeks"))
ValueError: could not convert string to float: 'Geeks'

Example 2: float() for infinity and Nan

Python3




# Python program to illustrate
# Various examples and working of float()
 
# for inf/infinity
print(float("InF"))
print(float("InFiNiTy"))
 
# for NaN
print(float("nan"))
print(float("NaN"))

Output:

inf
inf
nan
nan

Example 3: Converting an Integer to a Float in Python

Python3




# python code to convert int
# float
number = 90
result = float(number)
 
print(result)

Output:

90.0

Example 4: Converting a String to a Float in Python

Python3




# python code to convert string
# to float
string = "90"
result = float(string)
 
print(result)

Output:

90.0

Example 5: Python float() exception

float() will raise ValueError if passed parameter is not a numeric value.

Python3




number = "geeks"
try:
    print(float(number))
except ValueError as e:
    print(e)

Output:

could not convert string to float: 'geeks'

Example 6: Python float() OverflowError

float() will raise OverflowError if passed parameter is too large (ex.: 10**309)

Python3




print(float(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

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!