Function - example (judging decimals)

Example: Write a function to determine decimals

Requirement: A function to determine decimals

Demand analysis:
1. Decimal point. count()
2. Divide according to the decimal point 1.98 -> [1,98]
3. Positive decimal: the left side of the decimal point is an integer, and the right side is also an integer. isdigit()
4. Negative decimal point: the left side of the decimal point is an integer Starts with a minus sign, but only has one minus sign, with an integer to the right

def is_float(s):
s = str(s)
if s.count('.')==1:#number of decimal points
s_list = s.split('.')
left = s_list[0] #left of decimal point
right = s_list[1] #right of the decimal point
if left.isdigit() and right.isdigit(): #positive decimal
return True
elif left.startswith('-') and left.count('-')==1 and \
left. split('-')[1].isdigit() and \
right.isdigit(): #Determine legal negative decimals
return True
return False

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325260215&siteId=291194637