Parse decimal input
Convert request, environment, or command-line text into numeric values before calculations.
Python · Class
float constructs a floating-point number from a number or string. It also accepts no argument, returning 0.0.
float constructs a floating-point value from a number or a string. A string may contain a signed decimal value with surrounding whitespace; omitting the argument produces 0.0.
float(x=0.0)| Name | Type | Description |
|---|---|---|
x | number or string | A number or string to convert; omit it to obtain 0.0. |
A floating-point number; with no argument, 0.0.
value = float("1.25")
whole = float(3)
default = float()
print(value, whole, default)The snippet converts a decimal string and an integer, then shows the zero-argument default.
Convert request, environment, or command-line text into numeric values before calculations.
Normalize integer and floating-point values to one numeric representation.
Create a zero-valued floating-point default when an optional numeric value is absent.
float() creates a floating-point number from a number or string. With no argument, it returns 0.0.
Follow-up: What does float(3) do? It returns a floating-point value with the same value, subject to floating-point precision.
A string argument may contain a decimal number, an optional leading sign, and whitespace. Examples include values such as '+1.23' and ' -12345'.
Follow-up: What happens with nonnumeric text? Conversion fails because the string does not describe a valid decimal number.
For an integer or another floating-point value, float returns a floating-point number with the same value within Python's floating-point precision. No argument returns 0.0.
Follow-up: Why can decimal arithmetic differ from expectations? Floating-point precision limits how values are represented and preserved.
intcomplexstrroundabs