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下的subprocess模块的入门指引
Apr 16 Python
Python教程之全局变量用法
Jun 27 Python
关于Python面向对象编程的知识点总结
Feb 14 Python
Ubuntu安装Jupyter Notebook教程
Oct 18 Python
PyQt5每天必学之切换按钮
Aug 20 Python
使用python实现mqtt的发布和订阅
May 05 Python
python定义类self用法实例解析
Jan 22 Python
Python自动化办公Excel模块openpyxl原理及用法解析
Nov 05 Python
python爬取抖音视频的实例分析
Jan 19 Python
python Polars库的使用简介
Apr 21 Python
python numpy中multiply与*及matul 的区别说明
May 26 Python
Django基础CBV装饰器和中间件
Mar 22 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源代码
2009/08/21 PHP
浅析Yii2 GridView 日期格式化并实现日期可搜索教程
2016/04/22 PHP
PHP连接MYSQL数据库的3种常用方法
2017/02/27 PHP
datagrid框架的删除添加与修改
2013/04/08 Javascript
node.js中的fs.readdirSync方法使用说明
2014/12/17 Javascript
easyui取消表单实时验证,提交时统一验证的简单实例
2016/11/07 Javascript
JavaScript实现网页头部进度条刷新
2017/04/16 Javascript
JavaScript对JSON数据进行排序和搜索
2017/07/24 Javascript
基于js中this和event 的区别(详解)
2017/10/24 Javascript
使用vue-cli(vue脚手架)快速搭建项目的方法
2018/05/21 Javascript
玩转Koa之koa-router原理解析
2018/12/29 Javascript
Vuex持久化插件(vuex-persistedstate)解决刷新数据消失的问题
2019/04/16 Javascript
JQuery复选框全选效果如何实现
2020/05/08 jQuery
微信小程序实现多图上传
2020/06/19 Javascript
利用Vue的v-for和v-bind实现列表颜色切换
2020/07/17 Javascript
vue项目如何监听localStorage或sessionStorage的变化
2021/01/04 Vue.js
ES6的循环与可迭代对象示例详解
2021/01/31 Javascript
python简单实现计算过期时间的方法
2015/06/09 Python
Python的requests网络编程包使用教程
2016/07/11 Python
Python3.x爬虫下载网页图片的实例讲解
2018/05/22 Python
Pytorch通过保存为ONNX模型转TensorRT5的实现
2020/05/25 Python
解决pytorch下出现multi-target not supported at的一种可能原因
2021/02/06 Python
CSS3动画之利用requestAnimationFrame触发重新播放功能
2019/09/11 HTML / CSS
法国隐形眼镜网站:VisionDirect.fr
2020/03/03 全球购物
美国最大的烧烤架和户外生活用品专业零售商:Barbeques Galore
2021/01/09 全球购物
估算杭州有多少软件工程师
2015/08/11 面试题
Linux内核的同步机制是什么?主要有哪几种内核锁
2013/01/03 面试题
中学实习教师自我鉴定
2013/12/12 职场文书
《我要的是葫芦》教学反思
2014/02/23 职场文书
教育技术职业规划范文
2014/03/04 职场文书
差生评语大全
2014/05/04 职场文书
2014年人力资源部工作总结
2014/11/19 职场文书
2015年元旦促销方案书
2014/12/09 职场文书
如何书写公司员工保密协议?
2019/06/27 职场文书
python flask开发的简单基金查询工具
2021/06/02 Python
使用CSS实现黑白格背景效果
2022/06/01 HTML / CSS