Python语言如何实现大写汉字和数字的转换

2024年06月03日 建站教程

def chineseNumber2Int(strNum: str):
  result = 0
  temp = 1  # 存放一个单位的数字如:十万
  count = 0  # 判断是否有chArr
  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

本文链接:http://so.lmcjl.com/news/5858/

展开阅读全文
相关内容