[python]代码库
# -*- coding: utf-8 -*-
from functools import reduce
digits = {'.':-1, '0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9}
def str2float(s):
nums=map(lambda ch:digits[ch],s)
point = 0
# print(list(nums))# <map object at 0x03781750>
def t_float(f,n):
nonlocal point
if n == -1:
point = 1
return f
if point == 0 :
return f*10+n
else :
point = point *10
return f + n/ point
return reduce(t_float,nums,0.0)
print('str2float(\'123.456\') =', str2float('123.456'))
if abs(str2float('123.456') - 123.456) < 0.00001:
print('测试成功!')
else:
print('测试失败!')