Home / Python / Reference / Built-in functions

Python float() function

Convert a value to a number with a decimal point.

Syntax

float(x=0.0)

Accepts numbers and strings such as "3.5", "1e-3", "inf" and "nan". Floats are approximate binary numbers, so tiny rounding errors are normal.

Parameters

xa number or a string

Return value

float

Example

Code

print(float("3.5") * 2)
print(float(7))
print(float("1e3"))
print(0.1 + 0.2)
print(round(0.1 + 0.2, 2))

Output

7.0
7.0
1000.0
0.30000000000000004
0.3
Good to know. Never compare floats with ==; use math.isclose(a, b).

Related

Learn it step by step in the free lesson Values, Variables & Numbers, with exercises that check your answers.