# -*- coding: utf-8 -*- from functools import reduce def str2float(s): def char2num(s): digits = {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9} return digits[s] s1,s2=s.split('.') s2=s2[::-1] s2=s2+'0' so1=reduce(lambda x,y:x*10+y, map(char2num,s1)) so2=reduce(lambda x,y:x*0.1+y, map(char2num,s2)) return so1+so2 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
小宝12216