我必须创建一个 Python 类,将数字量更改为法语文本。我找到了一个可以完成这项工作的类,但是当每个示例的浮点数为 50.4 时,它会返回 " Cinquante units et 399
。
toText 函数接受 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")