Discuss / Python / str2float

str2float

Topic source

DIGITS = {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9}

def str2float(s:str):

    a=n=1

    for i in s:

        if i=='.':

            a=10

        else:

            n=n*a

    return (reduce(lambda x,y:x*10+y,map(lambda a:DIGITS[a],s.replace('.',''))))/n


  • 1

Reply