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使用MySQLdb for Python操作数据库教程
Oct 11 Python
Python采用Django制作简易的知乎日报API
Aug 03 Python
Python中矩阵库Numpy基本操作详解
Nov 21 Python
Python基于whois模块简单识别网站域名及所有者的方法
Apr 23 Python
Python 修改列表中的元素方法
Jun 26 Python
Python Pandas批量读取csv文件到dataframe的方法
Oct 08 Python
python+ffmpeg批量去视频开头的方法
Jan 09 Python
对python字典过滤条件的实例详解
Jan 22 Python
Python元组常见操作示例
Feb 19 Python
基于python判断目录或者文件代码实例
Nov 29 Python
django为Form生成的label标签添加class方式
May 20 Python
在python3.9下如何安装scrapy的方法
Feb 03 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
基于mysql的论坛(5)
2006/10/09 PHP
php中Smarty模板初体验
2011/08/08 PHP
如何让thinkphp在模型中自动完成session赋值小教程
2014/09/05 PHP
php禁止直接从浏览器输入地址访问.php文件的方法
2014/11/04 PHP
Zend Framework数据库操作技巧总结
2017/02/18 PHP
PHP按一定比例压缩图片的方法
2018/10/12 PHP
在JavaScript中通过URL传递汉字的方法
2007/04/09 Javascript
JQuery对checkbox操作 (循环获取)
2011/05/20 Javascript
一个页面元素appendchild追加到另一个页面元素的问题
2013/01/27 Javascript
uploadify在Firefox下丢失session问题的解决方法
2013/08/07 Javascript
用js实现in_array的方法
2013/11/05 Javascript
js获取会话框prompt的返回值的方法
2015/01/10 Javascript
jsp 网站引入外部css或者js失效问题解决
2016/10/31 Javascript
iview给radio按钮组件加点击事件的实例
2017/09/30 Javascript
JavaScript门道之标准库
2018/05/26 Javascript
解决layui中table异步数据请求不支持自定义返回数据格式的问题
2018/08/19 Javascript
Vuepress 搭建带评论功能的静态博客的实现
2019/02/17 Javascript
使用Python的Tornado框架实现一个一对一聊天的程序
2015/04/25 Python
python爬虫 爬取超清壁纸代码实例
2019/08/16 Python
基于TensorBoard中graph模块图结构分析
2020/02/15 Python
耐克波兰官方网站:Nike波兰
2019/09/03 全球购物
局域网定义和特性
2016/01/23 面试题
什么时候需要进行强制类型转换
2016/09/03 面试题
给实习单位的感谢信
2014/02/01 职场文书
个人近期表现材料
2014/02/11 职场文书
任命书格式
2014/06/05 职场文书
新闻专业毕业生求职信
2014/08/08 职场文书
社区志愿者活动方案
2014/08/18 职场文书
学校工会工作总结2015
2015/05/19 职场文书
高考升学宴主持词
2019/06/21 职场文书
导游词之黄帝陵景区
2019/09/16 职场文书
Nginx访问日志及错误日志参数说明
2021/03/31 Servers
Python GUI编程之tkinter 关于 ttkbootstrap 的使用详解
2022/03/03 Python
2022微信温控新功能上线
2022/05/09 数码科技
baselines示例程序train_cartpole.py的ImportError
2022/05/20 Python
CSS浮动引起的高度塌陷问题
2022/08/05 HTML / CSS