例:写一个判断小数的函数
需求:判断小数的函数
需求分析: 1、小数点数 .count() 2、按照小数点进行分割 1.98 -> [1,98] 3、正小数:小数点左边是整数,右边也是整数 .isdigit() 4、负小数:小数点左边是以负号开头,但是只有一个负号,右边是整数
def is_float(s): s = str(s) if s.count('.')==1:#小数点个数 s_list = s.split('.') left = s_list[0] #小数点左边 right = s_list[1] #小数点右边 if left.isdigit() and right.isdigit(): #正小数 return True elif left.startswith('-') and left.count('-')==1 and \ left.split('-')[1].isdigit() and \ right.isdigit(): #判断合法负小数 return True return False
转载于:https://www.cnblogs.com/lynn-chen/p/8984817.html
相关资源:JAVA上百实例源码以及开源项目