Table of Contents
Where X is an input parameter passed to the function when it is called in the program.
float () Parameters
One of the salient features of the python programming language is that it has rich predefined libraries with numerous methods defined in it. We can use them by just calling the function. While a function is being called, the necessary syntax must be followed and suitable parameters must be passed to the function. Here the term parameters refer to the input arguments given to the method by the user. In the next lines, let us understand more about the parameter of the float() function.
Calling float function: float(X)
Here, the variable X is called the parameter to that function. X can be a normal integer value or any string that has decimal points. An interesting fact about the float() is that the parameter is optional.
float () Return Value
Python programming language has many inbuilt libraries and functions. One among them is the float() function. With the help of the float() function, we can convert an input string or an integer value to a floating point value. We already knew that it is not mandatory to send a parameter to the float(), in such a case the function returns 0.0 as output. On another side, the float() function returns the corresponding floating-point value when an integer or string with decimals is given as an input argument. If any input value is out of range of floating value in python, then it returns OverflowError.
Float () Function Example 1
The following is what float() returns for 3 i.e. Integer.
Example 2: This is what float() returns for 5.5 i.e. decimal.
Examples of float() With String Parameter:
Example 1: This is what float() returns for “5.5” i.e. String.
Example 2: This is what float() returns for “5.5” i.e. String with white spaces.
Example 3: This is what float() returns for “5.500” i.e. String, which is decimal.
Example 4: This is what float() returns for “-15.5 \n” i.e. a string, which is a negative decimal.
Example 5: This is what float() returns for “Cucumber” i.e. String, which is not an integer or decimal.
Output–float() returns an error, as the string is not an integer or decimal.
Examples of float() With Infinity:
Example 1: This is what float() returns for 1.82e310 i.e. an integer that exceeds the maximum value of the Python floating-point number.
Examples of float() With Not a Number, i.e. NaN as a String.
Example 1: This is what float() returns for “NaN” i.e. String, which is not a number.
Examples of float() With Infinity, i.e. Infinity as a String.
Example 1: This is what float() returns for “Infinity” i.e. String.
Example 2: This is what float() returns for “inf” i.e. String.
Example: How does float () work with Python?
With this example, let us get a clearer understanding of the float() in the python programming language.
Snippet 1: print(float())
Output 1: 0.0
Snippet 2: print(float(17))
Output 2: 17.0
Snippet 3: print(float(10.02))
Output 3: 10.02
Snippet 4: print(float(‘podium’))
Output 4: ValueError: could not convert string to float: ‘podium’
Snippet 5: print(float(-8))
Output 5: -8.0
Snippet 6: print(float(‘1234’))
Output 6: 1234.0
Snippet 7: print(float(123456789123499999991111111111111111111122255555555555555555555555555555555555555555555550000000000000000000011111111111111111111111111111111111111111111111188888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888555555555555555555555555555555555555555555555555555555555555555555555555555555522222222222222222222222222222222222222222))
Output 7: OverflowError: int too large to convert to float
Example: float() for infinity and NaN (Not a Number)?
With this example, let us get a clearer understanding of the float() function in the programming language when infinity and NaN (Not a Number) values are given as input parameters to the float() function.
Snippet 8: print(float(“nan”))
Output 8: nan
Snippet 9: print(float(“NaN”))
Output 9: nan
Snippet 10: print(float(“inf”))
Output 10: inf
Snippet 11: print(float(“InF”))
Output 11: inf
Snippet 12: print(float(“InFiNiTy”))
Output 12: inf
Snippet 13: print(float(“infinity”))
Output 13: inf
Converting an Integer to Float in Python
As we discussed earlier, we can pass an integer value to the float() function as an input argument and get its equivalent floating point value as output. Let us understand this with the help of an example.
Snippet 14: print(float(23))
Output 14: 23.0
Snippet 15: print(float(112000))
Output 15: print(float(112000))
Converting a String to Float in Python
Output 16: ValueError: could not convert string to float: ‘python’
Snippet 17: print(float(’64’))
Output 17: 64.0
Additional Methods That Are Available In Float():
Float() also has some additional methods that are useful to identify finite integers, decimal from hexadecimal strings, etc. To understand this better, it makes sense to see it in-detail with examples.
What is float.as_integer_ratio()?
Float.as_integer_ratio() is a built-in function that provides a pair of integers whose ratio is equivalent to the original float provided.
return Tupple (integer pair)
What is float.is_integer()?
float.is_integer() is used to identify whether the given float instance is a finite integer or not. If it is finite, the integer returns true, else it returns false.
What is float.hex()?
float.hex() returns the hexadecimal string for the given floating number.
What is float.fromhex()?
float.fromhex() returns the floating-point number which represents the given hexadecimal string.
Float() in python is an important method that is useful to represent floating-point numbers. It is used to represent real numbers and is written with the decimals dividing the integer and fractional parts.
In this article, you have learned what float() is, the syntax for float(), and additional float() methods along with good working examples. You also saw how to define float(), how to print, as well as get the output from a float, etc.