Discuss / Python / 通用于浮点数和整数

通用于浮点数和整数

Topic source
DIGITS = {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9}
def char2int(n):
    return DIGITS[n]
def form(x, y):
    return x * 10 + y
i = 1
while i < len(s):
    if s[-i] != ".":
        i = i + 1
    else:
        break
nums = reduce(form, map(char2int, s[:-i] + s[(-i + 1):]))
return nums / 10 ** (i - 1)

  • 1

Reply