python 将中文数字转换为阿拉伯数字
def chineseNumber2Int(strNum: str):
result = 0
temp = 1
count = 0
cnArr = ['一', '二', '三', '四', '五', '六', '七', '八', '九']
chArr = ['十', '百', '千', '万', '亿']
for i in range(len(strNum)):
b = True
c = strNum[i]
for j in range(len(cnArr)):
if c == cnArr[j]:
if count != 0:
result += temp
count = 0
temp = j + 1
b = False
break
if b:
for j in range(len(chArr)):
if c == chArr[j]:
if j == 0:
temp *= 10
elif j == 1:
temp *= 100
elif j == 2:
temp *= 1000
elif j == 3:
temp *= 10000
elif j == 4:
temp *= 100000000
count += 1
if i == len(strNum) - 1:
result += temp
return result
print(chineseNumber2Int('一'))
print(chineseNumber2Int('二'))
print(chineseNumber2Int('三'))
print(chineseNumber2Int('四'))
print(chineseNumber2Int('五'))
print(chineseNumber2Int('六'))
print(chineseNumber2Int('七'))
print(chineseNumber2Int('八'))
print(chineseNumber2Int('九'))
print(chineseNumber2Int('十'))
print(chineseNumber2Int('十一'))
print(chineseNumber2Int('十二'))
print(chineseNumber2Int('二十二'))
print(chineseNumber2Int('一百'))
print(chineseNumber2Int('一百一十一'))
print(chineseNumber2Int('一千一百一十一'))
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
字符串传入中文数字 返回阿拉伯数字