Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Python float() Function

Python float() Function

Python3 Built-in Functions Python3 built-in 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

- 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

- `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

print

(

float

(

10

)

)

     

Output: 10.0

print

(

float

(

5

)

)

     

Output: -5.0

Convert from a string

print

(

float

(

"3.14"

)

)

 

Output: 3.14

print

(

float

(

"  2.5  "

)

)

 

Output: 2.5 (whitespace removed automatically)

Convert from a boolean

print

(

float

(

True

)

)

   

Output: 1.0

print

(

float

(

False

)

)

 

Output: 0.0

Without arguments

print

(

float

(

)

)

       

Output: 0.0

Expected output:

10.0
-5.0
3.14
2.5
1.0
0.0
0.0

Code analysis:

  1. When converting an integer to a float, the .0 decimal part is added automatically.
  2. String conversion automatically strips leading and trailing whitespace.
  3. Boolean True converts to 1.0,False converts to 0.0.

Example 2: special values - infinity and NaN

Python floats support representations 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 with 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 analysis:

Example 3: applications in mathematical calculations

Example

Integer division automatically produces a float

result

=

7

/

2

print

(

result

)

       

Output: 3.5

print

(

type

(

result

)

)

 

Output:

Force conversion to float for precise calculation

a

=

10

b

=

float

(

a

)

/

3

print

(

b

)

           

Output: 3.3333333333333335

Handle decimal input from a user

user_input

=

"3.14159"

pi

=

float

(

user_input

)

print

(

f

"Pi: {pi}"

)

 

Output: Pi: 3.14159

Floating-point precision issues

print

(

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 Functions Python3 built-in function