python中将阿拉伯数字转换成中文的实现代码


Posted in Python onMay 19, 2011
#!/usr/bin/python 
#-*- encoding: utf-8 -*- 
import types 
class NotIntegerError(Exception): 
pass 
class OutOfRangeError(Exception): 
pass 
_MAPPING = (u'零', u'一', u'二', u'三', u'四', u'五', u'六', u'七', u'八', u'九', ) 
_P0 = (u'', u'十', u'百', u'千', ) 
_S4, _S8, _S16 = 10 ** 4 , 10 ** 8, 10 ** 16 
_MIN, _MAX = 0, 9999999999999999 
def _to_chinese4(num): 
'''''转换[0, 10000)之间的阿拉伯数字 
''' 
assert(0 <= num and num < _S4) 
if num < 10: 
return _MAPPING[num] 
else: 
lst = [ ] 
while num >= 10: 
lst.append(num % 10) 
num = num / 10 
lst.append(num) 
c = len(lst) # 位数 
result = u'' 
for idx, val in enumerate(lst): 
if val != 0: 
result += _P0[idx] + _MAPPING[val] 
if idx < c - 1 and lst[idx + 1] == 0: 
result += u'零' 
return result[::-1].replace(u'一十', u'十') 
def _to_chinese8(num): 
assert(num < _S8) 
to4 = _to_chinese4 
if num < _S4: 
return to4(num) 
else: 
mod = _S4 
high, low = num / mod, num % mod 
if low == 0: 
return to4(high) + u'万' 
else: 
if low < _S4 / 10: 
return to4(high) + u'万零' + to4(low) 
else: 
return to4(high) + u'万' + to4(low) 
def _to_chinese16(num): 
assert(num < _S16) 
to8 = _to_chinese8 
mod = _S8 
high, low = num / mod, num % mod 
if low == 0: 
return to8(high) + u'亿' 
else: 
if low < _S8 / 10: 
return to8(high) + u'亿零' + to8(low) 
else: 
return to8(high) + u'亿' + to8(low) 
def to_chinese(num): 
if type(num) != types.IntType and type(num) != types.LongType: 
raise NotIntegerError(u'%s is not a integer.' % num) 
if num < _MIN or num > _MAX: 
raise OutOfRangeError(u'%d out of range[%d, %d)' % (num, _MIN, _MAX)) 
if num < _S4: 
return _to_chinese4(num) 
elif num < _S8: 
return _to_chinese8(num) 
else: 
return _to_chinese16(num) 
if __name__ == '__main__': 
print to_chinese(9000)

把金额小写转换成大写的Python代码
功能将小于十万亿元的小写金额转换为大写
代码
def IIf( b, s1, s2): 

if b: 


return s1 

else: 


return s2 
def num2chn(nin=None): 


cs = 
('零','壹','贰','叁','肆','伍','陆','柒','捌','玖','◇','分','角','圆','拾','佰','仟', 
'万','拾','佰','仟','亿','拾','佰','仟','万') 


st = ''; st1='' 


s = '%0.2f' % (nin)

 


sln =len(s) 


if sln >; 15: return None 


fg = (nin<1) 


for i in range(0, sln-3): 




ns = ord(s[sln-i-4]) - ord('0') 




st=IIf((ns==0)and(fg or (i==8)or(i==4)or(i==0)), '', cs[ns]) 



+ IIf((ns==0)and((i<>;8)and(i<>;4)and(i<>;0)or fg 
and(i==0)),'', cs[i+13]) 



+ st 




fg = (ns==0) 


fg = False 


for i in [1,2]: 




ns = ord(s[sln-i]) - ord('0') 




st1 = IIf((ns==0)and((i==1)or(i==2)and(fg or (nin<1))), '', cs[ns]) 



 + IIf((ns>;0), cs[i+10], IIf((i==2) or fg, '', '整')) 



 + st1 




fg = (ns==0) 


st.replace('亿万','万') 


return IIf( nin==0, '零', st + st1) 
if __name__ == '__main__': 

num = 12340.1 

print num 

