Python converts Arabic numerals to Chinese uppercase

高洛峰
Release: 2016-10-18 10:36:01
Original
3125 people have browsed it

Use Python to convert Arabic numerals into Chinese uppercase letters. In fact, the most troublesome part is the problem of multiple zeros in the middle. In this case, use the splitting rule to split a large number into an integer part and a decimal part. Then split the integer part into a list of four strings according to the digits of thousands, ten thousand, billions, and megabytes. Each string can have up to 4 characters. Then use the uppercase function to convert the strings in each digit into uppercase. Finally merge, this is equivalent to reducing the problem, and the processing is relatively simple

#!/usr/bin/env python
# -*- coding: utf-8 -*-
'''
#算法说明:要求字符串输入,现将字符串差费为整数部分和小数部分生成list[整数部分,小数部分]
#将整数部分拆分为:[亿,万,仟]三组字符串组成的List:['0000','0000','0000'](根据实际输入生成阶梯List)
#例如:600190000010.70整数部分拆分为:['600','1900','0010']
#然后对list中每个字符串分组进行大写化再合并
#最后处理小数部分的大写化
'''
class cnumber:
    cdict={}
    gdict={}
    xdict={}
    def __init__(self):
        self.cdict={1:u'',2:u'拾',3:u'佰',4:u'仟'}
        self.xdict={1:u'元',2:u'万',3:u'亿',4:u'兆'} #数字标识符
        self.gdict={0:u'零',1:u'壹',2:u'贰',3:u'叁',4:u'肆',5:u'伍',6:u'陆',7:u'柒',8:u'捌',9:u'玖'}    
    def csplit(self,cdata): #拆分函数,将整数字符串拆分成[亿,万,仟]的list
        g=len(cdata)%4
        csdata=[]
        lx=len(cdata)-1
        if g>0:
            csdata.append(cdata[0:g])
        k=g
        while k<=lx:
            csdata.append(cdata[k:k+4])
            k+=4
        return csdata
                  
    def cschange(self,cki): #对[亿,万,仟]的list中每个字符串分组进行大写化再合并
        lenki=len(cki)
        i=0
        lk=lenki
        chk=u&#39;&#39;
        for i in range(lenki):
            if int(cki[i])==0:
                if i
Copy after login


source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template