Python 判断小数的函数


需求分析:
1.小数点个数可以使用.count()方法
2.按照小数点进行分割 例如: 1.98 [1,98]
3.正小数:小数点左边是整数,右边也是整数 可以使用.isdigits()方法
4.负小数:小数点左边是是负号开头,但是只有一个负号,右边也是整数
代码如下:
 1 def is_fioat(s):
 2     s=str(s)
 3     if s.count(".")==1:#小数点个数
 4         s_list=s.split(".")
 5         left = s_list[0]#小数点左边
 6         right =s_list[1]#小数点右边
 7         if left.isdigit() and right.isdigit():
 8             return  True
 9         elif left.startswith('-') and left.count('_')==1 and left.split('-')[1].isdigit()and right.isdigit():
10             return  True
11     return  False

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM