Home > Backend Development > Python Tutorial > Introducing python's method of determining whether a number is a positive decimal or integer

Introducing python's method of determining whether a number is a positive decimal or integer

巴扎黑
Release: 2018-05-14 11:11:27
Original
9616 people have browsed it

This article mainly introduces relevant information about python's examples of judging whether it is a positive decimal and a positive integer. Examples are provided here. The explanation of the examples is very clear. Friends in need can refer to

python to judge whether it is positive. Examples of decimals and positive integers

Implementation code:

def check_float(string):
  #支付时,输入的金额可能是小数,也可能是整数
  s = str(string)
  if s.count('.') == 1: # 判断小数点个数
    sl = s.split('.') # 按照小数点进行分割
    left = sl[0] # 小数点前面的
    right = sl[1] # 小数点后面的
    if left.startswith('-') and left.count('-') == 1 and right.isdigit():
      lleft = left.split('-')[1] # 按照-分割,然后取负号后面的数字
      if lleft.isdigit():
        return False
    elif left.isdigit() and right.isdigit():
      # 判断是否为正小数
      return True
  elif s.isdigit():
    s = int(s)
    if s != 0:
      return True
  return False
Copy after login

The above is the detailed content of Introducing python's method of determining whether a number is a positive decimal or integer. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template