直接上源码
def digital_to_chinese(digital):
if not isinstance(digital, str):
str_digital = str(digital)
else:
str_digital = digital
chinese = {'1': '壹', '2': '贰', '3': '叁', '4': '肆', '5': '伍', '6': '陆', '7': '柒', '8': '捌', '9': '玖', '0': '零'}
left_chinese = ['', '拾', '佰', '仟'] # 小数点左边
left_chinese2 = ['', '万', '亿', '兆'] # 扩展位
right_chinese = ["角", "分", "毫", "厘"] # 小数点右边
leftStr, rightStr = "", ""
if '.' in str_digital:
leftStr, rightStr = str_digital.split('.')
else:
leftStr = str_digital
leftStrArr = []
if leftStr:
leftStrArr = [chinese[v] for v in leftStr]
leftStrArr.reverse()
for i in range(0, len(leftStrArr)):
index = i // 4
index2 = i % 4
if index2 == 0:
if leftStrArr[i] != "零":
leftStrArr[i] += left_chinese2[index]
if index2 > 0:
if leftStrArr[i] != "零":
leftStrArr[i] += left_chinese[index2]
leftStrArr.reverse()
leftStrArr.append("元")
rightStrArr = []
if rightStr:
rightStrArr = [chinese[v] for v in rightStr]
for i in range(0, len(rightStrArr)):
index = i % 3
if rightStrArr[i] != "零":
rightStrArr[i] += right_chinese[index]
else:
rightStrArr.append("整")
return ''.join(leftStrArr) + ''.join(rightStrArr)
执行代码
···
print(digital_to_chinese(“5203.103”))
···
结果
···
伍仟贰佰零叁元壹角零叁毫
···