|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 随便找找看看 于 2020-1-15 19:59 编辑
# 输入304005010.08,如何将输出结果:‘叁亿零仟肆佰零拾零萬伍仟零佰壹拾零圆捌分’ 中的 零圆,零拾、零佰、零仟、、零萬、零亿。这些转换成正确的值
# 正确结果:叁亿零肆佰萬零伍仟壹零拾圆零捌分
# 是否可以在源代码上修改。
num = ["壹","贰","叁","肆","伍","陆","柒","捌","玖","零"]
rmbdw = ["圆","拾","佰","仟","萬","拾","佰","仟","亿","拾","佰","仟"]
empty =''
while True:
yuan = []
jiao = []
rmb = input("请输入任意一个金额:")
if rmb == empty:
break
else:
rmb1 = rmb.split('.')
if int(rmb1[0]) >= 1 and len(rmb1) <= 1:
a = len(rmb1[0])-1
for i in list(rmb1[0]):
j = int(i)
yuan.append(num[j-1])
yuan.append(rmbdw[a])
a = a - 1
print ("" .join(yuan) + '整')
elif int(rmb1[0]) >= 0 and len(rmb1) > 1:
a = len(rmb1[0])-1
for i in list(rmb1[0]):
j = int(i)
yuan.append(num[j-1])
yuan.append(rmbdw[a])
a = a - 1
if len(rmb1[1]) == 1:
jiao.append(num[int(rmb1[1])-1] + '角')
if len(rmb1[1]) == 2 and int(rmb1[1][0]) != 0 :
jiao.append(num[int(rmb1[1][0])-1] + '角' + num[int(rmb1[1][1])-1] + '分')
if len(rmb1[1]) == 2 and int(rmb1[1][0]) == 0 :
jiao.append(num[int(rmb1[1][1])-1] + '分')
print("".join(yuan + jiao))
input('')
本帖最后由 XiaoPaiShen 于 2020-1-16 04:15 编辑 # -*- coding: utf-8 -*-
import re
class Current_Currency:
def __init__(self):
self.chn_numb = ['零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖']
self.num_unit = ["", "拾", "佰", "仟"]
self.group_unit = ["","萬","亿", "兆"]
self.rmb_unit = ['圆', '角', '分']
def split_to_group(self, str_integer):
len_curr = len(str_integer)
div, mod = divmod(len_curr, 4)
result = []
for index in range(div):
start = mod + (div-index-1)*4
end = start + 4
result.append((str_integer[start:end], index))
if mod:
result.append((str_integer[0:mod], index+1))
result.reverse()
return result
def convert_group_item(self, group_item):
result = ''
len_curr = len(group_item)
for index in range(len_curr):
num_value = int(group_item[index])
location = len_curr-index-1
if num_value:
result += self.chn_numb[num_value] + self.num_unit[location]
elif not result or not result[-1] == "零":
result += self.chn_numb[num_value]
return result.rstrip("零")
def convert_integer(self, str_integer):
result = ''
need_zero = False
groups = self.split_to_group(str_integer)
for item, index in groups:
if item != '0000':
if need_zero:
result += "零"
need_zero = False
chn_item = self.convert_group_item(item) + self.group_unit[index]
result += chn_item
else:
need_zero = True
result = re.sub(r'([\u96F6])\1+', r'\1', result)
return result + self.rmb_unit[0]
def convert_decimal(self, str_decimal):
result = ''
for index, digital in enumerate(str_decimal):
if int(digital):
result += self.chn_numb[int(digital)] + self.rmb_unit[index+1]
return result
def convert_currency(self, currency):
str_currency = str(currency)
if not str_currency:
return
integer, decimal = str_currency.split('.')
result = self.convert_integer(integer)
if not decimal.strip('0'):
return result + '整'
else:
return result + self.convert_decimal(decimal)
if __name__=='__main__':
cc = Current_Currency()
target = 304005010.08
print(cc.convert_currency(target))
|
|