例:’123.123‘变为123.123
思路:将字符串以小数点为界分开两部分,拼成数字后,再将小数部分缩小相应的倍数。
from functools import reduce
def str2float(s):
def fn(x,y):
return x*10+y
big,small=s.split('.')
def split(s):
return float(s[0])
return reduce(fn,map(split,big))+reduce(fn,map(split,small))/(10**len(list(small)))
str2float('0.0213')
0.0213