Discuss / Python / def str2float(s):

def str2float(s):

Topic source

ARTxuan666

#1 Created at ... [Delete] [Delete and Lock User]

# s.find()记录 '.' 位置,

# s.replace()去掉 '.' ,看成一个整数

# 最后除以n次10,算出最后结果

    return reduce(lambda x, y: x * 10 + y, map(int, s.replace('.', '')))/10 ** (len(s)-s.find('.')-1)


  • 1

Reply