conversion

float()

Constructs a floating-point number from a number or string. Accepts 'inf', '-inf', 'nan' (case-insensitive).

float(x=0.0)

Parameters

Parameter Purpose
x Number or string to convert
returns IEEE 754 double-precision float

Examples

float('3.14')

Returns 3.14

float('1e-3')

Returns 0.001

float('inf')

Returns positive infinity

float(1)

Returns 1.0

Gotcha

IEEE 754 floats can't represent 0.1 exactly — 0.1 + 0.2 == 0.3 is False. Use math.isclose() or decimal.Decimal for money.

Related built-ins

← All Python built-ins