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 相关文章推荐
Python中用Ctrl+C终止多线程程序的问题解决
Mar 30 Python
python静态方法实例
Jan 14 Python
python编写微信远程控制电脑的程序
Jan 05 Python
记一次python 内存泄漏问题及解决过程
Nov 29 Python
python 对多个csv文件分别进行处理的方法
Jan 07 Python
Django框架视图层URL映射与反向解析实例分析
Jul 29 Python
python3实现mysql导出excel的方法
Jul 31 Python
pytorch GAN伪造手写体mnist数据集方式
Jan 10 Python
pytorch 利用lstm做mnist手写数字识别分类的实例
Jan 10 Python
Python使用shutil模块实现文件拷贝
Jul 31 Python
用python制作个视频下载器
Feb 01 Python
pytorch中的 .view()函数的用法介绍
Mar 17 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的面试题集,附我的答案和分析(一)
2006/11/19 PHP
php self,$this,const,static,-&amp;gt;的使用
2009/10/22 PHP
不支持fsockopen但支持culr环境下下ucenter与modoer通讯问题
2011/08/12 PHP
PHP中的按位与和按位或操作示例
2014/01/27 PHP
PHP实现生成透明背景的PNG缩略图函数分享
2014/07/08 PHP
php将字符串全部转换成大写或者小写的方法
2015/03/17 PHP
PHP编程中的__clone()方法使用详解
2015/11/27 PHP
Mootools 1.2教程 Tooltips
2009/09/15 Javascript
BOM与DOM的区别分析
2010/10/26 Javascript
html中table数据排序的js代码
2011/08/09 Javascript
(跨浏览器基础事件/浏览器检测/判断浏览器)经验代码分享
2013/01/24 Javascript
使用Plupload实现直接上传附件至七牛云存储
2014/12/26 Javascript
理解JavaScript中worker事件api
2015/12/25 Javascript
jQuery中的一些小技巧
2017/01/18 Javascript
JS常见创建类的方法小结【工厂方式,构造器方式,原型方式,联合方式等】
2017/04/01 Javascript
JS实现图片居中悬浮效果
2017/12/25 Javascript
vue二级路由设置方法
2018/02/09 Javascript
基于 jQuery 实现键盘事件监听控件
2019/04/04 jQuery
使用Node.js在深度学习中做图片预处理的方法
2019/09/18 Javascript
[03:30]DOTA2完美“圣”典精彩集锦
2016/12/27 DOTA
python解决字典中的值是列表问题的方法
2013/03/04 Python
在Python中使用sort()方法进行排序的简单教程
2015/05/21 Python
单利模式及python实现方式详解
2018/03/20 Python
用TensorFlow实现戴明回归算法的示例
2018/05/02 Python
flask中过滤器的使用详解
2018/08/01 Python
使用pandas的box_plot去除异常值
2019/12/10 Python
常用的10个Python实用小技巧
2020/08/10 Python
Python通过类的组合模拟街道红绿灯
2020/09/16 Python
CSS3的column-fill属性对齐列内容高度的用法详解
2016/07/01 HTML / CSS
梅西百货澳大利亚:Macy’s Australia
2017/07/26 全球购物
应届大学生的推荐信
2013/11/20 职场文书
倡议书格式模板
2014/05/13 职场文书
实验室的标语
2014/06/20 职场文书
2014年小学生迎国庆65周年演讲稿
2014/09/27 职场文书
文书工作总结(范文)
2019/07/11 职场文书
图神经网络GNN算法
2022/05/11 Python