×


Python float() Function in Linux - Step by step guide ?

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.

Here at Ibmi Media, as part of our Server Management Services, we regularly help our Customers to perform related Python function queries.

In this context, we shall look into how to use the float() function in Python.


More about float() function in Python

The float() function converts a specific number to a floating-point number. Python floats are useful for any function that requires precision, like scientific notation.

It's syntax is given below:

float(value)

float() function Parameter Values:

value: can be a number or a string.


Examples of using float() function

1. Have a look at the below function:

x = float(6)
print(x)

It's Output gives:

6.0


2. Converting an integer to a float:

integer = 60
x = float(integer)
print(x)

It's Output will give:

60.0


3. Converting a string to a float:

string = 0.500
x = float(string)
print(x)

The Output gives:

0.5


4. for infinity and Nan:

x = "Inf"
y = "nan"
print(float(x))
print(float(y))

The Output gives:

inf
nan


[Need help in fixing Python function issues ? We can help you. ]


Conclusion

This article covers how to use the float() function in Python. In fact, the float() method returns a floating point number from a number or a string. There are two number types in Python: floating-point numbers (floats) and integers. While floats can contain decimals, integers cannot.


float() Return Value

The float() method returns:

  • Equivalent floating point number if an argument is passed.
  • 0.0 if no arguments passed.
  • OverflowError exception if the argument is outside the range of Python float.