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利用MethodType绑定方法到类示例代码
Aug 27 Python
Python与R语言的简要对比
Nov 14 Python
python实现媒体播放器功能
Feb 11 Python
python 实现判断ip连通性的方法总结
Apr 22 Python
OpenCV HSV颜色识别及HSV基本颜色分量范围
Mar 22 Python
图文详解Django使用Pycharm连接MySQL数据库
Aug 09 Python
如何理解python面向对象编程
Jun 01 Python
Python 存取npy格式数据实例
Jul 01 Python
5款实用的python 工具推荐
Oct 13 Python
GitHub上值得推荐的8个python 项目
Oct 30 Python
Django中使用Celery的方法步骤
Dec 07 Python
python使用openpyxl库读写Excel表格的方法(增删改查操作)
May 02 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实现对xml的增删改查操作案例分析
2017/05/19 PHP
解决php扩展安装不生效问题
2019/10/25 PHP
纯js实现的论坛常用的运行代码的效果
2008/07/15 Javascript
javascript面向对象编程(一) 实例代码
2010/06/25 Javascript
JavaScript中清空数组的三种方法分享
2011/04/07 Javascript
JavaScript实现动态创建CSS样式规则方案
2014/09/06 Javascript
JS实现的论坛Ajax打分效果完整实例
2015/10/31 Javascript
浏览器环境下JavaScript脚本加载与执行探析之动态脚本与Ajax脚本注入
2016/01/19 Javascript
JavaScript数组实现数据结构中的队列与堆栈
2016/05/26 Javascript
详解堆的javascript实现方法
2016/11/29 Javascript
vue2 如何实现div contenteditable=“true”(类似于v-model)的效果
2017/02/08 Javascript
Angularjs的键盘事件的绑定
2017/07/27 Javascript
详解用Node.js实现Restful风格webservice
2017/09/29 Javascript
bootstrap select2插件用ajax来获取和显示数据的实例
2018/08/09 Javascript
在Chrome DevTools中调试JavaScript的实现
2020/04/07 Javascript
Nuxt.js的路由跳转操作(页面跳转nuxt-link)
2020/11/06 Javascript
[05:40]DOTA2荣耀之路6:Wings最后进攻
2018/05/30 DOTA
[01:06]DOTA2小知识课堂 Ep.02 吹风竟可解梦境缠绕
2019/12/05 DOTA
Python语法快速入门指南
2015/10/12 Python
简单学习Python time模块
2016/04/29 Python
Python中的字符串替换操作示例
2016/06/27 Python
Python冒泡排序注意要点实例详解
2016/09/09 Python
简单谈谈Python中的json与pickle
2017/07/19 Python
JPype实现在python中调用JAVA的实例
2017/07/19 Python
numpy.linspace 生成等差数组的方法
2018/07/02 Python
使用Django2快速开发Web项目的详细步骤
2019/01/06 Python
Python变量访问权限控制详解
2019/06/29 Python
HTML5有哪些新特征
2015/12/01 HTML / CSS
电子商务专业个人的自我评价
2013/11/19 职场文书
应用数学自荐书范文
2013/11/24 职场文书
运动会开幕式主持词
2014/03/28 职场文书
营销经理工作检讨书
2014/11/03 职场文书
婚育证明格式
2015/06/17 职场文书
党风廉政建设心得体会
2019/05/21 职场文书
Mysql基础知识点汇总
2021/05/26 MySQL
tomcat默认最大连接数及相关调整方法
2022/05/06 Servers