Python float() Function

Python3 内置函数Python3 Built-in Functions


float()is a built-in function in Python used to convert other types of data into floating-point numbers (decimals).

Floating-point numbers are very important in scenarios such as scientific computing, data analysis, and graphics processing.float()The function can help us convert integers, strings, etc. into floating-point types.

Word Definition: floatIt means "floating point", which is a way of representing decimal numbers in a computer.


Basic Syntax and Parameters

float()is a built-in function that can be called directly.

Syntax Format

float(x)

Parameter Description

  • Parameter x:
    • Type: Integer, string, or other object that can be converted to a floating-point number.
    • Description: The value to be converted to a floating-point number. Typically a numeric string or integer.

Function Description

  • Return Value: Returns a floating-point number object.
  • Special Cases:
    • float()Without arguments, it returns 0.0
    • When an integer is converted to a float, the decimal part is .0
    • The strings "inf" or "infinity" represent infinity.
    • The string "nan" represents Not a Number (NaN).

Examples

Let's master through a series of examplesfloat()the usage of float().

Example 1: Basic Usage - Converting Numbers

Example

# Convert from integer
print(float(10))      # Output: 10.0
print(float(-5))      # Output: -5.0

# Convert from string
print(float("3.14"))  # Output: 3.14
print(float("  2.5  "))  # Output: 2.5 (spaces automatically removed)

# Convert from boolean
print(float(True))    # Output: 1.0
print(float(False))   # Output: 0.0

# Without argument
print(float())        # Output: 0.0

Expected output:

10.0
-5.0
3.14
2.5
1.0
0.0
0.0

Code explanation:

  1. When an integer is converted to a float, a .0 decimal part is added automatically.
  2. String conversion supports automatic trimming of leading and trailing whitespace characters.
  3. Boolean valuesTrueTrue converts to 1.0,FalseFalse converts to 0.0.

Example 2: Special Values - Infinity and NaN

Python floats support the representation of infinity and NaN.

Example

# Infinity
print(float("inf"))      # Output: inf
print(float("infinity"))  # Output: infinity
print(float("-inf"))     # Output: -inf

# NaN (Not a Number)
print(float("nan"))      # Output: nan

# Verify using the math module
import math
print(math.isinf(float("inf")))   # Output: True
print(math.isnan(float("nan")))   # Output: True

Expected output:

inf
infinity
-inf
nan
True
True

Code explanation:

  • "inf" or "infinity" represents positive infinity, and "-inf" represents negative infinity.
  • "nan" represents Not a Number, often used for undefined or unrepresentable results.
  • You can usemath.isinf()andmath.isnan()to detect these special values.

Example 3: Application in Mathematical Calculations

Example

# Result of integer division automatically becomes a float
result = 7 / 2
print(result)        # Output: 3.5
print(type(result))  # Output: <class 'float'>

# Force conversion to float for precise calculation
a = 10
b = float(a) / 3
print(b)             # Output: 3.3333333333333335

# Handling decimal input from user
user_input = "3.14159"
pi = float(user_input)
print(f"Pi: {pi}")  # Output: Pi: 3.14159

# Floating-point precision issue
print(0.1 + 0.2)  # Output: 0.30000000000000004

Expected output:

3.5
<class 'float'>
3.3333333333333335
圆周率: 3.14159
0.30000000000000004

This example demonstrates the application of floating-point numbers in mathematical calculations, as well as the existence of floating-point precision issues.


Python3 内置函数Python3 Built-in Functions


Other Extensions