HOWTO · Python

How to Convert String to Double in Python

This tutorial demonstrates how to convert a string type to a numeric variable with a double precision in Python

On this page

Python has no separate built-in double type. Its built-in float is a binary floating-point type, commonly used when another language or API asks for a double. A float stores an approximation for many decimal fractions, so converting a string does not preserve arbitrary decimal precision. Use Decimal when decimal arithmetic, such as monetary calculations, needs decimal digits to be represented predictably.

What “double” means in Python

In many languages, “double” means a double-precision binary floating-point value. Python’s float usually serves that role, but the name and details of the type are language-specific. The conversion changes the value’s representation; it does not make every decimal fraction exact. For example, the decimal value 0.1 has no finite binary representation.

Convert a numeric string with float()

Pass a string containing a number to the built-in float() function. The example prints both the usual floating-point result and a Decimal made from the same text:

Run this example:

from decimal import Decimal

numeric_text = "1974.6323"
number = float(numeric_text)
decimal_number = Decimal(numeric_text)
print(number)
print(decimal_number)
1974.6323
1974.6323

The float() call is the direct choice for ordinary numeric parsing, scientific calculations, and APIs that expect a Python float. The displayed decimal form is convenient, but it does not show every detail of the binary value held internally. Arithmetic can reveal those representation differences, so avoid relying on exact equality for many computed floats; compare within a tolerance when approximate results are appropriate.

Use Decimal when decimal exactness matters

Decimal is a class from Python’s standard decimal module, not another spelling of float or a double type. Construct it from the original string, as above, to preserve the decimal digits supplied by the user. Constructing it from a float instead captures that float’s existing binary approximation.

Decimal arithmetic follows a configurable precision context, so it does not provide unlimited precision or make every calculation exact. It is useful when decimal representation and rounding rules matter, such as in prices. Choose the precision and rounding policy for the application, and keep operands in the same numeric family: Python does not generally allow arithmetic that mixes Decimal and float directly.

Handle invalid input and choose the right type

If text is not a valid number, float() raises ValueError, while Decimal() signals decimal.InvalidOperation under the default context. Catch the relevant error at the input boundary and report a useful message or ask for corrected input; do not silently turn bad data into a plausible number. For example:

from decimal import Decimal, InvalidOperation

numeric_text = "12.50x"
try:
    amount = Decimal(numeric_text)
except (InvalidOperation, ValueError):
    amount = None
    print("Enter a valid decimal amount.")

Use float() for the normal Python equivalent of a double and for calculations where binary floating-point is suitable. Use Decimal when decimal digits and an explicit rounding policy are important. Both expect numeric text in a format their parser accepts; a localized value such as "1,25" is not automatically interpreted as 1.25, so normalize input according to the application’s locale rules before conversion.