Conversion d’un chiffre (montant) en lettre.

Description

C’est une fonction qui permet de convertir un chiffre en lettre, ça peut être utile pour les développeurs des applications de facturation.

Source / Exemple :


// a mon avis, la fonction n'a pas besoin d'être commentée.

Function Convert_ch_let(chiffre: real; SM: String): string;
var tab_1_19 : array[0..19] of string[10];
    tab_20_90: array[2..9] of string[20];
    tab: array[1..5] of string[10];
    ch_int: string[20];
    ch, ch_r: string;
    i, fin_I : integer;

Function result_1_99(ch: string): string;
begin
   result_1_99 := '';
   case strtoint(ch) of
   0..19  : result_1_99 := tab_1_19[strtoint(ch)];
   20..99 : case strtoint(ch) of
            20,30,40,50,60: result_1_99 := tab_20_90[strtoint(copy(ch,1,1))];
            21,31,41,51,61: result_1_99 := tab_20_90[strtoint(copy(ch,1,1))] + ' et' + tab_1_19[strtoint(copy(ch,2,1))];
            80: result_1_99 := tab_20_90[strtoint(copy(ch,1,1)) ];
            70..79 : result_1_99 := tab_20_90[strtoint(copy(ch,1,1)) - 1] + '-' + copy(tab_1_19[strtoint(ch) - 60],2,length(tab_1_19[strtoint(ch) - 60]) -1);
            81..89 : result_1_99 := tab_20_90[strtoint(copy(ch,1,1)) ] + '-' +  copy(tab_1_19[strtoint(ch) - 80],2,length(tab_1_19[strtoint(ch) - 80]) -1);
            90..99 : result_1_99 := tab_20_90[strtoint(copy(ch,1,1)) - 1] + '-' + copy(tab_1_19[strtoint(ch) - 80],2,length(tab_1_19[strtoint(ch) - 80])-1 );
            else
            result_1_99 := tab_20_90[strtoint(copy(ch,1,1))] + '-' + copy(tab_1_19[strtoint(copy(ch,2,1))],2,length(tab_1_19[strtoint(copy(ch,2,1))]) -1);
            end;
   end;
end;

begin
if length(floattostr(int(chiffre))) > 15 then  // s'il y a autre chose après le Billion, alors remplacer le 15 par 18
   begin
   messagedlg('Valeur incorrecte ',mtinformation,[mbok],0);
   exit;
   end;
tab_1_19[0] := '';
tab_1_19[1] := ' un'; tab_1_19[2] := ' deux'; tab_1_19[3] := ' trois';
tab_1_19[4] := ' quatre'; tab_1_19[5] := ' cinq'; tab_1_19[6] := ' six';
tab_1_19[7] := ' sept'; tab_1_19[8] := ' huit'; tab_1_19[9] := ' neuf';
tab_1_19[10] := ' dix'; tab_1_19[11] := ' onze'; tab_1_19[12] := ' douze';
tab_1_19[13] := ' treize'; tab_1_19[14] := ' quatorze'; tab_1_19[15] := ' quinze';
tab_1_19[16] := ' seize'; tab_1_19[17] := ' dix-sept'; tab_1_19[18] := ' dix-huit';
tab_1_19[19] := ' dix-neuf';

tab_20_90[2] := ' vingt'; tab_20_90[3] := ' trente'; tab_20_90[4] := ' quarante';
tab_20_90[5] := ' cinquante'; tab_20_90[6] := ' soixante';
tab_20_90[7] := ' soixante-dix'; tab_20_90[8] := ' quatre-vingt'; tab_20_90[9] := ' quatre-vingt-dix';

tab[1] := ' cent';
tab[2] := ' mille';
tab[3] := ' million';
tab[4] := ' milliard';
tab[5] := ' billion';

ch_int := floattostr(int(chiffre));
ch_r := '';
fin_i := length(ch_int) div 3;
if length(ch_int) > fin_i * 3 then
   begin
   ch := copy(ch_int,1, length(ch_int) - fin_i * 3);
   ch_r := result_1_99(ch);
   if (strtoint(ch) = 1) and (fin_i = 1) then
      ch_r := tab[fin_i + 1]
   else
      if length(ch_int) > 2 then
         ch_r := ch_r + tab[fin_i + 1];
   end;

For i := fin_i downto 1 do
    begin
    ch := copy(ch_int, length(ch_int) - i * 3 + 1 , 3);

    case strtoint(copy(ch,1,1)) of
    0: ch_r := ch_r + result_1_99(copy(ch,1,1)); // dans le cas 0, on peut remplacer result_1_99(copy(ch,1,1)) par une chaine vide. C'est à dire   ch_r := ch_r + '';
    1: ch_r := ch_r + tab[1]; // là aussi on peut mettre cent à la place de tab[1]
    else
    ch_r :=  ch_r +  result_1_99(copy(ch,1,1)) + tab[1];
    end;

    if (i = 2) and (strtoint(ch) = 1) then // pour le cas de:  par ex 125001489
        ch_r := ch_r + copy(result_1_99(copy(ch,2,2)),3,length(result_1_99(copy(ch,2,2))) - 3 )
    else
        ch_r := ch_r + result_1_99(copy(ch,2,2));

    if (i > 1) and (strtoint(ch) > 0) then
       ch_r := ch_r + tab[i];
    end;

if SM <> '' then
   begin
   ch_r := Uppercase(copy(ch_r,2,1)) + copy(ch_r,3,length(ch_r) - 2) + SM;
   if frac(chiffre) > 0 then
    ch_r := ch_r + ', et ' + copy(floattostr(frac(chiffre) + 1.005 ),3,2) + ' cts.';
    // le 1.005 pour obtenir le bon resultat, enlever le et essayer avec les montants suivants:  par ex  15,20 ou 147,50
   end;

Convert_ch_let := ch_r;
end;

Codes Sources

A voir également

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.