python determines whether it is an instance of a positive decimal and a positive integer

  • 2020-06-12 09:53:10
  • OfStack

python determines whether it is an instance of a positive decimal and a positive integer

Implementation code:


def check_float(string):
  # When paying, the amount entered may be decimal or integer 
  s = str(string)
  if s.count('.') == 1: #  Number of decimal points 
    sl = s.split('.') #  It's divided by the decimal point 
    left = sl[0] #  Before the decimal point 
    right = sl[1] #  After the decimal point 
    if left.startswith('-') and left.count('-') == 1 and right.isdigit():
      lleft = left.split('-')[1] #  In accordance with the - Divide, and then take the number after the minus sign 
      if lleft.isdigit():
        return False
    elif left.isdigit() and right.isdigit():
      #  Determine if it is a positive decimal 
      return True
  elif s.isdigit():
    s = int(s)
    if s != 0:
      return True
  return False

Above is the detailed explanation of python to determine whether it is a positive decimal and a positive integer. There are many articles about python in this site. I hope you can search and consult them.


Related articles: