|
#############
In [75]: mapping = {str(x):x for x in range(9)}
In [76]: mapping
Out[76]: {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8}
In [78]: s.partition('.')
Out[78]: ('123', '.', '456')
In [81]: s = "123"
In [82]: s.partition('.')
Out[82]: ('123', '', '')
In [83]: s = "12.3"
In [84]: s.partition('.')
Out[84]: ('12', '.', '3')
In [93]: s = "123.456"
In [94]: i, _, f = s.partition('.')
In [97]: ret = 0
In [98]: for idx, x in enumerate(i[::-1]):
...: ret += mapping[x] * 10 ** idx
...:
In [99]: ret
Out[99]: 123
In [100]: for idx, x in enumerate(f):
...: ret += mapping[x] / 10 ** (idx+1)
...:
In [101]: ret
Out[101]: 123.456 #结果可能会有精度的问题
###先全部当成整数来运算###
In [106]: ret = 0
In [107]: for idx, x in enumerate((i+f)[::-1]):
...: ret += mapping[x] * 10 ** idx
...:
In [108]: ret / 10 ** len(f)
Out[108]: 123.456 #精度损失会少一点 |
|
|