python实现人民币大写转换

yipeiwu_com6年前Python基础

本文实例为大家分享了python实现人民币大写转换的具体代码,供大家参考,具体内容如下

#!/usr/bin/python
# -*- coding:utf-8 -*-

# ********* 转换方法介绍 *********
# 将需要转换的数字从右向左,每4位分成一个section,如:24530467103,将该数字拆分后,得到:
# 245 3046 7103 (245亿3046万7103)
# 对拆分后的数字先按照section进行数字到汉字的转换,然后添加数值单位,如:仟,佰,拾,处理结束后可以得到转换后的序列。
# 对section处理结束后,再对每个section进行单位的追加。如:兆、亿、万。
# 这里需要注意一些特殊情况,如:section中连续出现0,最后一个数字为0等。

DEBUG = True

upper = ["零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"]
decimal_unit = ["角", "分", "厘", "毫"]
section_unit = ["万", "亿", "兆"]
count_unit = ["拾", "佰", "仟"]

def dbg_print(s):
  if DEBUG:
    print(s)

def split_num(num):
  num_list = []
  if (len(num) <= 4):
    num_list.append(num)
    return num_list
  while (len(num)):
    if (len(num) <= 4):
      num_list.append(num)
      num_list.reverse()
      return num_list
    sec = num[-4:]
    num_list.append(sec)
    num = num[:-4]

# 处理小数部分,只支持4位,多于4位,四舍五入。
def convert_dec(num):
  result = ""
  count = 0
  dbg_print(num)
  for i in num:
    n = int(i)
    if (0 != n):
      result += upper[n]
      result += decimal_unit[count]
    count += 1
  dbg_print(result)
  return result

# 处理整数部分
def convert_int(num):
  section_list = split_num(num)
  dbg_print(num)
  dbg_print(section_list)
  result = ""
  sec_index = len(section_list) - 2
  for item in section_list:
    index = len(item) - 2
    # 统计连续出现的数字0的个数。
    flag = 0
    # 计算遍历过的item中的字符数。 
    count = 0
    # 对每个section进行处理,得到数字对应的汉字。
    for i in item:
      n = int(i)
      if (0 == n):
        flag += 1
      else:
        flag = 0
      # 用来区分section的最后一位为0的情况
      if (count != len(item)-1):
        # 该位置的数字为0,并且它的下一个数字非0。
        if ((flag >= 1) and ('0' != item[count+1])):
          result += upper[n]
        else (0 != n):
          result += upper[n]
      else:
        # section的最后一个数字非0的情况。
        if (0 != n):
          result += upper[n]
      # 最后一个数字以及数字为0时,都不需要添加单位。
      if ((index >= 0) and (0 != n)):
        result += count_unit[index]
      index += 1
      count += 1
    从第1个section开始,如果section中的数字不全为0,其后就需要添加section对应的单位。
    if (sec_index >= 0 and flag != count):
      result += section_unit[sec_index]
    dbg_print(result)
    sec_index -= 1
  result = result.replace("壹拾", "拾")
  result += "元"
  return result

# 转换函数
def convert(num):
  result = ""
  num = round(float(num), 4)
  integer,decimal = str(num).split('.')
  result_int = convert_int(integer)
  result_dec = convert_dec(decimal)

  if (len(result_dec) == 0):
    result = result_int += "整"
  else:
    result = result_int + result_dec
  return result

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

浅谈Python中的可变对象和不可变对象

什么是可变/不可变对象 不可变对象,该对象所指向的内存中的值不能被改变。当改变某个变量时候,由于其所指的值不能被改变,相当于把原来的值复制一份后再改变,这会开辟一个新的地址,变量再指向这...

Python自然语言处理 NLTK 库用法入门教程【经典】

本文实例讲述了Python自然语言处理 NLTK 库用法。分享给大家供大家参考,具体如下: 在这篇文章中,我们将基于 Python 讨论自然语言处理(NLP)。本教程将会使用 Pyth...

Django Admin实现上传图片校验功能

Django Admin实现上传图片校验功能

 Django 为未来的开发人员提供了许多功能:一个成熟的标准库,一个活跃的用户社区,以及 Python 语言的所有好处。虽然其他 Web 框架也声称能提供同样的内容,但 Dj...

python全栈要学什么 python全栈学习路线

IT行业,技术要比学历、年龄、从业经验更为重要,技术水平直接决定就业薪资,想要学好python,首先要先了解精通Python语言基础、Python web开发、Python爬虫、Pyth...

分析经典Python开发工程师面试题

你知道吗?实际上Python早在20世纪90年代初就已经诞生,可是火爆时间却并不长,就小编本人来说,也是前几年才了解到它。据统计,目前Python开发人员的薪资待遇为10K以上,这样的诱...