def mul(x, y): return x*10 + y def str2float(s): L = list(s) if '.' in L: p = len(L) - L.index('.')-1 L.remove('.') num = list(map(int, L)) return reduce(mul, num)/(10**p) else: num = list(map(int, L)) return reduce(mul, num) print('str2float(\'123.456\') =', str2float('123.456'))
先都按int算, 如果出现小数点再根据小数点的位置除以相对应的值
Sign in to make a reply
Micscoooo
先都按int算, 如果出现小数点再根据小数点的位置除以相对应的值