from functools import reduce DIGITS = {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9} def str2float(s): n = - len(s) + s.find('.') + 1 s = [p for p in s if p != '.'] f = reduce(lambda x, y: 10 * x + y, map(lambda m: DIGITS[m], s)) * 10**n return f print('str2float(\'123.456\') =', str2float('123.456')) if abs(str2float('123.456') - 123.456) < 0.00001: print('测试成功!') else: print('测试失败!')
Sign in to make a reply
520bv