Python-判斷正負小數


#1、必須只有一個小數點
#2、小數點的左邊必須是整數,小數點的右邊必須是正整數
def is_float1(s=None):
    s = str(s) #.1
    if s.count('.')==1:
        left,right = s.split('.') #['-','1']
        if left.isdigit() and right.isdigit() and int(right)>0:#判斷正小數
            return True
        elif left.startswith('-') and left.count('-')==1 and right.isdigit() and int(right)>0:
            #先判斷負號開頭,只有一個負號,小數點右邊是整數
            lleft = left.split('-')[1] #如果有負號的話,按照負號分隔,取負號后面的數字
            if lleft.isdigit():#判斷左邊負號后邊是整數
                return True
    return False

print(is_float1(-111111.0))
print(is_float1('s.1'))
print(is_float1('...1'))
print(is_float1('1.s'))
print(is_float1(-1.1))

結果:
False
False
False
False
True

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM