October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsClean PCRecommendedOne scan can reveal what keeps slowing WindowsLook for cleanup and repair opportunities.Run ScanOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk2 min

Convert a Python String to a Floating-Point Number Safely

Python’s built-in float() converts numeric strings to floating-point values. Learn the accepted syntax, how to handle invalid input, and when Decimal is a better fit.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Use Python’s built-in float() to convert a numeric string to a floating-point value: value = float("3.14"). Python does not have a separate built-in type named double; float is the usual type for this purpose.

Convert a numeric string with float()

Pass the string to float(). The result is a Python float:

As an Amazon Associate I earn from qualifying purchases.

value = float("3.14")
print(value)  # 3.14

The accepted syntax includes an optional sign, decimal-point notation, and an exponent. Leading and trailing whitespace is allowed, so this works too:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
value = float("  -1.25e2  ")

The string form and conversion rules are documented in Python’s built-in functions reference.

Handle invalid input

If the text does not match a valid number, float() raises ValueError. Catch it when the value comes from user input or another source that may be malformed:

user_text = input("Enter a number: ")

try:
    value = float(user_text)
except ValueError:
    print("Enter a valid number")

This keeps invalid text from stopping the program unexpectedly. A comma decimal such as "3,14" is not the documented decimal-point form; if your application accepts locale-specific input, normalize it according to the relevant locale before conversion rather than assuming float() will interpret the comma as a decimal separator.

Know which strings produce special values

float() accepts the case-insensitive spellings "nan", "inf", and "-inf", producing NaN and positive or negative infinity. These are valid floating-point values, but they may not be appropriate for every calculation or downstream system. Decide explicitly whether your application should allow them.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Choose float or Decimal

Use float for general numeric work

For ordinary scientific, engineering, and general calculations, float is usually the right conversion. Python’s floats use binary floating-point representation with 53 bits of precision. Many decimal fractions, including 0.1, cannot be represented exactly as binary fractions, so arithmetic can expose small representation effects. Python explains this in its floating-point tutorial.

Use Decimal when decimal arithmetic matters

For financial values or other cases where decimal digits and decimal-oriented arithmetic matter, construct a Decimal directly from the original string:

from decimal import Decimal

amount = Decimal("3.14")

Do not convert the string to float first if you want to preserve its written decimal value. Decimal(float(text)) captures the float’s existing binary approximation, which can produce extra digits. The Python decimal documentation demonstrates the difference between Decimal('0.1') and Decimal(0.1).

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Outdated Drivers Are Slowing You DownFree scan - exact matches
Windows Errors? Fix Them Before They SpreadFree repair scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.