The built-in function float() in Python

Overview:

  • The built-in function float() creates a floating point number from integer, floating point and string values.
  • The function exposed as the built-in function is the constructor of the class float.

Example:

# Example Python program that uses built-in
# function float() to create floating point numbers
# from integer, string and floating point values
# As of Python version 3.11.1
import sys

# Create a floating point number from an integer
fval = float(10)
print(fval)

# Create a floating point number from a string
fval = float("3.14")
print(fval)

# Add (upto) 1e291 beyond the maximum value supported by the system
fval = float(sys.float_info.max + float(1e291))

# No effect
print(fval)

# Add 1e292 beyond the maximum value supported by the system
fval = float(sys.float_info.max + float(1e292))

# Results an inf
print(fval)

# Create floating point values from Unicode Characters
# of Decimal Number Category  

# From Thai version of 1.2
val = float("๑.๒")
print(val)

# From Arabic-Indic version of 1.3
val = float("١.٣")
print(val)

# From Devanagari version of 1.4
val = float("१.४")
print(val)

# From Mongolian version of 1.5
val = float("᠑.᠕")
print(val)

 

Output:

10.0
3.14
1.7976931348623157e+308
inf
1.2
1.3
1.4
1.5

 


Copyright 2024 © pythontic.com