The float() function in Python converts a number or a string and returns a floating-point number. The function works with a number that can be an integer or a decimal. A string can contain any number, can contain mathematical operators, and can be used with NaN, Infinity, or inf.
Now we’re gonna guide you about using the float() function in Python. Hope you understand.
x = float(6) print(x)
The float() function converts a specific number to a floating-point number.
value: can be a number or a string
Example 1: Converting an integer to a float
integer = 60 x = float(integer) print(x)
Example 2: Converting a string to a float
string = 0.500 x = float(string) print(x)
Example 3: for infinity and Nan
x = "Inf" y = "nan" print(float(x)) print(float(y))
And we guided you on how to use the float() function in Python.
Thank you for checking it out!
Karim Buzdar holds a degree in telecommunication engineering and holds several sysadmin certifications including CCNA RS, SCP, and ACE. As an IT engineer and technical author, he writes for various websites.