Python实现把数字转换成中文


Posted in Python onJune 29, 2015

周末在家,写了个小程序,用于将阿拉伯数字转换化大写中文。程序没经过任何优化,出没经过详细的测试,挂到网上,方便将来有需要的时候直接拿来用。

#!/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 相关文章推荐
深入理解Python 代码优化详解
Oct 27 Python
Python模拟登录验证码(代码简单)
Feb 06 Python
详谈python3中用for循环删除列表中元素的坑
Apr 19 Python
python实现linux下抓包并存库功能
Jul 18 Python
实例详解Matlab 与 Python 的区别
Apr 26 Python
python实现两个dict合并与计算操作示例
Jul 01 Python
tensorflow没有output结点,存储成pb文件的例子
Jan 04 Python
Django values()和value_list()的使用
Mar 31 Python
Python使用jpype模块调用jar包过程解析
Jul 29 Python
详解使用Python写一个向数据库填充数据的小工具(推荐)
Sep 11 Python
Python机器学习应用之基于线性判别模型的分类篇详解
Jan 18 Python
Python序列化模块JSON与Pickle
Jun 05 Python
Python中if __name__ == '__main__'作用解析
Jun 29 #Python
django接入新浪微博OAuth的方法
Jun 29 #Python
python链接Oracle数据库的方法
Jun 28 #Python
python写日志封装类实例
Jun 28 #Python
Python实现的简单hangman游戏实例
Jun 28 #Python
python实现矩阵乘法的方法
Jun 28 #Python
python实现的用于搜索文件并进行内容替换的类实例
Jun 28 #Python
You might like
php adodb连接不同数据库
2009/03/19 PHP
深入PHP empty(),isset(),is_null()的实例测试详解
2013/06/06 PHP
memcache命令启动参数中文解释
2014/01/13 PHP
php通过递归方式复制目录和子目录的方法
2015/03/13 PHP
Yii实现自动加载类地图的方法
2015/04/01 PHP
php面向对象重点知识分享
2019/09/27 PHP
Laravel Eloquent分表方法并使用模型关联的实现
2019/11/25 PHP
基于jQuery替换table中的内容并显示进度条的代码
2011/08/02 Javascript
jQuery中index()的用法分析
2014/09/05 Javascript
AngularJS的内置过滤器详解
2015/05/14 Javascript
jquery简单实现外部链接用新窗口打开的方法
2015/05/30 Javascript
jQuery简单实现input文本框内灰色提示文本效果的方法
2015/12/02 Javascript
JavaScript实现清空(重置)文件类型INPUT元素值的方法
2016/11/17 Javascript
JS实现点击链接切换显示隐藏内容的方法
2017/10/19 Javascript
JavaScript如何判断input数据类型
2020/02/06 Javascript
vue实现短信验证码输入框
2020/04/17 Javascript
Vue中通过属性绑定为元素绑定style行内样式的实例代码
2020/04/30 Javascript
[03:20]次级联赛厮杀超职业 现超级兵对拆世纪大战
2014/10/30 DOTA
Python sys.path详细介绍
2013/10/17 Python
python通过pil将图片转换成黑白效果的方法
2015/03/16 Python
浅谈python 线程池threadpool之实现
2017/11/17 Python
Python实现七彩蟒蛇绘制实例代码
2018/01/16 Python
Python爬虫框架scrapy实现downloader_middleware设置proxy代理功能示例
2018/08/04 Python
详解Django中CBV(Class Base Views)模型源码分析
2019/02/25 Python
Django框架使用mysql视图操作示例
2019/05/15 Python
纽约JewelryAffairs珠宝店:精细金银时尚首饰
2017/02/05 全球购物
美国杰西潘尼官网:JCPenney
2019/06/12 全球购物
乡镇网格化管理实施方案
2014/03/23 职场文书
共产党员公开承诺书范文
2014/03/28 职场文书
员工试用期自我评价
2014/09/18 职场文书
个人欠款协议书范本2014
2014/11/02 职场文书
大一新生军训新闻稿
2015/07/17 职场文书
班级管理经验交流材料
2015/11/02 职场文书
详解MySQL中的主键与事务
2021/05/27 MySQL
如何使用Tkinter进行窗口的管理与设置
2021/06/30 Python
python处理json数据文件
2022/04/11 Python