-
Notifications
You must be signed in to change notification settings - Fork 12
/
nombre_en_lettre.js
190 lines (178 loc) · 10.3 KB
/
nombre_en_lettre.js
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
module.exports=(function() {
// JavaScript Document
/****************************************************************************
*________________________________________________________________________ *
* About : Convertit jusqu'à 999 999 999 999 999 (billion) *
* avec respect des accords *
*_________________________________________________________________________ *
* Auteur : GALA OUSSE Brice, Engineer programmer of management *
* Mail : [email protected], [email protected] *
* Tél : +237 99 37 95 83 / +237 79 99 82 80 *
* Copyright : avril 2007
* Ce document intitulé « Conversion des nombres en lettre » issu de CommentCaMarche
* (codes-sources.commentcamarche.net) est mis à disposition sous les termes de
* la licence Creative Commons. Vous pouvez copier, modifier des copies de cette
* source, dans les conditions fixées par la licence, tant que cette note
* apparaît clairement. *
*_________________________________________________________________________ *
*****************************************************************************
*/
function Unite( nombre ){
var unite;
switch( nombre ){
case 0: unite = "zéro"; break;
case 1: unite = "un"; break;
case 2: unite = "deux"; break;
case 3: unite = "trois"; break;
case 4: unite = "quatre"; break;
case 5: unite = "cinq"; break;
case 6: unite = "six"; break;
case 7: unite = "sept"; break;
case 8: unite = "huit"; break;
case 9: unite = "neuf"; break;
}//fin switch
return unite;
}//-----------------------------------------------------------------------
function Dizaine( nombre ){
switch( nombre ){
case 10: dizaine = "dix"; break;
case 11: dizaine = "onze"; break;
case 12: dizaine = "douze"; break;
case 13: dizaine = "treize"; break;
case 14: dizaine = "quatorze"; break;
case 15: dizaine = "quinze"; break;
case 16: dizaine = "seize"; break;
case 17: dizaine = "dix-sept"; break;
case 18: dizaine = "dix-huit"; break;
case 19: dizaine = "dix-neuf"; break;
case 20: dizaine = "vingt"; break;
case 30: dizaine = "trente"; break;
case 40: dizaine = "quarante"; break;
case 50: dizaine = "cinquante"; break;
case 60: dizaine = "soixante"; break;
case 70: dizaine = "soixante-dix"; break;
case 80: dizaine = "quatre-vingt"; break;
case 90: dizaine = "quatre-vingt-dix"; break;
}//fin switch
return dizaine;
}//-----------------------------------------------------------------------
function NumberToLetter( nombre ){
var i, j, n, quotient, reste, nb ;
var ch
var numberToLetter='';
//__________________________________
if( nombre.toString().replace( / /gi, "" ).length > 15 ) return "dépassement de capacité";
if( isNaN(nombre.toString().replace( / /gi, "" )) ) return "Nombre non valide";
nb = parseFloat(nombre.toString().replace( / /gi, "" ));
if( Math.ceil(nb) != nb ) return "Nombre avec virgule non géré.";
n = nb.toString().length;
switch( n ){
case 1: numberToLetter = Unite(nb); break;
case 2: if( nb > 19 ){
quotient = Math.floor(nb / 10);
reste = nb % 10;
if( nb < 71 || (nb > 79 && nb < 91) ){
if( reste == 0 ) numberToLetter = Dizaine(quotient * 10);
if( reste == 1 ) numberToLetter = Dizaine(quotient * 10) + "-et-" + Unite(reste);
if( reste > 1 ) numberToLetter = Dizaine(quotient * 10) + "-" + Unite(reste);
}else numberToLetter = Dizaine((quotient - 1) * 10) + "-" + Dizaine(10 + reste);
}else numberToLetter = Dizaine(nb);
break;
case 3: quotient = Math.floor(nb / 100);
reste = nb % 100;
if( quotient == 1 && reste == 0 ) numberToLetter = "cent";
if( quotient == 1 && reste != 0 ) numberToLetter = "cent" + " " + NumberToLetter(reste);
if( quotient > 1 && reste == 0 ) numberToLetter = Unite(quotient) + " cents";
if( quotient > 1 && reste != 0 ) numberToLetter = Unite(quotient) + " cent " + NumberToLetter(reste);
break;
case 4 : quotient = Math.floor(nb / 1000);
reste = nb - quotient * 1000;
if( quotient == 1 && reste == 0 ) numberToLetter = "mille";
if( quotient == 1 && reste != 0 ) numberToLetter = "mille" + " " + NumberToLetter(reste);
if( quotient > 1 && reste == 0 ) numberToLetter = NumberToLetter(quotient) + " mille";
if( quotient > 1 && reste != 0 ) numberToLetter = NumberToLetter(quotient) + " mille " + NumberToLetter(reste);
break;
case 5 : quotient = Math.floor(nb / 1000);
reste = nb - quotient * 1000;
if( quotient == 1 && reste == 0 ) numberToLetter = "mille";
if( quotient == 1 && reste != 0 ) numberToLetter = "mille" + " " + NumberToLetter(reste);
if( quotient > 1 && reste == 0 ) numberToLetter = NumberToLetter(quotient) + " mille";
if( quotient > 1 && reste != 0 ) numberToLetter = NumberToLetter(quotient) + " mille " + NumberToLetter(reste);
break;
case 6 : quotient = Math.floor(nb / 1000);
reste = nb - quotient * 1000;
if( quotient == 1 && reste == 0 ) numberToLetter = "mille";
if( quotient == 1 && reste != 0 ) numberToLetter = "mille" + " " + NumberToLetter(reste);
if( quotient > 1 && reste == 0 ) numberToLetter = NumberToLetter(quotient) + " mille";
if( quotient > 1 && reste != 0 ) numberToLetter = NumberToLetter(quotient) + " mille " + NumberToLetter(reste);
break;
case 7: quotient = Math.floor(nb / 1000000);
reste = nb % 1000000;
if( quotient == 1 && reste == 0 ) numberToLetter = "un million";
if( quotient == 1 && reste != 0 ) numberToLetter = "un million" + " " + NumberToLetter(reste);
if( quotient > 1 && reste == 0 ) numberToLetter = NumberToLetter(quotient) + " millions";
if( quotient > 1 && reste != 0 ) numberToLetter = NumberToLetter(quotient) + " millions " + NumberToLetter(reste);
break;
case 8: quotient = Math.floor(nb / 1000000);
reste = nb % 1000000;
if( quotient == 1 && reste == 0 ) numberToLetter = "un million";
if( quotient == 1 && reste != 0 ) numberToLetter = "un million" + " " + NumberToLetter(reste);
if( quotient > 1 && reste == 0 ) numberToLetter = NumberToLetter(quotient) + " millions";
if( quotient > 1 && reste != 0 ) numberToLetter = NumberToLetter(quotient) + " millions " + NumberToLetter(reste);
break;
case 9: quotient = Math.floor(nb / 1000000);
reste = nb % 1000000;
if( quotient == 1 && reste == 0 ) numberToLetter = "un million";
if( quotient == 1 && reste != 0 ) numberToLetter = "un million" + " " + NumberToLetter(reste);
if( quotient > 1 && reste == 0 ) numberToLetter = NumberToLetter(quotient) + " millions";
if( quotient > 1 && reste != 0 ) numberToLetter = NumberToLetter(quotient) + " millions " + NumberToLetter(reste);
break;
case 10: quotient = Math.floor(nb / 1000000000);
reste = nb - quotient * 1000000000;
if( quotient == 1 && reste == 0 ) numberToLetter = "un milliard";
if( quotient == 1 && reste != 0 ) numberToLetter = "un milliard" + " " + NumberToLetter(reste);
if( quotient > 1 && reste == 0 ) numberToLetter = NumberToLetter(quotient) + " milliards";
if( quotient > 1 && reste != 0 ) numberToLetter = NumberToLetter(quotient) + " milliards " + NumberToLetter(reste);
break;
case 11: quotient = Math.floor(nb / 1000000000);
reste = nb - quotient * 1000000000;
if( quotient == 1 && reste == 0 ) numberToLetter = "un milliard";
if( quotient == 1 && reste != 0 ) numberToLetter = "un milliard" + " " + NumberToLetter(reste);
if( quotient > 1 && reste == 0 ) numberToLetter = NumberToLetter(quotient) + " milliards";
if( quotient > 1 && reste != 0 ) numberToLetter = NumberToLetter(quotient) + " milliards " + NumberToLetter(reste);
break;
case 12: quotient = Math.floor(nb / 1000000000);
reste = nb - quotient * 1000000000;
if( quotient == 1 && reste == 0 ) numberToLetter = "un milliard";
if( quotient == 1 && reste != 0 ) numberToLetter = "un milliard" + " " + NumberToLetter(reste);
if( quotient > 1 && reste == 0 ) numberToLetter = NumberToLetter(quotient) + " milliards";
if( quotient > 1 && reste != 0 ) numberToLetter = NumberToLetter(quotient) + " milliards " + NumberToLetter(reste);
break;
case 13: quotient = Math.floor(nb / 1000000000000);
reste = nb - quotient * 1000000000000;
if( quotient == 1 && reste == 0 ) numberToLetter = "un billion";
if( quotient == 1 && reste != 0 ) numberToLetter = "un billion" + " " + NumberToLetter(reste);
if( quotient > 1 && reste == 0 ) numberToLetter = NumberToLetter(quotient) + " billions";
if( quotient > 1 && reste != 0 ) numberToLetter = NumberToLetter(quotient) + " billions " + NumberToLetter(reste);
break;
case 14: quotient = Math.floor(nb / 1000000000000);
reste = nb - quotient * 1000000000000;
if( quotient == 1 && reste == 0 ) numberToLetter = "un billion";
if( quotient == 1 && reste != 0 ) numberToLetter = "un billion" + " " + NumberToLetter(reste);
if( quotient > 1 && reste == 0 ) numberToLetter = NumberToLetter(quotient) + " billions";
if( quotient > 1 && reste != 0 ) numberToLetter = NumberToLetter(quotient) + " billions " + NumberToLetter(reste);
break;
case 15: quotient = Math.floor(nb / 1000000000000);
reste = nb - quotient * 1000000000000;
if( quotient == 1 && reste == 0 ) numberToLetter = "un billion";
if( quotient == 1 && reste != 0 ) numberToLetter = "un billion" + " " + NumberToLetter(reste);
if( quotient > 1 && reste == 0 ) numberToLetter = NumberToLetter(quotient) + " billions";
if( quotient > 1 && reste != 0 ) numberToLetter = NumberToLetter(quotient) + " billions " + NumberToLetter(reste);
break;
}//fin switch
/*respect de l'accord de quatre-vingt*/
if( numberToLetter.substr(numberToLetter.length-"quatre-vingt".length,"quatre-vingt".length) == "quatre-vingt" ) numberToLetter = numberToLetter + "s";
return numberToLetter;
}//-----------------------------------------------------------------------
return NumberToLetter;
})();