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 28 Python
修改Python的pyxmpp2中的主循环使其提高性能
Apr 24 Python
python寻找list中最大值、最小值并返回其所在位置的方法
Jun 27 Python
python 列表降维的实例讲解
Jun 28 Python
python实现键盘控制鼠标移动
Nov 27 Python
python射线法判断一个点在图形区域内外
Jun 28 Python
Django通用类视图实现忘记密码重置密码功能示例
Dec 17 Python
对tensorflow中cifar-10文档的Read操作详解
Feb 10 Python
Java Spring项目国际化(i18n)详细方法与实例
Mar 20 Python
Python爬虫之Selenium设置元素等待的方法
Dec 04 Python
理解深度学习之深度学习简介
Apr 14 Python
Django+Celery实现定时任务的示例
Jun 23 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
Views rows style模板重写代码
2011/05/16 PHP
php用正则判断是否为数字的方法
2016/03/25 PHP
thinkPHP5框架接口写法简单示例
2019/08/05 PHP
php5对象复制、clone、浅复制与深复制实例详解
2019/08/14 PHP
HR vs CL BO3 第一场 2.13
2021/03/10 DOTA
网页中实现浏览器的最大,最小化和关闭按钮
2007/03/12 Javascript
工作中常用到的JS表单验证代码(包括例子)
2010/11/11 Javascript
js遍历td tr等html元素
2012/12/13 Javascript
利用js实现前台动态添加文本框,后台获取文本框内容(示例代码)
2013/11/25 Javascript
jquery中常用的函数和属性详细解析
2014/03/07 Javascript
jQuery Uploadify 上传插件出现Http Error 302 错误的解决办法
2015/12/12 Javascript
AngularJs验证重复密码的方法(两种)
2016/11/25 Javascript
JS实现焦点图轮播效果的方法详解
2016/12/19 Javascript
详解刷新页面vuex数据不消失和不跳转页面的解决
2018/01/30 Javascript
基于layui轮播图满屏是高度自适应的解决方法
2019/09/16 Javascript
vue 实现LED数字时钟效果(开箱即用)
2019/12/08 Javascript
Vue+elementUI实现多图片上传与回显功能(含回显后继续上传或删除)
2020/03/23 Javascript
vue中的.$mount('#app')手动挂载操作
2020/09/02 Javascript
vue3.0 加载json的方法(非ajax)
2020/10/26 Javascript
antd design table更改某行数据的样式操作
2020/10/31 Javascript
微信小程序开发数据缓存基础知识辨析及运用实例详解
2020/11/06 Javascript
Python使用filetype精确判断文件类型
2017/07/02 Python
python中利用队列asyncio.Queue进行通讯详解
2017/09/10 Python
python+selenium实现自动化百度搜索关键词
2019/06/03 Python
详解python pandas 分组统计的方法
2019/07/30 Python
Python文件操作基础流程解析
2020/03/19 Python
基于css3仿造window7的开始菜单
2010/06/17 HTML / CSS
测绘工程系学生的自我评价
2013/11/30 职场文书
优秀的茶餐厅创业计划书
2014/01/03 职场文书
《大江保卫战》教学反思
2014/04/11 职场文书
实习生工作证明范本
2014/09/14 职场文书
入党积极分子个人总结
2015/03/02 职场文书
导游词之西安骊山
2019/12/20 职场文书
Nginx已编译的nginx-添加新模块
2021/04/01 Servers
Python文件的操作示例的详细讲解
2021/04/08 Python
SQL SERVER中的流程控制语句
2022/05/25 SQL Server