Python float() Function
float() is a built-in Python function that converts other types of data to a floating-point number (decimal).
Floats are very important in scientific computing, data analysis, graphics processing, and similar scenarios;float() the function helps us convert integers, strings, and so on to the float type.
Word meaning: float means "floating point", a way of representing decimals in computing.
Basic syntax and parameters
float() is a built-in function and can be called directly.
Syntax format
float(x)
Parameter Description
- Parameter x:
- Type: integer, string, or another object convertible to float
- Description: the value to convert to a float. Commonly a numeric string or an integer.
Function description
- Return Value: returns a float object.
- Special cases:
float()Returns 0.0 with no arguments- When converting an integer to a float, the decimal part is .0
- The string "inf" or "infinity" means infinity
- The string "nan" means Not a Number
Example
Let's master it through a series of examples, float() through a series of examples from simple to complex.
Example 1: basic usage - converting numbers
Example
Convert from an integer
(
float
(
10
)
)
Output: 10.0
(
float
(
5
)
)
Output: -5.0
Convert from a string
(
float
(
"3.14"
)
)
Output: 3.14
(
float
(
" 2.5 "
)
)
Output: 2.5 (whitespace removed automatically)
Convert from a boolean
(
float
(
True
)
)
Output: 1.0
(
float
(
False
)
)
Output: 0.0
Without arguments
(
float
(
)
)
Output: 0.0
Expected output:
10.0
-5.0
3.14
2.5
1.0
0.0
0.0
Code analysis:
- When converting an integer to a float, the .0 decimal part is added automatically.
- String conversion automatically strips leading and trailing whitespace.
- Boolean
Trueconverts to 1.0,Falseconverts to 0.0.
Example 2: special values - infinity and NaN
Python floats support representations of infinity and NaN.
Example
Infinity
(
float
(
"inf"
)
)
Output: inf
(
float
(
"infinity"
)
)
Output: infinity
(
float
(
"-inf"
)
)
Output: -inf
NaN (Not a Number)
(
float
(
"nan"
)
)
Output: nan
Verify with the math module
import
math
(
math
.
isinf
(
float
(
"inf"
)
)
)
Output: True
(
math
.
isnan
(
float
(
"nan"
)
)
)
Output: True
Expected output:
inf
infinity
-inf
nan
True
True
Code analysis:
- "inf" or "infinity" means positive infinity, and "-inf" means negative infinity.
- "nan" means Not a Number, often used to represent an undefined or unrepresentable result.
- You can use the
math.isinf()andmath.isnan()can be used to detect these special values.
Example 3: applications in mathematical calculations
Example
Integer division automatically produces a float
result
=
7
/
2
(
result
)
Output: 3.5
(
type
(
result
)
)
Output:
Force conversion to float for precise calculation
a
=
10
b
=
float
(
a
)
/
3
(
b
)
Output: 3.3333333333333335
Handle decimal input from a user
user_input
=
"3.14159"
pi
=
float
(
user_input
)
(
f
"Pi: {pi}"
)
Output: Pi: 3.14159
Floating-point precision issues
(
0.1
0.2
)
Output: 0.30000000000000004
Expected output:
3.5
3.3333333333333335
圆周率: 3.14159
0.30000000000000004
This example shows the use of floats in mathematical calculations and the existence of floating-point precision issues.
Python3 built-in function