PyTip Task16 人民币金额转换 |模拟 |超多细节

最近做的题里A的最感动的一道……我要哭了。

  • 对0的处理,切成两半后连接点处的0,还有连续0的处理。
  • 负数!
  • 编码!

纪念

# a = '9999999'

m = str(a)

totalans = ''

if(m[0] == '-'):
    m = m[1:]
    totalans += u'负'
    
p = len(m)

dict1 = {'0':u'零','1':u'壹','2':u'贰','3':u'叁','4':u'肆','5':u'伍','6':u'陆','7':u'柒','8':u'捌','9':u'玖'}

dict2 = [u'拾',u'佰',u'仟','']

def four(s):
    ans = ''
    tlen = len(s)

    
    for i in range(tlen):
        
#         print(ans, i, tlen-1, s[i])
        
        if(p != 1):  
            if(i == 0 and flag != 1 and s[i] != '0'):
                if(m[3] == '0' and m[2] == '0'):
                    ans += u'零'

            if(i == tlen-1 and s[i] == '0'): #像1000 10 100 尾0不加‘零’
                continue;
            if(s[i] == '0' and s[i+1] == '0'):#中间有多个连续的0只加一个0
                continue;

        ans += dict1[s[i]]
        
        
#         print(ans, i, tlen-1, s[i])
        
        if(p != 1):
            if(s[i] == '0' and s[i+1] !='0'):
                continue
        ans += dict2[tlen-i-2]
        
    return ans

if(p>4):
    flag = 1
    head = m[:p-4]
    totalans += four(head)
    totalans += u'万'

    flag = 0
    tail = m[p-4:]
    totalans += four(tail)
    

else:
    flag = 1
    totalans += four(m)
    

totalans += u'圆'
print(totalans)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值