数値をフランス語のテキストに変更する Python クラスを作成する必要があります。この作業を行うクラスを見つけましたが、フロートが例ごとに 50.4 の場合、 " を返しますCinquante units et 399
。
関数 toText は、変換する float、単位、decim の 3 つのパラメータを取ります。
def tradd(num):
global t1,t2
ch=''
if num==0 :
ch=''
elif num<20:
ch=t1[num]
elif num>=20:
if (num>=70 and num<=79)or(num>=90):
z=int(num/10)-1
else:
z=int(num/10)
ch=t2[z]
num=num-z*10
if (num==1 or num==11) and z<8:
ch=ch+' et'
if num>0:
ch=ch+' '+tradd(num)
else:
ch=ch+tradd(num)
return ch
def tradn(num):
global t1,t2
ch=''
flagcent=False
if num>=1000000000:
z=int(num/1000000000)
ch=ch+tradn(z)+' milliard'
if z>1:
ch=ch+'s'
num=num-z*1000000000
if num>=1000000:
z=int(num/1000000)
ch=ch+tradn(z)+' million'
if z>1:
ch=ch+'s'
num=num-z*1000000
if num>=1000:
if num>=100000:
z=int(num/100000)
if z>1:
ch=ch+' '+tradd(z)
ch=ch+' cent'
flagcent=True
num=num-z*100000
if int(num/1000)==0 and z>1:
ch=ch+'s'
if num>=1000:
z=int(num/1000)
if (z==1 and flagcent) or z>1:
ch=ch+' '+tradd(z)
num=num-z*1000
ch=ch+' mille'
if num>=100:
z=int(num/100)
if z>1:
ch=ch+' '+tradd(z)
ch=ch+" cent"
num=num-z*100
if num==0 and z>1:
ch=ch+'s'
if num>0:
ch=ch+" "+tradd(num)
return ch
def trad(nb, unite):
global t1,t2
x=int(nb)
y=int((nb-x)*1000)
t1=["","un","deux","trois","quatre","cinq","six","sept","huit","neuf","dix","onze","douze","treize","quatorze","quinze","seize","dix-sept","dix-huit","dix-neuf"]
t2=["","dix","vingt","trente","quarante","cinquante","soixante","soixante-dix","quatre-vingt","quatre-vingt dix"]
if x==0:
ch="zéro"
else:
ch=tradn(abs(x))
if x>1 or x<-1:
if unite!='':
ch=ch+" "+unite+'s'
else:
ch=ch+" "+unite
if x<0:
ch="moins "+ch
return ch
def toText(nb, unite="Dinar", decim="millime"):
x=int(nb)
y=(nb-x)*1000
z=int(y)
if y > 1:
text_amount=trad(x,unite)+" et "+str(z)+" "+decim+"s"
elif y==1:
text_amount=trad(x,unite)+" et "+str(z)+" "+decim
elif y==0:
text_amount=trad(x,unite)+" et zéro "+decim
return text_amount
if __name__=='__main__':
print toText(45.4,"dinar")