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
int()
Constructs an integer from a number or string. When x is a string, base selects the numeric base (0 auto-detects from prefix).
str()
Returns a str version of an object. With bytes/bytearray, decodes using encoding.
round()
Rounds a number to ndigits decimal places. When ndigits is None (default), returns an int rounded to nearest integer using banker's rounding.
dict()
Constructs a dictionary from keyword args, another mapping, or an iterable of key/value pairs. Insertion order is preserved (Python 3.7+).