Python TIPS上一道关于人民币金额小写转大写的题

人民币金额打印

题目链接:here。我发现我写的好复杂,但万幸编码还算符合人类,看了其他答案,感觉都是天书。

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Date    : 2017-06-09
# @Author  : Bob Liao ([email protected])
# @Link    : https://github.com/coderchaser
# @Version : python3.4

#创建大小写转换字典
num_dic={
    # 1 : "壹",
    # 2 : "贰",
    # 3 : "叁",
    # 4 : "肆",
    # 5 : "伍",
    # 6 : "陆",
    # 7 : "柒",
    # 8 : "捌",
    # 9 : "玖",
    # 0 : "零"
    u"1": u"壹",
    u"2": u"贰",
    u"3": u"叁",
    u"4": u"肆",
    u"5": u"伍",
    u"6": u"陆",
    u"7": u"柒",
    u"8": u"捌",
    u"9": u"玖",
    u"0": u"零"
}
#将输入金额分组,从最低位开始四位分组,最后一组可以少于四位
def group(num_str) :
    group_list=[]
    i=len(num_str)
    while i>=4 :
        group_list.append(num_str[i-4:i])
        i-=4
    if i!=0 :
        group_list.append(num_str[:i])
    #保证顺序输出
    group_list.reverse()
    return group_list
#原始读函数,数字必须全部转化为字符串
def read_four(num_four) :
    #读四位分组
    gewei=""
    shiwei=""
    baiwei=""
    qianwei=""
    num_len=len(num_four)
    #当传入的数字为0000时num_len会变成1,会发生list越界
    if num_four[-1]!=u'0' :
        if num_four[-2]!=u'0' :
            gewei=num_dic[num_four[-1]]
        else :
            gewei=u"零"+num_dic[num_four[-1]]
    if num_four[-2]!=u'0' :
        if num_four[-3]!=u'0' :
            shiwei=num_dic[num_four[-2]]+u"拾"
        else :
            shiwei=u"零"+num_dic[num_four[-2]]+u"拾"
    if num_four[-3]!=u'0' :
        if num_four[-4]!=u'0' :
            baiwei=num_dic[num_four[-3]]+u"佰"
        else :
            baiwei=u"零"+num_dic[num_four[-3]]+u"佰"
    if num_four[-4]!=u'0' :
        qianwei=num_dic[num_four[-4]]+u"仟"
    return qianwei+baiwei+shiwei+gewei
def read_three(num_three) :
    #读三位分组
    gewei=""
    shiwei=""
    baiwei=""
    if num_three[-1]!=u'0' :
        if num_three[-2]!=u'0' :
            gewei=num_dic[num_three[-1]]
        else :
            gewei=u"零"+num_dic[num_three[-1]]
    if num_three[-2]!=u'0' :
        if num_three[-3]!=u'0' :
            shiwei=num_dic[num_three[-2]]+u"拾"
        else :
            shiwei=u"零"+num_dic[num_three[-2]]+u"拾"
    if num_three[-3]!=u'0' :
        baiwei=num_dic[num_three[-3]]+u"佰"
    return baiwei+shiwei+gewei
def read_two(num_two) :
    #读二位分组
    gewei=""
    shiwei=""
    if num_two[-1]!=u'0' :
        if num_two[-2]!=u'0' :
            gewei=num_dic[num_two[-1]]
        else :
            gewei=u"零"+num_dic[num_two[-1]]
    if num_two[-2]!=u'0' :
        if num_two[-2] ==u'1' :
            shiwei=u"拾"
        else :
            shiwei=num_dic[num_two[-2]]+u"拾"
    return shiwei+gewei
def read_one(num_one) :
    #读一位分组
    if num_one!=u'0' :
        return num_dic[num_one]
    else :
        return ""
#建立调用字典关系
call_read={
    1 : read_one,
    2 : read_two,
    3 : read_three,
    4 : read_four
}
def read(num) :
    #将数字int转化为字符串
    #金额不可以像如下形式:
    #       0100
    #Python3会将0开头的数字
    #识别为8进制,一般人不会
    #这样写金额,不予考虑
        if num == 0:
            print("零圆")
            return
    num_str=str(abs(num))
    #分组
    group_list=group(num_str)
    read_part=[]
    if num<0 :
        read_part.append(u'负')
    for i in group_list :
        len_part=len(i)
        read_part.append(call_read[len_part](i))
        if group_list.index(i)== 0 and len(group_list)>1 :
            read_part.append(u'万')
    read_part.append(u'圆')
    print("".join(read_part))

