• 博客访问： 65639
• 博文数量： 15
• 博客积分： 152
• 博客等级： 入伍新兵
• 技术积分： 255
• 用 户 组： 普通用户
• 注册时间： 2010-05-29 17:10

2017年（1）

2015年（1）

2013年（8）

2012年（5）

2015-04-24 03:58:19

1. CHINESE_DIGITS = '零一二三四五六七八九'
2. CHINESE_UNITS = ('','十','百','千','万', '亿')
3. TRANS = str.maketrans('一二三四五六七八九十百千', '壹贰叁肆伍陆柒捌玖拾佰仟')

4. __all__ = ["digit2Chinesexx", "digit2ChinexeDX"]

5. def digit2Chinesexx(num):
6.     """Return the small Chinese Charactors string of a num."""
7.     num = int(num)
8.     res = tslt_le16(abs(num))
9.     if num < 0:
10.         res = '负' + res
11.     return res

12. def digit2ChineseDX(num):
13.     """Return the big Chinese Charactors string of a num."""
14.     return digit2Chinesexx(num).translate(TRANS)

15. def tslt_le4(intnum):
16.     lststr = list(str(intnum).zfill(4)[::-1])
17.     units = tuple(CHINESE_UNITS[i] if lststr[i] != '0' else '' for i in range(4))

18.     for i in range(3):
19.         if lststr[i+1] == '0' and lststr[i] == '0':
20.             lststr[i] = ''
21.     else:
22.         if lststr[3] == '0':
23.             lststr[3] = ''

24.     for i in range(4):
25.         if lststr[i]:
26.             lststr[i] = CHINESE_DIGITS[int(lststr[i])]

27.     result = ''.join(lststr[i] + units[i] for i in range(3, -1, -1))

28.     result = result[:-3].replace('二', '两') + result[-3:]

29.     result = result.rstrip('零')

30.     return result

31. def tslt_le8(intnum):
32.     leftint = intnum // 10**4
33.     rightint = intnum % 10**4

34.     left = tslt_le4(leftint)
35.     if left:
36.         left += '万'

37.     right = tslt_le4(rightint)
38.     if leftint and 0 < rightint < 10**3:
39.         right = '零' + right

40.     result = left + right

41.     return result

42. def tslt_le16(intnum):
43.     leftint = intnum // 10**8
44.     rightint = intnum % 10**8

45.     left = tslt_le8(leftint)
46.     if left:
47.         left += '亿'

48.     right = tslt_le8(rightint)
49.     if leftint and 0 < rightint < 10**7:
50.         right = '零' + right

51.     result = left + right

52.     if result == '':
53.         result = '零'

54.     if result.startswith('一十'):
55.         result = result[1:]

56.     return result

57. for i in (0, 123, 10234, 10034, 1000002, 1000000, 9000000100002300):
58.     print(i, digit2ChineseDX(i))

0