Mega Code Archive

 
Categories / Delphi / Examples
 

Number in french plain text

Title: Number in french plain text Question: 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 R := X[N]; end; // Si le nombre est superieur a 20 If (N 20) and (N // on prend la dizaine A := N div 10; // on pend l'unit B := N Mod 10; // si l'unit est un, le sparateur est 'et' If (B = 1) and (A in [2,3,4,5,6,7]) then C := ' et ' else C := ' '; // si l'unite est suprieure a 1, le sparateur 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 units 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 infrieur 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 dcomposer 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 suprieur 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 := 'zro'; Result := R; end;