python编写以函数,将数字金额转换为中文大写金额
时间: 2023-06-08 21:06:29 浏览: 163
好的,以下是Python代码实现:
```python
# encoding:utf-8
def transition(number):
if not isinstance(number, (int, float)):
return '参数类型错误'
if number > 999999999999.99 or number < 0:
return '金额范围有误'
unit_list = ['千', '百', '十', '']
chinese_num_list = ['零', '一', '二', '三', '四', '五', '六', '七', '八', '九']
chinese_unit_list = ['', '万', '亿', '兆']
integral, decimal = str(number).split('.')
integral_list = list(integral)
integral_list.reverse()
unit_index, zero_num, chinese_num, chinese_money = 0, 0, 0, ''
for i, num in enumerate(integral_list):
if num == '0':
zero_num += 1
if unit_index % 4 == 0:
chinese_money = chinese_unit_list[unit_index // 4] + chinese_money
if unit_index % 4 == 3:
chinese_money = '零' + chinese_money
else:
if zero_num > 0:
chinese_money = chinese_num_list[0] + chinese_money
zero_num = 0
chinese_num = chinese_num_list[int(num)]
chinese_money = chinese_num + unit_list[unit_index % 4] + chinese_money
if unit_index % 4 == 3:
chinese_money = chinese_unit_list[unit_index // 4] + chinese_money
unit_index += 1
if zero_num > 0:
chinese_money = chinese_num_list[0] + chinese_money
chinese_money += '元'
if decimal == '00':
chinese_money += '整'
else:
chinese_money += chinese_num_list[int(decimal[0])] + '角'
if decimal[1] != '0':
chinese_money += chinese_num_list[int(decimal[1])] + '分'
return chinese_money
if __name__ == '__main__':
number = 123456789.33
result = transition(number)
print(result)
```
这个函数可以将数字金额转换为中文大写金额,例如将“123456789.33”转换为“壹亿贰仟叁佰肆拾伍万陆仟柒佰捌拾玖元叁角叁分”。
阅读全文