-
Notifications
You must be signed in to change notification settings - Fork 3
/
trad_chiffre_mot.py
105 lines (96 loc) · 2.78 KB
/
trad_chiffre_mot.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# -*- coding: cp1252 -*-
"""
Traduction d'un nombre en texte.
Realisation : Michel Claveau http://mclaveau.com
SVP, n'enlevez pas mon adresse/URL ; merci d'avance
Note : traduction franco-francaise, avec unites variables, orthographe gere, unites et centiemes.
"""
def tradd(num):
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", "septante", "quatre-vingt", "nonante"]
ch = ''
if num == 0:
ch = ''
elif num < 20:
ch = t1[num]
elif num >= 20:
if (70 <= 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):
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="", decim=""):
nb = round(nb, 2)
z1 = int(nb)
z3 = (nb - z1) * 100
z2 = int(round(z3, 0))
if z1 == 0:
ch = "zéro"
else:
ch = tradn(abs(z1))
if z1 > 1 or z1 < -1:
if unite != '':
ch = ch + " " + unite + 's'
else:
ch = ch + " " + unite
if z2 > 0:
ch = ch + tradn(z2)
if z2 > 1 or z2 < -1:
if decim != '':
ch = ch + " " + decim + 's'
else:
ch = ch + " " + decim
if nb < 0:
ch = "moins " + ch
return ch