# num=int(input('Your Number: '))
# print(num)
read(90901001)

原文地址:https://www.cnblogs.com/bobliao/p/9943459.html

时间: 2024-08-02 02:09:20

Python TIPS上一道关于人民币金额小写转大写的题的相关文章

[转] 金额小写变大写

1 /// <summary> 2 /// 金额小写变大写 3 /// </summary> 4 /// <param name="smallnum"></param> 5 /// <returns></returns> 6 public static string gMoney(decimal smallnum) 7 { 8 string cmoney , cnumber, cnum, cnum_end,cmon

JS函数实现金额小写转大写

止乎于分享! 1 ///<summery>小写金额转化大写金额</summery> 2 function AmountLtoU(amount) { 3 if (isNaN(amount) || amount >= 1000000000000) return "无效金额!"; //数值最大不超过1万亿 4 var sPrefix = amount < 0 ? "(负)" : ""; //将负号‘-’显示成汉字‘(

C#金额小写转大写

public string ConvertMoney(decimal Money) { //金额转换程序 string MoneyNum = "";//记录小写金额字符串[输入参数] string MoneyStr = "";//记录大写金额字符串[输出参数] string BNumStr = "零壹贰叁肆伍陆柒捌玖";//模 string UnitStr = "万仟佰拾亿仟佰拾万仟佰拾圆角分";//模 MoneyNum =

SQL金额小写转大写

CREATE FUNCTION dbo.L2U(@n_LowerMoney numeric(15,2),@v_TransType int) RETURNS VARCHAR(200) AS BEGIN Declare @v_LowerStr VARCHAR(200) -- 小写金额 Declare @v_UpperPart VARCHAR(200) Declare @v_UpperStr VARCHAR(200) -- 大写金额 Declare @i_I int set @v_LowerStr =

用python实现把数字人民币金额转换成大写的脚本程序

# -*- coding: utf-8 -*- def Num2MoneyFormat( change_number ): """ .转换数字为大写货币格式( format_word.__len__() - 3 + 2位小数 ) change_number 支持 float, int, long, string """ format_word = ["分", "角", "元", &quo

js 金额小写转换为大写

<script> jQuery(document).ready(function () { //当金额文本框失去焦点时,自动将数字转化为大写填充到 大写的文本框中 $("#Amount").blur(function () { var amount = $("#Amount").val(); amount = AmountLtoU(amount); $("#CapitalAmount").val(amount); }); }); fu

C#金额小写转换为大写

//传入需要转换的金额(字符串) public static string MoneyToChinese(string strAmount)     { string functionReturnValue = null; bool IsNegative = false; // 是否是负数 if (strAmount.Trim().Substring(0, 1) == "-")         { // 是负数则先转为正数 strAmount = strAmount.Trim().Re

python tips(持续更新)

1. 引用上一层目录 import syssys.path.append('..')import xx 2. python json JSON是一种轻量级的数据交换格式.可以解决数据库中文存储问题,对象序列化问题,等等. import json encodedjson = json.dumps(obj) decodejson = json.loads(encodedjson) 非常简单. 3. 静态方法 在函数前面@staticmethod @staticmethod def func(): p

成功解决在Python文件上右键菜单无“Edit with IDLE”选项

我电脑是Win7旗舰版,之前电脑上安装的是Python2.6版本的,前两天为了体验一下Microsoft Excel与Python之间互操作, 下载并安装了DataNitro,在安装的时候脑残的安装了Python2.7.5,但是这两天在用的时候,发现之前安装的一些包后不能用了: print sys.path 才知道,Python的搜索路径变成了Python2.7.5的路径了,傻眼了,怎么办? 于是,一通百度,google之后,折腾了一个上午,终于搞定了: 首先,我在cmd里面是可以正常使用Pyt