Python float() Method with Examples

In the previous article, we have discussed Python complex() Method with Examples
float() Method in Python:

The float() function returns a floating-point number from the specified value.

Syntax:

float(value)

Parameter Values:

value: A number or string that can be converted to a floating-point number.

Return Value:

The float() method yields:

  • If an argument is passed, the equivalent floating-point number is returned.
  • If no arguments are passed, the value is 0.0;
  • Otherwise, an OverflowError exception is thrown if the argument is outside the range of Python float.

Examples:

Example1:

Input:

Given Number = 14

Output:

The given number's{ 14 } Floating-point number =  14.0

Example2:

Input:

Given Number = "-25.431" (# given in string format)

Output:

The given number's{ -25.431 } Floating-point number =  -25.431

float() Method with Examples in Python

Method #1: Using Built-in Functions (Static Input)

Approach:

  • Give the number as static input and store it in a variable.
  • Pass the given number as an argument to the float() function to get the floating-point number of a given number.
  • Store it in another variable.
  • Print the floating-point number of a given number.
  • The Exit of the program.

Below is the implementation:

# Give the number as static input and store it in a variable.
gvn_numbr = 14
# Pass the given number as an argument to the float() function to get the
# the floating-point number of a given number.
# Store it in another variable.
rslt = float(gvn_numbr)
# Print the floating-point number of a given number.
print("The given number's{", gvn_numbr,
      "} Floating-point number = ", rslt)

Output:

The given number's{ 14 } Floating-point number =  14.0

Similarly, check it out for other values and print the result

gvn_numbr = "-25.456"
rslt = float(gvn_numbr)
print("The given number's{", gvn_numbr,
      "} Floating-point number = ", rslt)

Output:

The given number's{ -25.456 } Floating-point number =  -25.456
float() for infinity and Nan(Not a number) values
print(float("InFiNiTy"))
print(float("infinity"))
print(float("nan"))
print(float("NaN"))
print(float("inf"))
print(float("InF"))

Output:

inf
inf
nan
nan
inf
inf

Method #2: Using Built-in Functions (User Input)

Approach:

  • Give the number as a string as user input using the input() function and store it in a variable.
  • Pass the given number as an argument to the float() function to get the floating-point number of a given number.
  • Store it in another variable.
  • Print the floating-point number of a given number.
  • The Exit of the program.

Below is the implementation:

# Give the number as a string as user input using the input() function
# and store it in a variable.
gvn_numbr = input("Enter some random number = ")
# Pass the given number as an argument to the float() function to get the
# the floating-point number of a given number.
# Store it in another variable.
rslt = float(gvn_numbr)
# Print the floating-point number of a given number.
print("The given number's{", gvn_numbr,
      "} Floating-point number = ", rslt)

Output:

Enter some random number = -19
The given number's{ -19 } Floating-point number = -19.0

Fed up with searching various pages for the list of Python Built in Functions? Look at the tutorial linked here and explore all coding samples of built-in functions of python.