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 正则式使用心得
May 07 Python
python错误:AttributeError: 'module' object has no attribute 'setdefaultencoding'问题的解决方法
Aug 22 Python
Python中exit、return、sys.exit()等使用实例和区别
May 28 Python
Python爬取网页中的图片(搜狗图片)详解
Mar 23 Python
Python WSGI的深入理解
Aug 01 Python
python实现在图片上画特定大小角度矩形框
Oct 24 Python
Python编程深度学习绘图库之matplotlib
Dec 28 Python
树莓派极简安装OpenCv的方法步骤
Oct 10 Python
Python 用三行代码提取PDF表格数据
Oct 13 Python
Python多线程thread及模块使用实例
Apr 28 Python
python网络爬虫实现发送短信验证码的方法
Feb 25 Python
Python实现Telnet自动连接检测密码的示例
Apr 16 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/01/08 PHP
thinkPHP中_initialize方法实例分析
2016/12/05 PHP
PHPStudy下如何为Apache安装SSL证书的方法步骤
2019/01/23 PHP
PHP中数组转换为SimpleXML教程
2019/01/27 PHP
Javascript调试工具(下载)
2007/01/09 Javascript
bgsound 背景音乐 的一些常用方法及特殊用法小结
2010/05/11 Javascript
jquery入门—编写一个导航条(可伸缩)
2013/01/07 Javascript
Javascript实现简单二级下拉菜单实例
2014/06/15 Javascript
js构造函数、索引数组和属性的实现方式和使用
2014/11/16 Javascript
Javascript学习笔记之函数篇(六) : 作用域与命名空间
2014/11/23 Javascript
JavaScript获取IP获取的是IPV6 如何校验
2016/06/12 Javascript
jQuery查看选中对象HTML代码的方法
2016/06/17 Javascript
Bootstrap三种表单布局的使用方法
2016/06/21 Javascript
ionic cordova一次上传多张图片(类似input file提交表单)的实现方法
2016/12/16 Javascript
JS实现PC手机端和嵌入式滑动拼图验证码三种效果
2017/02/15 Javascript
你有必要知道的10个JavaScript难点
2017/07/25 Javascript
JS轮播图实现简单代码
2021/02/19 Javascript
Angular4 Select选择改变事件的方法
2018/10/09 Javascript
Javascript组合继承方法代码实例解析
2020/04/02 Javascript
ant-design表单处理和常用方法及自定义验证操作
2020/10/27 Javascript
[08:54]《一刀刀一天》之DOTA全时刻18:十九支奔赴西雅图队伍全部出炉
2014/06/04 DOTA
Python内置函数之filter map reduce介绍
2014/11/30 Python
全面了解python字符串和字典
2016/07/07 Python
完美解决在oj中Python的循环输入问题
2018/06/25 Python
python使用selenium实现批量文件下载
2019/03/11 Python
使用python实现男神女神颜值打分系统(推荐)
2019/10/31 Python
Python绘制二维曲线的日常应用详解
2019/12/04 Python
python 实现检验33品种数据是否是正态分布
2019/12/09 Python
Python获取指定网段正在使用的IP
2020/12/14 Python
百联网上商城:i百联
2017/01/28 全球购物
Gtech官方网站:地毯清洁器、吸尘器及园艺设备
2018/05/23 全球购物
巴西最大的在线约会网站:ParPerfeito
2018/07/11 全球购物
Yves Rocher伊夫·黎雪美国官网:法国始创植物美肌1959
2019/01/09 全球购物
银行求职自荐书
2014/06/25 职场文书
国际贸易本科毕业生求职信
2014/09/26 职场文书
员工聘用合同范本
2015/09/21 职场文书