Skip to main content
added 10 characters in body
Source Link
user2390182
  • 73.7k
  • 6
  • 71
  • 95

You could use a fairly simple regular expression:

import re

def isFloat(n):
    n = str(n)  # optional; make sure you have string
    return bool(re.match(r'^-?\d+(\.\d+)?$', n))  # bool is not strictly necessary
    # ^         string beginning
    # -?        an optional -
    # \d+       followed by one or more digits (\d* if you want to allow e.g. '.95')
    # (\.\d+)?  followed by an optional group of a dot and one or more digits
    # $         string end

>>> isFloat('4')
True
>>> isFloat('4.567')
True
>>> isFloat('-4.567')
True
>>> isFloat('-4.')
False
>>> isFloat('-4.45v')
False
>>> isFloat('NaN')
False
>>> isFloat('1e1')
False

You could use a fairly simple regular expression:

import re

def isFloat(n):
    n = str(n)  # make sure you have string
    return bool(re.match(r'^-?\d+(\.\d+)?$', n))  # bool is not strictly necessary
    # ^         string beginning
    # -?        an optional -
    # \d+       followed by one or more digits (\d* if you want to allow e.g. '.95')
    # (\.\d+)?  followed by an optional group of a dot and one or more digits
    # $         string end

>>> isFloat('4')
True
>>> isFloat('4.567')
True
>>> isFloat('-4.567')
True
>>> isFloat('-4.')
False
>>> isFloat('-4.45v')
False
>>> isFloat('NaN')
False

You could use a fairly simple regular expression:

import re

def isFloat(n):
    n = str(n)  # optional; make sure you have string
    return bool(re.match(r'^-?\d+(\.\d+)?$', n))  # bool is not strictly necessary
    # ^         string beginning
    # -?        an optional -
    # \d+       followed by one or more digits (\d* if you want to allow e.g. '.95')
    # (\.\d+)?  followed by an optional group of a dot and one or more digits
    # $         string end

>>> isFloat('4')
True
>>> isFloat('4.567')
True
>>> isFloat('-4.567')
True
>>> isFloat('-4.')
False
>>> isFloat('-4.45v')
False
>>> isFloat('NaN')
False
>>> isFloat('1e1')
False
edited body
Source Link
user2390182
  • 73.7k
  • 6
  • 71
  • 95

You could use a fairly simple regular expressionregular expression:

import re

def isFloat(n):
    n = str(n)  # make sure you have string
    return bool(re.match(r'^-?\d+(\.\d+)?$', n))  # bool is not strictly necessary
    # ^         string beginning
    # -?        an optiomaloptional -
    # \d+       followed by one or more digits (\d* if you want to allow e.g. '.95')
    # (\.\d+)?  followed by an optional group of a dot and one or more digits
    # $         string end

>>> isFloat('4')
True
>>> isFloat('4.567')
True
>>> isFloat('-4.567')
True
>>> isFloat('-4.')
False
>>> isFloat('-4.45v')
False
>>> isFloat('NaN')
False

You could use a fairly simple regular expression:

import re

def isFloat(n):
    return bool(re.match(r'^-?\d+(\.\d+)?$', n))  # bool is not strictly necessary
    # ^         string beginning
    # -?        an optiomal -
    # \d+       followed by one or more digits (\d* if you want to allow e.g. '.95')
    # (\.\d+)?  followed by an optional group of a dot and one or more digits
    # $         string end

>>> isFloat('4')
True
>>> isFloat('4.567')
True
>>> isFloat('-4.567')
True
>>> isFloat('-4.')
False
>>> isFloat('-4.45v')
False
>>> isFloat('NaN')
False

You could use a fairly simple regular expression:

import re

def isFloat(n):
    n = str(n)  # make sure you have string
    return bool(re.match(r'^-?\d+(\.\d+)?$', n))  # bool is not strictly necessary
    # ^         string beginning
    # -?        an optional -
    # \d+       followed by one or more digits (\d* if you want to allow e.g. '.95')
    # (\.\d+)?  followed by an optional group of a dot and one or more digits
    # $         string end

>>> isFloat('4')
True
>>> isFloat('4.567')
True
>>> isFloat('-4.567')
True
>>> isFloat('-4.')
False
>>> isFloat('-4.45v')
False
>>> isFloat('NaN')
False
added 170 characters in body
Source Link
user2390182
  • 73.7k
  • 6
  • 71
  • 95

You could use a fairly simple regular expression:

import re

def isFloat(n):
    return bool(re.match(r'^-?\d+(\.\d+)?$', n))  # bool is not strictly necessary
    # ^         string beginning
    # -?        an optiomal -
    # \d+       followed by one or more digits (\d* if you want to allow e.g. '.95')
    # (\.\d+)?  followed by an optional group of a dot and one or more digits
    # $         string end

>>> isFloat('4')
True
>>> isFloat('4.567')
True
>>> isFloat('-4.567')
True
>>> isFloat('-4.')
False
>>> isFloat('-4.45v')
False
>>> isFloat('NaN')
False

You could use a fairly simple regular expression:

import re

def isFloat(n):
    return bool(re.match(r'^-?\d+(\.\d+)?$', n))  # bool is not strictly necessary

>>> isFloat('4')
True
>>> isFloat('4.567')
True
>>> isFloat('-4.567')
True
>>> isFloat('-4.')
False
>>> isFloat('-4.45v')
False
>>> isFloat('NaN')
False

You could use a fairly simple regular expression:

import re

def isFloat(n):
    return bool(re.match(r'^-?\d+(\.\d+)?$', n))  # bool is not strictly necessary
    # ^         string beginning
    # -?        an optiomal -
    # \d+       followed by one or more digits (\d* if you want to allow e.g. '.95')
    # (\.\d+)?  followed by an optional group of a dot and one or more digits
    # $         string end

>>> isFloat('4')
True
>>> isFloat('4.567')
True
>>> isFloat('-4.567')
True
>>> isFloat('-4.')
False
>>> isFloat('-4.45v')
False
>>> isFloat('NaN')
False
Source Link
user2390182
  • 73.7k
  • 6
  • 71
  • 95
Loading