代码如下:
基于
(1条消息) python人民币金额转汉字大写_Glen_Zou的博客-优快云博客_python人民币转大写
做了一些Debug
如果有什么漏掉的错误情况,请评论给我~
#!/bin/python
# -*- coding: utf8 -*-
import sys
import os
import re
import copy
# 请完成下面这个函数,实现题目要求的功能
# 当然,你也可以不按照下面这个模板来作答,完全按照自己的想法来 ^-^
# ******************************开始写代码******************************
def getFinanceStr(s):
cnNums = ['零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖']
cnIntRadice = ['', '拾', '佰', '仟']
cnIntUnits = ['', '万', '亿',"兆"]
cnDecUnits = ['角', '分']
cnInteger = "整"
cnIntLast = '元'
maxNum = float('inf')
res = ""
parts = []
if s == "":
return ""
money = float(s)
if money == 0:
return cnNums[0] + cnIntLast + cnInteger
money = str(money)
if money.find('.') == -1:
IntegerNum = money
DecimalNum = ""
else:
IntegerNum, DecimalNum = money.split('.')
if int(IntegerNum) > 0:
zeroCount = 0
for i in range(0, len(IntegerNum)):
n = IntegerNum[i:i + 1:1]
p = len(IntegerNum) - i - 1
q = p // 4
m = p % 4
if n == "0":
zeroCount += 1
else:
if zeroCount > 0:
res += cnNums[0]
zeroCount = 0
res += cnNums[int(n)]
if m == 0 and zeroCount < 4:
res += cnIntUnits[q]
else:
res+=cnIntRadice[p%4]
res += cnIntLast
if DecimalNum != "":
for i in range(len(DecimalNum)):
n = DecimalNum[i:i + 1:1]
if n != '0':
res += cnNums[int(n)] + cnDecUnits[i]
if res == "":
res += cnNums[0] + cnIntLast + cnInteger
elif DecimalNum == "0":
res += cnInteger
return res
# ******************************结束写代码******************************
try:
_s = input()
except:
_s = None
res = getFinanceStr(_s)
print(res + "\n")