中文数字直接用"数字+权位"的方式组成数字。数字:“零一二三四五六七八九”,权位:“十,百,千,万,亿”。
特点:1.每个基础数字都跟着一个权位,这个权位就是这个数字的量值,相当于阿拉伯数字中的数位。
2.以"万"为小节。
注意问题
中文数字中的零:
1.以10000为小节,小节的结尾即使是0,也不使用零;
2.小节内两个非0数字之间要使用零;
3.当小节的千为是0时,若本小姐的前一节无其他数字,则不适用零,否则就要用零。
阿拉伯数字转中文数字
chn_num_char = ["零", "一", "二", "三", "四", "五", "六", "七", "八", "九"]
chn_unit_section = ["", "万", "亿", "万亿"]
chn_unit_char = ["", "十", "百", "千"]
def transfrom(section):
"""
节内数字转换
args:
section: 四位以内的整数
return:
chn_section: 中文数字
"""
chn_section = ""
str_ins = ""
# 权位
unit_pos = 0
# 零的标志位
zero = True
while section > 0:
v = section % 10
if v == 0:
if not zero:
zero = True
chn_section = chn_num_char[v] + chn_section
else:
zero = False
str_ins = chn_num_char[v]
str_ins += chn_unit_char[unit_pos]
chn_section = str_ins + chn_section
unit_pos += 1
section //= 10
return chn_section
print(1, transfrom(1))
print(10, transfrom(10))
print(11, transfrom(11))
print(100, transfrom(100))
print(110, transfrom(110))
print(101, transfrom(101))
print(111, transfrom(111))
print(1000, transfrom(1000))
print(1100, transfrom(1100))
print(1010, transfrom(1010))
print(1001, transfrom(1001))
print(1011, transfrom(1011))
print(1101, transfrom(1101))
print(1110, transfrom(1110))
print(1111, transfrom(1111))
1 一
10 一十
11 一十一
100 一百
110 一百一十
101 一百零一
111 一百一十一
1000 一千
1100 一千一百
1010 一千零一十
1001 一千零一
1011 一千零一十一
1101 一千一百零一
1110 一千一百一十
1111 一千一百一十一
def Arabic_to_Chinese(number):
"""
阿拉伯数字转中文数字:
args:
number: 阿拉伯整数
return:
chn_number: 中文数字
"""
chn_number = ""
str_ins = ""
if number == 0:
chn_number = chn_num_char[0]
return chn_number
unit_pos = 0 # 权位
zero = False
while number > 0:
section = number % 10000
if zero:
chn_number = chn_num_char[0] + chn_number
if section > 0:
str_ins = transfrom(section) + chn_unit_section[unit_pos]
chn_number = str_ins + chn_number
zero = (section<1000 and section>0)
number //= 10000
unit_pos += 1
return chn_number
print(0, Arabic_to_Chinese(0))
print(1, Arabic_to_Chinese(1))
print(2, Arabic_to_Chinese(2))
print(3, Arabic_to_Chinese(3))
print(4, Arabic_to_Chinese(4))
print(5, Arabic_to_Chinese(5))
print(6, Arabic_to_Chinese(6))
print(7, Arabic_to_Chinese(7))
print(8, Arabic_to_Chinese(8))
print(9, Arabic_to_Chinese(9))
print(10, Arabic_to_Chinese(10))
print(11, Arabic_to_Chinese(11))
print(110, Arabic_to_Chinese(110))
print(111, Arabic_to_Chinese(111))
print(100, Arabic_to_Chinese(100))
print(102, Arabic_to_Chinese(102))
print(1020, Arabic_to_Chinese(1020))
print(1001, Arabic_to_Chinese(1001))
print(1015, Arabic_to_Chinese(1015))
print(1000, Arabic_to_Chinese(1000))
print(10000, Arabic_to_Chinese(10000))
print(100000, Arabic_to_Chinese(100000))
print(1000000, Arabic_to_Chinese(1000000))
print(10000000, Arabic_to_Chinese(10000000))
print(100000000, Arabic_to_Chinese(100000000))
print(1000000000, Arabic_to_Chinese(1000000000))
print(1000001000, Arabic_to_Chinese(1000001000))
print(1000000100, Arabic_to_Chinese(1000000100))
print(200010, Arabic_to_Chinese(200010))
print(2000105, Arabic_to_Chinese(2000105))
print(20001007, Arabic_to_Chinese(20001007))
print(2000100190, Arabic_to_Chinese(2000100190))
print(1040010000, Arabic_to_Chinese(1040010000))
print(200012301, Arabic_to_Chinese(200012301))
print(2005010010, Arabic_to_Chinese(2005010010))
print(4009060200, Arabic_to_Chinese(4009060200))
print(4294967295, Arabic_to_Chinese(4294967295))
0 零
1 一
2 二
3 三
4 四
5 五
6 六
7 七
8 八
9 九
10 一十
11 一十一
110 一百一十
111 一百一十一
100 一百
102 一百零二
1020 一千零二十
1001 一千零一
1015 一千零一十五
1000 一千
10000 一万
100000 一十万
1000000 一百万
10000000 一千万
100000000 一亿
1000000000 一十亿
1000001000 一十亿一千
1000000100 一十亿零一百
200010 二十万零一十
2000105 二百万零一百零五
20001007 二千万一千零七
2000100190 二十亿零一十万零一百九十
1040010000 一十亿四千零一万
200012301 二亿零一万二千三百零一
2005010010 二十亿零五百零一万零一十
4009060200 四十亿零九百零六万零二百
4294967295 四十二亿九千四百九十六万七千二百九十五
中文数字转阿拉伯数字
cn_sum = {"零":0, '一': 1, '二': 2, '三': 3, '四': 4, '五': 5, '六': 6,\
'七': 7, '八': 8, '九': 9, '十': 10, '百': 100, '千': 1000, '万': 10000, '亿': 100000000}
def Chinese_to_Arabic(chn_number):
"""
中文数字转阿拉伯数字
args:
chn_number: 中文数字
return:
number: 阿拉伯数字
"""
pos = 0
number = 0
section = 0
num = 0
if cn_sum[chn_number[pos]] == "零":
return number
while pos < len(chn_number):
if cn_sum[chn_number[pos]] < 10 and cn_sum[chn_number[pos]] > 0:
num = cn_sum[chn_number[pos]]
else:
if cn_sum[chn_number[pos]] > 1000:
# 一节结束
section = (section + num) * cn_sum[chn_number[pos]]
num = 0
number += section
section = 0
elif cn_sum[chn_number[pos]] < 1001 and cn_sum[chn_number[pos]] > 9:
# 节内计算
section += num * cn_sum[chn_number[pos]]
num = 0
# 最后一节直接输出
if pos == len(chn_number)-1:
section += num
number += section
pos += 1
return number
print("零", Chinese_to_Arabic("零"))
print("一", Chinese_to_Arabic("一"))
print("二", Chinese_to_Arabic("二"))
print("三", Chinese_to_Arabic("三"))
print("四", Chinese_to_Arabic("四"))
print("五", Chinese_to_Arabic("五"))
print("六", Chinese_to_Arabic("六"))
print("七", Chinese_to_Arabic("七"))
print("八", Chinese_to_Arabic("八"))
print("九", Chinese_to_Arabic("九"))
print("一十", Chinese_to_Arabic("一十"))
print("一十一", Chinese_to_Arabic("一十一"))
print("一百一十", Chinese_to_Arabic("一百一十"))
print("一百一十一", Chinese_to_Arabic("一百一十一"))
print("一百", Chinese_to_Arabic("一百"))
print("一百零二", Chinese_to_Arabic("一百零二"))
print("一千零二十", Chinese_to_Arabic("一千零二十"))
print("一千零一", Chinese_to_Arabic("一千零一"))
print("一千零一十五", Chinese_to_Arabic("一千零一十五"))
print("一万", Chinese_to_Arabic("一万"))
print("一万", Chinese_to_Arabic("一万"))
print("一十万", Chinese_to_Arabic("一十万"))
print("一百万", Chinese_to_Arabic("一百万"))
print("一千万", Chinese_to_Arabic("一千万"))
print("一亿", Chinese_to_Arabic("一亿"))
print("一十亿", Chinese_to_Arabic("一十亿"))
print("一十亿一千", Chinese_to_Arabic("一十亿一千"))
print("一十亿零一百", Chinese_to_Arabic("一十亿零一百"))
print("二十万零一十", Chinese_to_Arabic("二十万零一十"))
print("二百万零一百零五", Chinese_to_Arabic("二百万零一百零五"))
print("二千万一千零七", Chinese_to_Arabic("二千万一千零七"))
print("二十亿零一十万零一百九十", Chinese_to_Arabic("二十亿零一十万零一百九十"))
print("一十亿四千零一万", Chinese_to_Arabic("一十亿四千零一万"))
print("二亿零一万二千三百零一", Chinese_to_Arabic("二亿零一万二千三百零一"))
print("二十亿零五百零一万零一十", Chinese_to_Arabic("二十亿零五百零一万零一十"))
print("四十亿零九百零六万零二百", Chinese_to_Arabic("四十亿零九百零六万零二百"))
print("四十二亿九千四百九十六万七千二百九十五", Chinese_to_Arabic("四十二亿九千四百九十六万七千二百九十五"))
零 0
一 1
二 2
三 3
四 4
五 5
六 6
七 7
八 8
九 9
一十 10
一十一 11
一百一十 110
一百一十一 111
一百 100
一百零二 102
一千零二十 1020
一千零一 1001
一千零一十五 1015
一万 10000
一万 10000
一十万 100000
一百万 1000000
一千万 10000000
一亿 100000000
一十亿 1000000000
一十亿一千 1000001000
一十亿零一百 1000000100
二十万零一十 200010
二百万零一百零五 2000105
二千万一千零七 20001007
二十亿零一十万零一百九十 2000100190
一十亿四千零一万 1040010000
二亿零一万二千三百零一 200012301
二十亿零五百零一万零一十 2005010010
四十亿零九百零六万零二百 4009060200
四十二亿九千四百九十六万七千二百九十五 4294967295