码迷,mamicode.com
首页 > 编程语言 > 详细

python - 判断是否为正小数和正整数

时间:2017-07-22 17:00:50      阅读:227      评论:0      收藏:0      [点我收藏+]

标签:dig   分割   pytho   str   tar   bsp   输入   python   turn   


判断输入的金额是否为正整数和正小数

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

 

python - 判断是否为正小数和正整数

标签:dig   分割   pytho   str   tar   bsp   输入   python   turn   

原文地址:http://www.cnblogs.com/lhly/p/7221456.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!