Number in french plain text (Views: 27)
Problem/Question/Abstract: Converting integer to plain text in French Answer: function IntToLetters(N: Integer): string; function Mille(N: Integer; P: Integer): string; // Calcul des nombre de 0..99 function Cent(N: Integer): string; const X: array[0..20] of string = ('zero', 'un', 'deux', 'trois', 'quatre', 'cinq', 'six', 'sept', 'huit', 'neuf', 'dix', 'onze', 'douze', 'treize', 'quatorze', 'quinze', 'seize', 'dix-sept', 'dix-huit', 'dix-neuf', 'vingt'); Y: array[2..10] of string = ('vingt', 'trente', 'quarante', 'cinquante', 'soixante', 'soixante', 'quatre-vingt', 'quatre-vingt', 'cent'); var A, B: Integer; R, C: string; begin // Si le nombre est inferieur ou egal a 20 on a la solution directe if (N <= 20) then begin R := X[N]; end; // Si le nombre est superieur a 20 if (N > 20) and (N < 100) then begin // on prend la dizaine A := N div 10; // on pend l'unité B := N mod 10; // si l'unité est un, le séparateur est 'et' if (B = 1) and (A in [2, 3, 4, 5, 6, 7]) then C := ' et ' else C := ' '; // si l'unite est supérieure a 1, le séparateur est un '-' if (B > 1) and (A in [2, 3, 4, 5, 6, 7, 8, 9]) then C := '-'; // si la dizaine est 7 ou 9 on compte les unités de 10 ? 19 if (A = 7) or (A = 9) then B := B + 10; // On calcule la solution if (B = 0) then R := Y[A] else R := Y[A] + C + X[B]; end; Result := R; end; // Calcul des nombres de 100..999 var A, B: Integer; R: string; begin if (N >= 100) then begin // on prend la centaine A := N div 100; // on prend le reste B := N mod 100; if (A = 0) or (A = 1) then begin // si la centaine est 0 ou 1 // on calcule et on 'cent' est au singulier if (B = 0) then R := 'cent ' else R := 'cent ' + Cent(B); end else begin // si la centaine est > 1 if (P = 0) then begin // si c'est la fin d'un nombre (P=0) // on mets 'cents' au pluriel si pas d'unité sinon on met 'cent' au singulier if (B = 0) then R := Cent(A) + ' cents ' else R := Cent(A) + ' cent ' + Cent(B); end else begin // si ce n'est pas la fin d'un nombre 'cent' est au singulier if (B = 0) then R := Cent(A) + ' cent ' else R := Cent(A) + ' cent ' + Cent(B); end; end; end else begin // si le nombre est inférieur a 100 on le calcule directement R := Cent(N); end; Result := R; end; // Function principale const Z: array[0..3] of string = ('', 'mille', 'million', 'milliard'); var B, I: Integer; R, M: string; begin R := ''; I := 0; // On va décomposer en tranche de 1000 en partant de la droite while (N > 0) do begin // prend une tranche (reste de la division par 1000) B := N mod 1000; // le pluriel est utilisé a partir des milliers if (I = 0) then M := ' ' else M := 's '; if I = 1 then begin // on calcule la tranche des milliers // si le nombre de milliers est supérieur a 1 on ecrit le nombre et 'milles' if (B > 1) then R := Mille(B, I) + ' ' + Z[I] + M + R; // sinon on éecrit 'mille' et pas 'un mille' if (B = 1) then R := Z[I] + ' ' + R; end else begin // on calcule les millions et suivants // on mets un 's' au pluriel if (B > 1) then R := Mille(B, I) + ' ' + Z[I] + M + R; // on n'en met pas au singulier if (B = 1) then R := Mille(B, I) + ' ' + Z[I] + ' ' + R; end; // on decale de 3 rangs vers la droite N := N div 1000; I := I + 1; end; if (R = '') then R := 'zéro'; Result := R; end; |