数字转人民币金额大写

代码如下:

基于

(1条消息) python人民币金额转汉字大写_Glen_Zou的博客-CSDN博客_python人民币转大写

做了一些Debug

如果有什么漏掉的错误情况,请评论给我~

#!/bin/python
# -*- coding: utf8 -*-
import sys
import os
import re
import copy


# 请完成下面这个函数,实现题目要求的功能
# 当然,你也可以不按照下面这个模板来作答,完全按照自己的想法来 ^-^
# ******************************开始写代码******************************


def getFinanceStr(s):
    cnNums = ['零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖']
    cnIntRadice = ['', '拾', '佰', '仟']
    cnIntUnits = ['', '万', '亿',"兆"]
    cnDecUnits = ['角', '分']
    cnInteger = "整"
    cnIntLast = '元'
    maxNum = float('inf')
    res = ""
    parts = []
    if s == "":
        return ""
    money = float(s)
    if money == 0:
        return cnNums[0] + cnIntLast + cnInteger
    money = str(money)
    if money.find('.') == -1:
        IntegerNum = money
        DecimalNum = ""
    else:
        IntegerNum, DecimalNum = money.split('.')
    if int(IntegerNum) > 0:
        zeroCount = 0
        for i in range(0, len(IntegerNum)):
            n = IntegerNum[i:i + 1:1]
            p = len(IntegerNum) - i - 1
            q = p // 4
            m = p % 4
            if n == "0":
                zeroCount += 1
            else:
                if zeroCount > 0:
                    res += cnNums[0]
                zeroCount = 0
                res += cnNums[int(n)]
                if m == 0 and zeroCount < 4:
                    res += cnIntUnits[q]
                else:
                    res+=cnIntRadice[p%4]
        res += cnIntLast
    if DecimalNum != "":
        for i in range(len(DecimalNum)):
            n = DecimalNum[i:i + 1:1]
            if n != '0':
                res += cnNums[int(n)] + cnDecUnits[i]
    if res == "":
        res += cnNums[0] + cnIntLast + cnInteger
    elif DecimalNum == "0":
        res += cnInteger
    return res


# ******************************结束写代码******************************


try:
    _s = input()
except:
    _s = None

res = getFinanceStr(_s)

print(res + "\n")

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值