#coding=gbk
import re
import math
chinese_digits = (u'零',u'一',u'二',u'三',u'四',u'五',u'六',u'七',u'八',u'九')
chinese_jinzhi = (u'万',u'',u'千',u'百',u'十',u'')
wanindex = 5
def formatdigitwithjinzhi(digit,jinzhi):
if(digit==0):
return chinese_digits[digit]
else:
return chinese_digits[digit] + jinzhi
def formatcapchnumber(chnumber):
s = chnumber
s = s.replace(u"一",u"壹").replace(u"二",u"贰").replace(u"三",u"叁").replace(u"四",u"肆").replace(u"五",u"伍").replace(u"六",u"陆").replace(u"七",u"柒").replace(u"八",u"捌").replace(u"九",u"玖")
s = s.replace(u"十",u"拾").replace(u"百",u"佰").replace(u"千",u"仟")
print s
def format4digits(digits,saveonezeroathead = False):
digits = '%04d' % digits
s = ''
jinzhi = -4
for digit in digits:
numeric_digit= int(digit)
s+=formatdigitwithjinzhi(numeric_digit,chinese_jinzhi[jinzhi])
jinzhi+=1
if(saveonezeroathead):
s = re.sub(r"^" + u"零" + "{1,}",u"零",s)
else:
s = re.sub(r"^" + u"零" + "+","",s)
s = re.sub(r"^" + u"一十",u"十",s)
s = re.sub(r"" + u"零" + "+$","",s)
s = re.sub(r"" + u"零" + "{2,}",u"零",s)
return s
def genejinzhi(wanindex):
s = ''
if(wanindex % 2==1):
s+=u"万"
yicount = wanindex / 2
for i in range(yicount):
s+=u"亿"
return s
def render(arg):
source = re.sub("\D","",arg)
source_list = []
for i in range(int(math.ceil(len(source)/4.0))):
if(i==0):
source_list.insert(0,int(source[-4:]))
else:
source_list.insert(0,int(source[((-i-1)*4):(-i*4)]))
result = ""
oldresult = ""
firstpartdanweiindex = wanindex - len(source_list) + 2
partindex = 0
for part in source_list:
oldresult = result
if(len(source_list)>1 and partindex==(len(source_list)-1)):
result += format4digits(part,saveonezeroathead = True)
else:
result += format4digits(part)
if(oldresult!=result):
result += genejinzhi(len(source_list) - partindex - 1)
partindex +=1
print result
formatcapchnumber(result)
while(True):
arg = raw_input("\n(q to quit)\n>>>")
if(arg=="quit" or arg=="q" or arg=="exit"):
break
else:
render(arg)
import re
import math
chinese_digits = (u'零',u'一',u'二',u'三',u'四',u'五',u'六',u'七',u'八',u'九')
chinese_jinzhi = (u'万',u'',u'千',u'百',u'十',u'')
wanindex = 5
def formatdigitwithjinzhi(digit,jinzhi):
if(digit==0):
return chinese_digits[digit]
else:
return chinese_digits[digit] + jinzhi
def formatcapchnumber(chnumber):
s = chnumber
s = s.replace(u"一",u"壹").replace(u"二",u"贰").replace(u"三",u"叁").replace(u"四",u"肆").replace(u"五",u"伍").replace(u"六",u"陆").replace(u"七",u"柒").replace(u"八",u"捌").replace(u"九",u"玖")
s = s.replace(u"十",u"拾").replace(u"百",u"佰").replace(u"千",u"仟")
print s
def format4digits(digits,saveonezeroathead = False):
digits = '%04d' % digits
s = ''
jinzhi = -4
for digit in digits:
numeric_digit= int(digit)
s+=formatdigitwithjinzhi(numeric_digit,chinese_jinzhi[jinzhi])
jinzhi+=1
if(saveonezeroathead):
s = re.sub(r"^" + u"零" + "{1,}",u"零",s)
else:
s = re.sub(r"^" + u"零" + "+","",s)
s = re.sub(r"^" + u"一十",u"十",s)
s = re.sub(r"" + u"零" + "+$","",s)
s = re.sub(r"" + u"零" + "{2,}",u"零",s)
return s
def genejinzhi(wanindex):
s = ''
if(wanindex % 2==1):
s+=u"万"
yicount = wanindex / 2
for i in range(yicount):
s+=u"亿"
return s
def render(arg):
source = re.sub("\D","",arg)
source_list = []
for i in range(int(math.ceil(len(source)/4.0))):
if(i==0):
source_list.insert(0,int(source[-4:]))
else:
source_list.insert(0,int(source[((-i-1)*4):(-i*4)]))
result = ""
oldresult = ""
firstpartdanweiindex = wanindex - len(source_list) + 2
partindex = 0
for part in source_list:
oldresult = result
if(len(source_list)>1 and partindex==(len(source_list)-1)):
result += format4digits(part,saveonezeroathead = True)
else:
result += format4digits(part)
if(oldresult!=result):
result += genejinzhi(len(source_list) - partindex - 1)
partindex +=1
print result
formatcapchnumber(result)
while(True):
arg = raw_input("\n(q to quit)\n>>>")
if(arg=="quit" or arg=="q" or arg=="exit"):
break
else:
render(arg)