Python convert numeric strings to float type values

Convert Python numeric strings to float type values

def str2float(s):
    def str2int(s):
        return {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9}[s]

    def f(x, y):
        return x * 10 + y

    resultNum = 0
    numL = s.split('.')
    for i, j in enumerate(numL):
        if i == 0:
            resultNum = resultNum + reduce(f, map(str2int, j))
        else:
            resultNum = resultNum + reduce(f, map(str2int, j))*pow(10, 0 - len(j))
            
    return resultNum

Comments

Popular posts from this blog

Python Receiving and parse JSON Data via UDP protocol

ubus lua client method and event registration code demo/example