utilize Python Convert Arabic numerals into Chinese capital , In fact, the most troublesome place is that there are many in the middle space 0 The problem of , In this case , Adopt the split rule , Put a big number , First split into integer parts and small Several parts , Then the integer part is divided into thousands 、 ten thousand 、 Billion 、 The megabit is split into four strings List, Each string has a maximum of 4 Characters , Then the string of each quantile is converted to uppercase by the uppercase function , most After the merger , This is tantamount to reducing the problem , Processing is relatively simple
#!/usr/bin/env python -- coding: utf-8 -- ''' Algorithm description : Require string input , Now the string difference fee is generated as integer part and decimal part list[ Integral part , The fractional part ] Split the integer part into :[ Billion , ten thousand , Thousand ] Three sets of strings List:['0000','0000','0000']( Generate ladder based on actual input List) for example :600190000010.70 The integer part is split into :['600','1900','0010'] Then on list Capitalize and merge each string group in Finally, capitalize the decimal part ''' class cnumber: cdict={} gdict={} xdict={} def init(self): self.cdict={1:u'',2:u' Ten ',3:u' Bai ',4:u' Thousand '} self.xdict={1:u' element ',2:u' ten thousand ',3:u' Billion ',4:u' mega '} # Digital identifier self.gdict={0:u' zero ',1:u' one ',2:u' Ii. ',3:u' 3 ',4:u' boss ',5:u' wu ',6:u' lu ',7:u' Retailer, ',8:u' ',9:u' nine '} def csplit(self,cdata): # Split function , Split an integer string into [ Billion , ten thousand , Thousand ] Of 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): # Yes [ Billion , ten thousand , Thousand ] Of list Capitalize and merge each string group in lenki=len(cki) i=0 lk=lenki chk=u'' for i in range(lenki): if int(cki[i])==0: if i</pre>