
正文
Python 判断小数的函数
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
需求分析:
1.小数点个数可以使用.count()方法
2.按照小数点进行分割 例如: 1.98 [1,98]
3.正小数:小数点左边是整数,右边也是整数 可以使用.isdigits()方法
4.负小数:小数点左边是是负号开头,但是只有一个负号,右边也是整数
代码如下:
def is_fioat(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







