A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/python/float-in-python/ below:

float() in Python - GeeksforGeeks

float() in Python

Last Updated : 10 May, 2023

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
# convert integer value to float
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:

Return: Float Value

Values that the Python float() method can return depending upon the argument passed

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
# 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"))

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
# python code to convert int 
# float
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
# 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
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
# python code to convert string
# to float
string = "90"
result1 = float(string)

# for floating type strings
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)

Python3

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


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4