Python实现字符串转换成浮点型数字

使用Python实现float函数

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 char2num(s):
    return digits[s]
def str2int(s):
    return reduce(lambda x, y :x*10+y,map(char2num,s))
def intlen(i):
    return len('%d'%i)
def int2dec(i):
    return i/(10**intlen(i))
def str2float(s):
    return reduce(lambda x,y : x+int2dec(y),map(str2int,s.split('.')))
print(str2float('123.4567'))

猜你喜欢

转载自blog.csdn.net/Catdingwt/article/details/80615091