DriversRecommendedOutdated drivers can make a good PC feel brokenScan driver issues before chasing fixes manually.Scan NowOctober 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 Scan×
Skip to content
World desk2 min

How to Check Whether a String Is a Decimal Number in Python

Check numeric text in Python with float() or Decimal(), handle conversion errors, and apply finite-value or format checks that fit your input rules.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

For text that should become a Python float, try float(text) and catch ValueError. If the result must be finite, also check math.isfinite(), because Python accepts spellings for NaN and infinity. If decimal representation matters, parse the original text with Decimal(text) and check is_finite().

How do I check if a string is a number in Python?

Use conversion rather than a string character test. float() attempts to parse the whole input as a floating-point value; malformed text raises ValueError.

import math

def is_finite_float_text(text: str) -> bool:
    try:
        return math.isfinite(float(text))
    except ValueError:
        return False

This function returns true only when the text converts to a finite float. If the application intentionally accepts NaN or infinity, return true after successful conversion instead of calling math.isfinite(). Python documents the accepted float spellings in its built-in types reference.

How do I validate decimal input in Python?

Use Decimal when decimal-oriented input semantics matter, and pass it the original string. Catch InvalidOperation for malformed input; call is_finite() if infinity and NaN should not count.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
from decimal import Decimal, InvalidOperation

def is_finite_decimal_text(text: str) -> bool:
    try:
        return Decimal(text).is_finite()
    except InvalidOperation:
        return False

Decimal('0.1') represents the decimal value written in the input. By contrast, Decimal(0.1) captures the exact value of the already-created binary float; Python’s documented example for Decimal(float('1.1')) is 1.100000000000000088817841970012523233890533447265625. See the decimal module documentation.

What syntax does Python accept?

A successful conversion does not mean text matches a particular form such as digits, a decimal point, and more digits. Python parsers accept forms including signs and exponents, and special values such as NaN and infinity. The Decimal string grammar also allows surrounding whitespace and underscores, and permits Unicode decimal digits.

If a form must meet a specific input contract—for example, no exponent notation, no whitespace, or ASCII digits only—define and validate that contract separately. A narrow expression such as ^[0-9]+.[0-9]+$ rejects integers, signed numbers, and exponent notation, even though a parser may accept them. Parsing answers whether conversion succeeds; policy validation answers whether the input is acceptable for your application.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Why not use isdecimal(), isdigit(), or isnumeric()?

These string methods classify characters; they do not parse general numeric notation. isdecimal() accepts Unicode decimal characters, including Arabic-Indic digits, but not signs or decimal points. isdigit() includes characters such as superscript two, and isnumeric() also recognizes values such as the vulgar fraction one fifth. Those characters are not necessarily valid decimal-number text for your application. Use these methods only when the field is deliberately limited to the relevant character category. See Python’s string type documentation.

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

Which method should I choose?

Requirement Approach Important qualification
Convert ordinary input to a float float(text) in a try/except ValueError NaN and infinity spellings can parse successfully.
Accept only finite floats math.isfinite(float(text)) with conversion error handling Does not enforce a custom text format.
Preserve decimal input semantics Decimal(text), then is_finite() as needed Pass the original string, not a float converted earlier.
Parse using known local decimal and grouping conventions locale.atof(text) Interpretation follows the active LC_NUMERIC locale; see the locale documentation.
Check a digits-only field Choose a character predicate that matches the field definition These predicates do not validate signed or exponent notation.

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.

Leave a Reply

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

Free tools Windows power users keep installed

One-click scans. No signup required.

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
Crashes, No Sound, or Screen Glitches?Free driver scan
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.