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

Python 判断小数的函数

时间:2018-04-27 19:44:35      阅读:160      评论:0      收藏:0      [点我收藏+]

标签:spl   oat   1.9   its   count()   整数   list   split   class   

需求分析:
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

 

Python 判断小数的函数

标签:spl   oat   1.9   its   count()   整数   list   split   class   

原文地址:https://www.cnblogs.com/xiehong/p/8963635.html

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