一亿以内的数字转汉字(闲着没事)
j=0
n=0
g=0
point = str()
h=len(str(n))
Num = ["零","一","二","三","四","五","六","七","八","九"]
Name = ["十","百","千","","十","百","千","万"]
def PD_1(n,q):
if n%(10**(q))<(10**(q-1)):
return 0
else:
return 1
def PD(a,b):
if a!=0:
c = str(Num[a] + Name[b-2])
return c
else:
if PD_1(d,b+1)==1:
return Num[0]
else:
l=1
f = open("hanzi.txt","w")
c=int(input("请输入截至数字:"))
while(j<=c):
n=j
h=len(str(j))
g=len(str(j))
while(h>=2):
if PD_1(j,h)==1:
point = point + PD(int(n/(10**(h-1))),h)
n=n-(10**(len(str(n))-1))*(int(n/(10**(len(str(n))-1))))
else:
if (PD_1(j,h-1)==1)&(h>=3)&(h!=5):
point = point + "零"
if h==5:
point = point + "万"
h=h-1
if PD_1(j,1)==1:
if (PD_1(j,2)==1):
point = point + Num[n]
if (PD_1(j,2)==0)&(g>=2):
point = point+"零"+Num[n]
if(PD_1(j,2)==0)&(g<2):
point = point + Num[n]
f.write(point)
point = ""
j+=1
f.close()
print("over!!")
用了常规方法很麻烦,没有用正则表达方便。