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使用PIL缩放网络图片并保存的方法
Apr 24 Python
python数据结构之图的实现方法
Jul 08 Python
python 时间戳与格式化时间的转化实现代码
Mar 23 Python
Python基于回溯法子集树模板实现8皇后问题
Sep 01 Python
详解Python中如何写控制台进度条的整理
Mar 07 Python
解决pip install的时候报错timed out的问题
Jun 12 Python
dataframe 按条件替换某一列中的值方法
Jan 29 Python
分享一个pycharm专业版安装的永久使用方法
Sep 24 Python
浅谈python输出列表元素的所有排列形式
Feb 26 Python
基于Python爬取爱奇艺资源过程解析
Mar 02 Python
Python matplotlib模块及柱状图用法解析
Aug 10 Python
Python Pygame实战之塔防游戏的实现
Mar 17 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/12/25 PHP
一段利用WSH获取登录时间的jscript代码
2008/05/11 Javascript
用js统计用户下载网页所需时间的脚本
2008/10/15 Javascript
jQuery 源代码显示控件 (Ajax加载方式).
2009/05/18 Javascript
关于javascript模块加载技术的一些思考
2014/11/28 Javascript
js HTML5多图片上传及预览实例解析(不含前端的文件分割)
2016/08/26 Javascript
Bootstrap基本插件学习笔记之Tooltip提示工具(18)
2016/12/08 Javascript
jquery mobile移动端幻灯片滑动切换效果
2020/04/15 Javascript
基于Vue2.0+ElementUI实现表格翻页功能
2017/10/23 Javascript
vue interceptor 使用教程实例详解
2018/09/13 Javascript
TypeScript基础入门教程之三重斜线指令详解
2018/10/22 Javascript
关于layui 实现点击按钮添加一行(方法渲染创建的table)
2019/09/29 Javascript
jquery+css3实现的经典弹出层效果示例
2020/05/16 jQuery
Jquery 获取相同NAME 或者id删除行操作
2020/08/24 jQuery
[01:41]DOTA2超级联赛专访YYF 称一辈子难忘TI2
2013/05/28 DOTA
[01:46]DOTA2上海特锦赛小组赛英文解说KotlGuy采访
2016/02/27 DOTA
在Python程序中进行文件读取和写入操作的教程
2015/04/28 Python
详解django中url路由配置及渲染方式
2019/02/25 Python
浅析Python数字类型和字符串类型的内置方法
2019/12/22 Python
pytorch使用tensorboardX进行loss可视化实例
2020/02/24 Python
python烟花效果的代码实例
2020/02/25 Python
Python数据结构dict常用操作代码实例
2020/03/12 Python
安装多个版本的TensorFlow的方法步骤
2020/04/21 Python
玩转CSS3色彩
2010/01/16 HTML / CSS
CSS3+HTML5+JS 实现一个块的收缩与展开动画效果
2020/11/17 HTML / CSS
波兰家居和花园家具专家:4Home
2019/05/26 全球购物
Chain Reaction Cycles俄罗斯:世界上最大的在线自行车商店
2019/08/27 全球购物
手工制作的意大利皮革运动鞋:KOIO
2020/01/05 全球购物
四川internet信息高速公路(C#)笔试题
2012/02/29 面试题
介绍一下linux的文件权限
2012/02/15 面试题
大学生毕业鉴定
2014/01/31 职场文书
财务部经理岗位职责
2014/02/03 职场文书
汉语言文学毕业生自荐信范文
2014/03/24 职场文书
2014年社团工作总结范文
2014/11/27 职场文书
承诺书的内容有哪些,怎么写?
2019/06/21 职场文书
演讲开头怎么书写?
2019/08/06 职场文书