forked from luxigo/number-to-letters
-
Notifications
You must be signed in to change notification settings - Fork 10
/
nombre_en_lettre.js
144 lines (134 loc) · 6.74 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
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 NumberToLetter(nombre, U=null, D=null) {
var letter = {
0: "zéro",
1: "un",
2: "deux",
3: "trois",
4: "quatre",
5: "cinq",
6: "six",
7: "sept",
8: "huit",
9: "neuf",
10: "dix",
11: "onze",
12: "douze",
13: "treize",
14: "quatorze",
15: "quinze",
16: "seize",
17: "dix-sept",
18: "dix-huit",
19: "dix-neuf",
20: "vingt",
30: "trente",
40: "quarante",
50: "cinquante",
60: "soixante",
70: "soixante-dix",
80: "quatre-vingt",
90: "quatre-vingt-dix",
};
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é.";
if(Math.ceil(nb) != nb){
nb = nombre.toString().split('.');
//return NumberToLetter(nb[0]) + " virgule " + NumberToLetter(nb[1]);
return NumberToLetter(nb[0]) + (U ? " " + U + " et " : " virgule ") + NumberToLetter(nb[1]) + (D ? " " + D : "");
}
n = nb.toString().length;
switch (n) {
case 1:
numberToLetter = letter[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 = letter[quotient * 10];
if (reste == 1) numberToLetter = letter[quotient * 10] + "-et-" + letter[reste];
if (reste > 1) numberToLetter = letter[quotient * 10] + "-" + letter[reste];
} else numberToLetter = letter[(quotient - 1) * 10] + "-" + letter[10 + reste];
} else numberToLetter = letter[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 = letter[quotient] + " cents";
if (quotient > 1 && reste != 0) numberToLetter = letter[quotient] + " cent " + NumberToLetter(reste);
break;
case 4 :
case 5 :
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:
case 8:
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:
case 11:
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:
case 14:
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;
})();