0

我必须创建一个 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")
4

5 回答 5

2

这简直就是大家最爱的老朋友,浮点数错误。

这些行:

x=int(nb)
y=int((nb-x)*1000)

最终做:

(50.4-50)*1000 
# try this in your interpreter: 399.9999999999986

50.4 不能用(53 位)浮点数精确表示。您需要通过字符串格式或舍入进行相应调整。

于 2013-10-25T08:11:22.987 回答
2

而不是z=int(y)尝试使用z=int(round(y,0)). 那应该将您的 399.99... 舍入到 400。

>>> int(round((50.4-50.)*1000, 0))
400
于 2013-10-25T08:14:08.003 回答
2

正如其他答案已经说过的那样,您不能用 精确地表示浮点数float,因此50.4如您所见打印。

您可以尝试使用包十进制:它能够准确地表示浮点数,您可能能够得到预期的结果

于 2013-10-25T08:14:16.763 回答
1

问题是50.4无法准确表示:

>>> print('{:.47f}'.format(50.4))
50.39999999999999857891452847979962825775146484375

错误在于toText函数的定义。这些行:

x=int(nb)
y=(nb-x)*1000
z=int(y)

将导致z399代替400

如果您想提供更多“用户友好”的输出,您必须手动检查小数部分是否“奇怪”并相应地四舍五入。

于 2013-10-25T08:10:52.737 回答
1

你可以使用round

def toText(nb, unite="Dinar", decim="millime"):
    x=int(nb)
    y=(nb-x)*1000
    z=int(round(y))
于 2013-10-25T08:15:19.743 回答