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调用浏览器并打开一个网址的例子
Jun 05 Python
Python的string模块中的Template类字符串模板用法
Jun 27 Python
Python实现压缩与解压gzip大文件的方法
Sep 18 Python
python binascii 进制转换实例
Jun 12 Python
python 实现二维列表转置
Dec 02 Python
Python之Django自动实现html代码(下拉框,数据选择)
Mar 13 Python
django项目中新增app的2种实现方法
Apr 01 Python
解决pycharm中的run和debug失效无法点击运行
Jun 09 Python
用opencv给图片换背景色的示例代码
Jul 08 Python
Python3如何使用多线程升程序运行速度
Aug 11 Python
Python经纬度坐标转换为距离及角度的实现
Nov 01 Python
Python趣味挑战之教你用pygame画进度条
May 31 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密码生成类实例
2014/09/24 PHP
PHP面向对象编程之深入理解方法重载与方法覆盖(多态)
2015/12/24 PHP
PHP中Trait及其应用详解
2017/02/14 PHP
Tinymce+jQuery.Validation使用产生的BUG
2010/03/29 Javascript
js实时监听文本框状态的方法
2011/04/26 Javascript
jQuery extend 的简单实例
2013/09/18 Javascript
Node.js中AES加密和其它语言不一致问题解决办法
2014/03/10 Javascript
一个检测表单数据的JavaScript实例
2014/10/31 Javascript
jQuery使用$.ajax进行即时验证实例详解
2015/12/11 Javascript
使用JQuery中的trim()方法去掉前后空格
2016/09/16 Javascript
Vue 2.0的数据依赖实现原理代码简析
2017/07/10 Javascript
微信小程序如何获取用户信息
2018/01/26 Javascript
JS改变页面颜色源码分享
2018/02/24 Javascript
Vue 让元素抖动/摆动起来的实现代码
2018/05/31 Javascript
JS实现面向对象继承的5种方式分析
2018/07/21 Javascript
微信小程序基于canvas渐变实现的彩虹效果示例
2019/05/03 Javascript
vue实现网络图片瀑布流 + 下拉刷新 + 上拉加载更多(步骤详解)
2020/01/14 Javascript
Python使用random和tertools模块解一些经典概率问题
2015/01/28 Python
Flask数据库迁移简单介绍
2017/10/24 Python
python 的numpy库中的mean()函数用法介绍
2020/03/03 Python
python中的socket实现ftp客户端和服务器收发文件及md5加密文件
2020/04/01 Python
Django多层嵌套ManyToMany字段ORM操作详解
2020/05/19 Python
python 制作python包,封装成可用模块教程
2020/07/13 Python
Django集成MongoDB实现过程解析
2020/12/01 Python
几道Web/Ajax的面试题
2016/11/05 面试题
财政专业求职信范文
2014/02/19 职场文书
财务人员的自我评价范文
2014/03/03 职场文书
空乘英文求职信
2014/04/13 职场文书
小学开学标语
2014/07/01 职场文书
会计学习心得体会
2014/09/09 职场文书
群众路线学习心得体会范文
2014/11/05 职场文书
幼儿园小班个人工作总结
2015/02/12 职场文书
2015年师德师风自我评价范文
2015/03/05 职场文书
汶川大地震感悟
2015/08/10 职场文书
Go 自定义package包设置与导入操作
2021/05/06 Golang
字节飞书面试promise.all实现示例
2022/06/16 Javascript