print num2chn(num)
Python 相关文章推荐
zbar解码二维码和条形码示例
Feb 07 Python
关于你不想知道的所有Python3 unicode特性
Nov 28 Python
探究数组排序提升Python程序的循环的运行效率的原因
Apr 01 Python
Python Sql数据库增删改查操作简单封装
Apr 18 Python
windows下安装Python和pip终极图文教程
Mar 05 Python
浅谈python中列表、字符串、字典的常用操作
Sep 19 Python
Python3多线程爬虫实例讲解代码
Jan 05 Python
Django中使用Whoosh进行全文检索的方法
Mar 31 Python
基于python实现自动化办公学习笔记(CSV、word、Excel、PPT)
Aug 06 Python
python GUI库图形界面开发之PyQt5表单布局控件QFormLayout详细使用方法与实例
Mar 06 Python
python缺失值的解决方法总结
Jun 09 Python
Python中OpenCV实现简单车牌字符切割
Jun 11 Python
python访问纯真IP数据库的代码
May 19 #Python
Python模块学习 re 正则表达式
May 19 #Python
PYTHON正则表达式 re模块使用说明
May 19 #Python
python 随机数生成的代码的详细分析
May 15 #Python
python 生成不重复的随机数的代码
May 15 #Python
精确查找PHP WEBSHELL木马的方法(1)
Apr 12 #Python
Python中删除文件的程序代码
Mar 13 #Python
You might like
php实现基于微信公众平台开发SDK(demo)扩展的方法
2014/12/22 PHP
ThinkPHP中使用Ueditor富文本编辑器
2015/09/02 PHP
深入了解PHP中的Array数组和foreach
2016/11/06 PHP
YII框架中搜索分页jQuery写法详解
2016/12/19 PHP
PHP连接SQL Server的方法分析【基于thinkPHP5.1框架】
2019/05/06 PHP
用javascript实现计算两个日期的间隔天数
2007/08/14 Javascript
根据鼠标的位置动态的控制层的位置
2009/11/24 Javascript
JS面向对象编程浅析
2011/08/28 Javascript
jquery创建一个新的节点对象(自定义结构/内容)的好方法
2013/01/21 Javascript
实现51Map地图接口(示例代码)
2013/11/22 Javascript
js怎么判断flash swf文件是否加载完毕
2014/08/14 Javascript
javascript学习笔记(四)function函数部分
2014/09/30 Javascript
jQuery表单域选择器用法分析
2015/02/10 Javascript
javascript实现列表切换效果
2016/05/02 Javascript
总结在前端排序中遇到的问题
2016/07/19 Javascript
Bootstrap3下拉菜单的实现
2017/02/22 Javascript
vuejs 单文件组件.vue 文件的使用
2017/07/28 Javascript
基于JavaScript实现带数据验证和复选框的表单提交
2017/08/23 Javascript
React Native中NavigatorIOS组件的简单使用详解
2018/01/27 Javascript
基于layui table返回的值的多级嵌套的解决方法
2019/09/19 Javascript
layui字体图标 loading图标静止不旋转的解决方法
2019/09/23 Javascript
JS async 函数的含义和用法实例总结
2020/04/08 Javascript
详解python基础之while循环及if判断
2017/08/24 Python
python中数据爬虫requests库使用方法详解
2018/02/11 Python
深入浅析Python的类
2018/06/22 Python
解决python3中的requests解析中文页面出现乱码问题
2019/04/19 Python
pymysql 开启调试模式的实现
2019/09/24 Python
中国网上药店领导者:1药网
2017/02/16 全球购物
Quiksilver美国官网:始于1969年的优质冲浪服和滑雪板外套
2020/04/20 全球购物
MIS软件工程师的面试题
2016/04/22 面试题
静态变量和实例变量的区别
2015/07/07 面试题
小学英语教师先进事迹
2014/05/28 职场文书
银行客户经理岗位职责
2015/04/09 职场文书
护理培训心得体会
2016/01/22 职场文书
Pandas自定义选项option设置
2021/07/25 Python
AJAX实现指定部分页面刷新效果
2021/10/16 Javascript