python2.x实现人民币转大写人民币
作者:haeasringnar 时间:2023-06-26 10:35:53
本文实例为大家分享了python实现人民币转大写人民币的具体代码,供大家参考,具体内容如下
直接上代码:
# -*- coding: utf-8 -*-
def changenum( change_number ):
format_num = ["零","壹","贰","叁","肆","伍","陆","柒","捌","玖"]
format_name = ["分", "角", "元",
"拾","百","千","万",
"拾","百","千","亿",
"拾","百","千","万",
"拾","百","千","兆"]
if type( change_number ) == str:
if '.' in change_number:
try: change_number = float( change_number )
except: raise ValueError, '%s can\'t change'%change_number
else:
try: change_number = int( change_number )
except: raise ValueError, '%s can\'t change'%change_number
if type( change_number ) == float:
real_numbers = []
for i in range( len( format_name ) - 3, -3, -1 ):
if change_number >= 10 ** i or i < 1:
real_numbers.append( int( round( change_number/( 10**i ), 2)%10 ) )
elif isinstance( change_number, (int, long) ):
real_numbers = [ int( i ) for i in str( change_number ) + '00' ]
else:
raise ValueError, '%s can\'t change'%change_number
zflag = 0
start = len(real_numbers) - 3
change_words = []
for i in range(start, -3, -1):
if 0 <> real_numbers[start-i] or len(change_words) == 0:
if zflag:
change_words.append(format_num[0])
zflag = 0
change_words.append( format_num[ real_numbers[ start - i ] ] )
change_words.append(format_name [i+2])
elif 0 == i or (0 == i%4 and zflag < 3):
change_words.append(format_name [i+2])
zflag = 0
else:
zflag += 1
if change_words[-1] not in (format_name[0], format_name[1]):
change_words.append("整")
return ''.join(change_words)
#测试
# print changenum(865324.98)
#查看输出的结果是否正确
来源:https://blog.csdn.net/haeasringnar/article/details/79523836
标签:python,人民币,大写
0
投稿
猜你喜欢
NCCL深度学习Bootstrap网络连接建立源码解析
2022-02-25 22:28:10
js实现浏览器倒计时跳转页面效果
2023-07-18 06:49:57
Perl学习笔记之文件操作
2022-04-01 21:48:18
mysql 之通过配置文件链接数据库
2024-01-17 13:32:28
SQL Server分页方法汇总
2024-01-26 20:18:49
详解SQL Server数据库架构和对象、定义数据完整性
2024-01-23 06:48:16
ASP在线转flv+缩略图
2007-08-27 16:18:00
python 全角半角互换的实现示例
2021-02-03 20:30:52
mysql数据库下损坏数据的恢复操作其过程总结
2009-02-13 13:36:00
Python3使用Matplotlib 绘制精美的数学函数图形
2021-09-21 17:38:45
简单了解python 生成器 列表推导式 生成器表达式
2023-12-05 09:21:02
python实现的简单FTP上传下载文件实例
2022-12-29 13:17:42
python实现图像高斯金字塔的示例代码
2023-05-06 02:02:32
Python编程实现简易的音乐播放器基本操作
2022-05-25 08:41:16
python光学仿真面向对象光学元件类的实现
2022-10-11 19:45:06
mysql 5.7.20常用下载、安装和配置方法及简单操作技巧(解压版免安装)
2024-01-15 17:21:24
在Mac中配置Python虚拟环境过程解析
2023-11-14 06:55:43
Python中处理时间的几种方法小结
2021-03-15 17:58:33
python网络编程 使用UDP、TCP协议收发信息详解
2021-02-15 14:37:41
Go语言开发框架反射机制及常见函数示例详解
2024-02-16 17:23:19