IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

 Delphi Discussion :

convertir un chiffre en lettre (chiffre avec valeur -)


Sujet :

Delphi

  1. #1
    Membre du Club
    Inscrit en
    Mai 2008
    Messages
    89
    Détails du profil
    Informations forums :
    Inscription : Mai 2008
    Messages : 89
    Points : 49
    Points
    49
    Par défaut convertir un chiffre en lettre (chiffre avec valeur -)
    salut a tous

    je suis entrain de faire une application en Delphi 7, et cette dernière comporte une conversion de chiffre en valeur '-' moins exemple : " -12500 "
    j'ai pu trouver cette fonction sauf qu'elle ne prend pas les valeur en moins
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
     
     
    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;

    y'a t-il quelqu'un qui peut m'aider a ajouter la condition d'un chiffre en mois avec cette procédure
    merci a vous

  2. #2
    Modérateur

    Homme Profil pro
    Ingénieur retraité
    Inscrit en
    Octobre 2005
    Messages
    2 396
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Rhône (Rhône Alpes)

    Informations professionnelles :
    Activité : Ingénieur retraité

    Informations forums :
    Inscription : Octobre 2005
    Messages : 2 396
    Points : 3 263
    Points
    3 263
    Par défaut
    Bonjour,

    j'ai pu trouver cette fonction sauf qu'elle ne prend pas les valeur en moins
    Bin si Resultat:=Convert_ch_let(chiffre: real; SM: String): string;
    et if chiffre<0.0 then Resultat:='Moins '+Resultat.

    A+.
    N'oubliez pas de consulter les FAQ Delphi et les cours et tutoriels Delphi

  3. #3
    Membre du Club
    Inscrit en
    Mai 2008
    Messages
    89
    Détails du profil
    Informations forums :
    Inscription : Mai 2008
    Messages : 89
    Points : 49
    Points
    49
    Par défaut
    Citation Envoyé par Gilbert Geyer Voir le message
    Bonjour,



    Bin si Resultat:=Convert_ch_let(chiffre: real; SM: String): string;
    et if chiffre<0.0 then Resultat:='Moins '+Resultat.

    A+.
    ou dois-je mettre ce code
    voulez vous bien me faire éclaircire

    merci

  4. #4
    Expert confirmé
    Avatar de popo
    Homme Profil pro
    Analyste programmeur Delphi / C#
    Inscrit en
    Mars 2005
    Messages
    2 674
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Rhône (Rhône Alpes)

    Informations professionnelles :
    Activité : Analyste programmeur Delphi / C#
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Mars 2005
    Messages : 2 674
    Points : 5 259
    Points
    5 259
    Par défaut
    Où tu dois mettre ce code ?

    Je veux bien qu'on soit débutant, mais là tu abuses un peu !!!

    Mais aujourd'hui je suis dans un bon jour.
    Code Delphi : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    function ChiffreVersLettre(Const Chiffre : real; SymboleMonetaire : String = 'euros') : String;
    var Negatif : Boolean;
         ValeurAbsolue : Real;
    begin
       Negatif := (chiffre < 0);
       ValeurAbsolue:= Abs(Chiffre);
       Result := Convert_ch_let(ValeurAbsolue, SymboleMonetaire );
       if (Negatif) then
          Result := 'Moins ' + Result;
    end;

  5. #5
    Membre expert
    Avatar de Charly910
    Homme Profil pro
    Ingénieur TP
    Inscrit en
    Décembre 2006
    Messages
    2 345
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Ingénieur TP
    Secteur : Bâtiment Travaux Publics

    Informations forums :
    Inscription : Décembre 2006
    Messages : 2 345
    Points : 3 123
    Points
    3 123
    Par défaut
    Bonjour,

    sinon voici une version de ta fonction qui traite aussi les nombres négatifs

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    { ======================================================================== }
    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;
        Decimales : Real ;
        Negatif : Boolean ;
     
    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
    Negatif := False ;
    If (Chiffre <0 ) Then
      Begin
        Negatif := True ;
        Chiffre := -Chiffre ;
      End ;
    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;
    SM := Trim(SM) ;
    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
       Else
       Begin
       ch_r := Uppercase(copy(ch_r,2,1)) + copy(ch_r,3,length(ch_r) - 2) ;
       if frac(chiffre) > 0 then
          Begin
            Decimales := Round(frac(chiffre)*100) ;
            ch_r := ch_r + ' virgule ' + LowerCase(Convert_ch_let(Decimales, '')) ;
          End ;
       End ;
    If Negatif Then ch_r := 'Moins '+ LowerCase(ch_r) ;
    Convert_ch_let := ch_r;
    End;
    A+
    Charly

  6. #6
    Membre expert
    Avatar de Charly910
    Homme Profil pro
    Ingénieur TP
    Inscrit en
    Décembre 2006
    Messages
    2 345
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Ingénieur TP
    Secteur : Bâtiment Travaux Publics

    Informations forums :
    Inscription : Décembre 2006
    Messages : 2 345
    Points : 3 123
    Points
    3 123
    Par défaut
    Bonjour,
    petit soucis dans ta fonction de conversion : elle ne suit pas les règles de l'orthographe pour cent et vingt. Par exemple, elle renvoie cinq cent et non cinq cents ou quatre-vingt et non quatre-vingts.

    idem pour deux millions qui doit prendre un s.

    C 'est un peu compliqué à faire, mais ce serait intéressant de compléter cette fonction.

    A+
    Charly

  7. #7
    Membre expert
    Avatar de Charly910
    Homme Profil pro
    Ingénieur TP
    Inscrit en
    Décembre 2006
    Messages
    2 345
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Ingénieur TP
    Secteur : Bâtiment Travaux Publics

    Informations forums :
    Inscription : Décembre 2006
    Messages : 2 345
    Points : 3 123
    Points
    3 123
    Par défaut
    Regarde dans la FAQ, tu as une fonction qui semble bien meilleure et qui traite les nombres négatifs :

    FAQ Delphi

    je vais tester ...

    A+
    Charly

  8. #8
    Membre expert
    Avatar de Charly910
    Homme Profil pro
    Ingénieur TP
    Inscrit en
    Décembre 2006
    Messages
    2 345
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Ingénieur TP
    Secteur : Bâtiment Travaux Publics

    Informations forums :
    Inscription : Décembre 2006
    Messages : 2 345
    Points : 3 123
    Points
    3 123
    Par défaut
    Bonjour,

    voici la fonction de la FAQ modifiée pour prendre un réel à la place d'un entier :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    Function NombreEnLettres(Valeur : Real; SM : String = '' ; France : Boolean = True ) : string;
    // Conversion d'un réel à 2 décimales en lettre avec symbole monétaire en option
    //  Valeur : nombre à convertir
    //  SM     : symbole monétaire
    //  France : langue française, sinon belge
    Const
      unite     : array[1..19] of string = ('un','deux','trois','quatre','cinq','six',
                                            'sept','huit','neuf','dix','onze','douze',
                                            'treize','quatorze','quinze','seize',
                                            'dix sept','dix huit','dix neuf');
      dizaineF  : array[2.. 9] of string = ('vingt','trente','quarante','cinquante',
                                           'soixante','','quatre-vingt','');
      dizaineB  : array[2.. 9] of string = ('vingt','trente','quarante','cinquante',
                                           'soixante','septante','octante','nonante');
      coefs     : array[0.. 3] of string = ('cent','mille','million','milliard');
    Var
      temp      : String;
      c, d, u   : Integer;
      coef      : Integer;
      i         : Integer;
      neg       : boolean;
      n         : Integer ;
      Decimales : Real ;
      Centimes  : String ;
    Begin
      result := '';
     
     
      // Cas particulier de zéro
      If Valeur = 0 then
      begin
        result := ' zero';
        exit;
      end;
     
      // Mémorisation du signe
      neg := Valeur < 0;
      If neg then Valeur := -Valeur;
     
      n         := Trunc(Valeur) ;
      decimales := Round(Frac(Valeur) * 100) ;   // 2 premières décimales
      coef      := 0 ;
      Repeat
        // Récupération de l'unité du bloc de trois chiffres en cours
        u:=n mod 10; n:=n div 10;
        // Récupération de la dizaine du bloc de trois chiffres en cours
        d:=n mod 10; n:=n div 10;
     
        // Traitement des dizaines
        temp := '';
        // Passage sur la dizaine inférieure pour 10 à 19
        // et pour 70-79 90-99 dans le cas de la France
        If (d=1) Or ((d in [7,9])And France) then
        begin
          Dec(d);
          Inc(u,10);
        end;
        if d > 1 then
        begin
          If France Then
          Begin
            temp := ' ' + DizaineF[d];
            // Ajout du cas particulier de 'et' entre la dizaine et 1
            if (d < 8) and ((u = 1) or (u = 11)) then
              temp := temp + ' et';
          End Else
          Begin
            temp := ' ' + DizaineB[d];
            // Ajout du cas particulier de 'et' entre la dizaine et 1
            if (u = 1) then
              temp := temp + ' et';
          End;
        end;
        // ajout du texte de l'unité
        if u > 0 then temp := temp + ' ' + unite[u];
        // ajout du 's' à Quatre-vingt si rien ne suit
        If (result = '') and (d = 8) and (u = 0) and France then result := 's';
        result := temp + result;
     
        // Récupération de la centaine du bloc de trois chiffres en cours
        c := n mod 10; n := n div 10; {Récupère centaine}
        If c > 0 then
        begin
          temp := '';
          if c > 1 then temp := ' ' + unite[c] + temp;
          temp := temp + ' ' + coefs[0];
          // Traitement du cas particulier du 's' à cent si rien ne suit
          If (result = '') and (c > 1) then result := 's';
          result := temp + result;
        end;
     
        // Traitement du prochain groupe de 3 chiffres
        if n > 0 then
        begin
          Inc(coef);
          I := n mod 1000;
          If (i > 1) and (coef > 1) then result := 's' + result;
          If i > 0 then result := ' ' + coefs[coef] + result;
          // Traitement du cas particulier 'mille' ( non pas 'un mille' )
          If (i = 1) and (coef = 1) then Dec(n);
        end;
      until n = 0;
     
      SM := Trim(SM) ;
      If (SM <> '') Then
        Begin
          result := Trim(result)+ ' '+ SM;
          If (Decimales > 0 ) Then
             Begin
               Centimes := ' centimes' ;
               If (Trunc(Decimales) = 1) Then  Centimes := ' centime' ;
                Result := Result + ' et '+IntToStr(Trunc(Decimales)) + Centimes ;
             End ;
        End
      Else
        Begin
          result := Trim(result);
          If (Decimales > 0 ) Then
             Begin
                Result := Result + ' virgule '+NombreEnLettres(Decimales) ;
             End ;
        End ;
     
      // Ajout du signe en cas de besoin
      if neg then result := 'moins ' + result;
    End;
    resterait à traiter le cas ou ce n'est pas des centimes, mais autre chose ...

    A+
    Charly

  9. #9
    Membre habitué
    Inscrit en
    Mars 2007
    Messages
    191
    Détails du profil
    Informations forums :
    Inscription : Mars 2007
    Messages : 191
    Points : 132
    Points
    132
    Par défaut
    Charly910

    et si en veut ecrire cinq cent virgule ZERO Trois ça donne cinq cent virgule trois ???

  10. #10
    Membre expert
    Avatar de Charly910
    Homme Profil pro
    Ingénieur TP
    Inscrit en
    Décembre 2006
    Messages
    2 345
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Ingénieur TP
    Secteur : Bâtiment Travaux Publics

    Informations forums :
    Inscription : Décembre 2006
    Messages : 2 345
    Points : 3 123
    Points
    3 123
    Par défaut
    Oui, c'est vrai.
    il faut faire un cas particulier si Frac < 10.

    A ajouter au traitement ...

    si j'ai le temps je le ferai


    A+
    Charly

  11. #11
    Membre expert
    Avatar de Charly910
    Homme Profil pro
    Ingénieur TP
    Inscrit en
    Décembre 2006
    Messages
    2 345
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Ingénieur TP
    Secteur : Bâtiment Travaux Publics

    Informations forums :
    Inscription : Décembre 2006
    Messages : 2 345
    Points : 3 123
    Points
    3 123
    Par défaut
    Bon, voici la fonction corrigée :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    Function NombreEnLettres(Valeur : Real; SM : String = '' ; SSM : String = '' ; France : Boolean = True ) : string;
    // Conversion d'un réel à 2 décimales en lettre avec symbole monétaire en option
    //  Valeur : nombre à convertir
    //  SM     : symbole monétaire (Exemple € )
    //  SSM     : sous symbole monétaire (Exemple le centime par défaut)
    //  France : langue française, sinon belge
    Const
      unite     : array[1..19] of string = ('un','deux','trois','quatre','cinq','six',
                                            'sept','huit','neuf','dix','onze','douze',
                                            'treize','quatorze','quinze','seize',
                                            'dix sept','dix huit','dix neuf');
      dizaineF  : array[2.. 9] of string = ('vingt','trente','quarante','cinquante',
                                           'soixante','','quatre-vingt','');
      dizaineB  : array[2.. 9] of string = ('vingt','trente','quarante','cinquante',
                                           'soixante','septante','octante','nonante');
      coefs     : array[0.. 3] of string = ('cent','mille','million','milliard');
    Var
      temp      : String;
      c, d, u   : Integer;
      coef      : Integer;
      i         : Integer;
      neg       : boolean;
      n         : Integer ;
      Decimales : Real ;
      Centimes  : String ;
      Penny     : Boolean ;
    Begin
      result := '';
     
     
      // Cas particulier de zéro
      If Valeur = 0 then
      begin
        result := ' zero '+SM;
        exit;
      end;
     
      // Mémorisation du signe
      neg := Valeur < 0;
      If neg then Valeur := -Valeur;
     
      // Traitement des décimales
      n         := Trunc(Valeur) ;
      decimales := Round(Frac(Valeur) * 100) ;   // 2 premières décimales
     
      // Traitement de la sous unité monétaire
      SSM := LowerCase(Trim(SSM)) ;
        // Cas spécial du penny
      Penny := False ;
      if ((SSM = 'penny') or (SSM = 'pence')) Then Penny := True ;
      If (SSM = '') Then SSM := 'centime'   //  cas général si SSM oublié : le centime
      Else
        Begin
          if (SSM[Length(SSM)] = 's') then
            begin
              Delete(SSM, Length(SSM), 1) ;    // on met au singulier
            end;
        End ;
      // Traitement du nombre
      coef      := 0 ;
      Repeat
        // Récupération de l'unité du bloc de trois chiffres en cours
        u:=n mod 10; n:=n div 10;
        // Récupération de la dizaine du bloc de trois chiffres en cours
        d:=n mod 10; n:=n div 10;
     
        // Traitement des dizaines
        temp := '';
        // Passage sur la dizaine inférieure pour 10 à 19
        // et pour 70-79 90-99 dans le cas de la France
        If (d=1) Or ((d in [7,9])And France) then
        begin
          Dec(d);
          Inc(u,10);
        end;
        if d > 1 then
        begin
          If France Then
          Begin
            temp := ' ' + DizaineF[d];
            // Ajout du cas particulier de 'et' entre la dizaine et 1
            if (d < 8) and ((u = 1) or (u = 11)) then
              temp := temp + ' et';
          End Else
          Begin
            temp := ' ' + DizaineB[d];
            // Ajout du cas particulier de 'et' entre la dizaine et 1
            if (u = 1) then
              temp := temp + ' et';
          End;
        end;
        // ajout du texte de l'unité
        if u > 0 then temp := temp + ' ' + unite[u];
        // ajout du 's' à Quatre-vingt si rien ne suit
        If (result = '') and (d = 8) and (u = 0) and France then result := 's';
        result := temp + result;
     
        // Récupération de la centaine du bloc de trois chiffres en cours
        c := n mod 10; n := n div 10; {Récupère centaine}
        If c > 0 then
        begin
          temp := '';
          if c > 1 then temp := ' ' + unite[c] + temp;
          temp := temp + ' ' + coefs[0];
          // Traitement du cas particulier du 's' à cent si rien ne suit
          If (result = '') and (c > 1) then result := 's';
          result := temp + result;
        end;
     
        // Traitement du prochain groupe de 3 chiffres
        if n > 0 then
        begin
          Inc(coef);
          I := n mod 1000;
          If (i > 1) and (coef > 1) then result := 's' + result;
          If i > 0 then result := ' ' + coefs[coef] + result;
          // Traitement du cas particulier 'mille' ( non pas 'un mille' )
          If (i = 1) and (coef = 1) then Dec(n);
        end;
      until n = 0;
     
     
      //  *******************  Traiter le cas du penny = True
      SM := Trim(SM) ;
      If (SM <> '') Then
        Begin
          result := Trim(result)+ ' '+ SM;
          If (Decimales > 0 ) Then
             Begin
               Centimes := ' '+ SSM ;
               If (Trunc(Decimales) > 1) Then  Centimes := centimes + 's' ;
                Result := Result + ' et '+IntToStr(Trunc(Decimales)) + Centimes ;
             End ;
        End
      Else
        Begin
          result := Trim(result);
          If (Decimales > 0 ) Then
             Begin
                If (Decimales < 10) Then
                  Result := Result + ' virgule zéro '+NombreEnLettres(Decimales)
                Else
                  Result := Result + ' virgule '+NombreEnLettres(Decimales) ;
             End ;
        End ;
     
      // Ajout du signe en cas de besoin
      if neg then result := 'moins ' + result;
    End;
    A+
    Charly

  12. #12
    Rédacteur/Modérateur

    Avatar de SergioMaster
    Homme Profil pro
    Développeur informatique retraité
    Inscrit en
    Janvier 2007
    Messages
    15 042
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 67
    Localisation : France, Loire Atlantique (Pays de la Loire)

    Informations professionnelles :
    Activité : Développeur informatique retraité
    Secteur : Industrie

    Informations forums :
    Inscription : Janvier 2007
    Messages : 15 042
    Points : 40 952
    Points
    40 952
    Billets dans le blog
    62
    Par défaut
    Bonjour
    Citation Envoyé par MIWAN Voir le message
    et si on veut écrire cinq cent virgule ZERO Trois ça donne cinq cent virgule trois ???
    s'il s'agit de données monétaires cinq cent virgule trois c'est tout à fait correct en français,AMHA mettre un zéro est inutile et une faute de français
    d'ailleurs on écrirait : cinq cents euros et trois centimes plutôt que cinq cent virgule trois euro

    500,03 et 500,30 (cinq cent virgule trente) ne peuvent être confondus

    même s'il s'agit d'autres unités il y a souvent des sous-entendus
    par exemple 5,3 kg -> "Cinq kilos trois" est l'équivalent de "5 kilos trois cents grammes"

    @Charly on peut effectivement traiter le cas particulier comme tu l'as proposé mais toujours à mon avis c'est inutile.
    MVP Embarcadero
    Delphi installés : D3,D7,D2010,XE4,XE7,D10 (Rio, Sidney), D11 (Alexandria), D12 (Athènes)
    SGBD : Firebird 2.5, 3, SQLite
    générateurs États : FastReport, Rave, QuickReport
    OS : Window Vista, Windows 10, Windows 11, Ubuntu, Androïd

  13. #13
    Membre expert
    Avatar de Charly910
    Homme Profil pro
    Ingénieur TP
    Inscrit en
    Décembre 2006
    Messages
    2 345
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Ingénieur TP
    Secteur : Bâtiment Travaux Publics

    Informations forums :
    Inscription : Décembre 2006
    Messages : 2 345
    Points : 3 123
    Points
    3 123
    Par défaut
    Bonjour Serge,

    ce n'est pas inutile, car la fonction issue de la FAQ que j'ai modifié traite 2 cas :

    pour 5,03 :

    s'il y a un symbole monétaire (exemple €) ==> cinq euros et 3 centimes
    sans symbole monétaire ==> cinq virgule zéro trois

    j'ai ajouté aussi un sous symbole monétaire facultatif (par défaut le centime)

    A+
    Charly

  14. #14
    Membre habitué
    Inscrit en
    Mars 2007
    Messages
    191
    Détails du profil
    Informations forums :
    Inscription : Mars 2007
    Messages : 191
    Points : 132
    Points
    132
    Par défaut
    Bonsoir a tous

    Merci beaucoup Charly910 pour les ajouts

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. [AC-2003] Convertir chiffres en lettres avec changement de la devise
    Par sbobo dans le forum VBA Access
    Réponses: 1
    Dernier message: 06/06/2013, 17h54
  2. Convertir des chiffres en lettres avec décimale
    Par informatiquedm dans le forum VB.NET
    Réponses: 1
    Dernier message: 17/08/2011, 20h43
  3. possible convertir un chiffre en lettre avec builder ?
    Par devlopassion dans le forum C++Builder
    Réponses: 8
    Dernier message: 11/09/2006, 17h24
  4. [VBA-E]Convertir un motant qui est en chiffre, en lettres
    Par Elstak dans le forum Macros et VBA Excel
    Réponses: 6
    Dernier message: 15/05/2006, 09h12
  5. [VB]Convertir chiffres en lettres
    Par RichY dans le forum VB 6 et antérieur
    Réponses: 6
    Dernier message: 16/02/2006, 14h43

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo