def str2float(s): DIGITS = {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9} def cheng(x, y): #整数部分 return x * 10 + y def chu(x, y): #小数部分 if x>=1: return x/100+y/10 else: return x/10+y/10 def str2num(k): #字符串转整数 return DIGITS[k] max = len(s) zhengshu=[] xiaoshu=[] i = 0 while i < max: #字符串以. 分割,也可以直接用.split() if s[i] != '.': zhengshu.append(s[i]) i=i+1 else: xiaoshu=s[-1:i:-1] #逆向存储 break res = reduce(cheng, map(str2num, zhengshu)) + reduce(chu, map(str2num, xiaoshu)) return res
Sign in to make a reply
-一世欢颜