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

Langage Delphi Discussion :

Convertir un chiffre en lettres


Sujet :

Langage Delphi

  1. #1
    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 Convertir un chiffre en lettres
    bonjour
    j'ai bien cherché une bonne solution pour convertir un chiffre en lettres dans le forum et et la méthode de NABIL74 : convertir-chiffre-lettre-arabe
    nécessite quelques modifications .
    j'ai fait un autre recherche et je suis tombé sur cette Unite en anglais
    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
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
     
     
    unit NumberToWords;
    {-----------------------------------------------------------------------------
     Unit Name: NumberToWords
     Author:    Dave Keighan
     Date:      29-Jan-2006
     Purpose:   Provide Number To Words functionality.
     History:   29 Jan 2006 originally written.
     
     **** IMPORTANT ****
     1) You are free to use this code within your own applications, but you are
     expressly forbidden from selling or otherwise distributing this source code
     including inclusion of the code in a unit, module or component where this code
     would reasonably be considered to be a defining element of the unit or
     component without prior written consent.
     2) You must include this header in all instances of the code.
     3) You must ensure that you have read, understand and have modified the code as
     required for your use. This code is provided "as is" and the author makes no
     representations or warranties, express or implied. The user is solely
     responsible for the use and all results of the code provided herein.
    -----------------------------------------------------------------------------}
    interface
     
    uses
      Dialogs,
      SysUtils, Classes;
     
    const
      Ones: array[0..9] of string = ('zero','un ', 'deux ', 'trois ', 'quatre ', 'cinq ', 'six ', 'sept ', 'huit ', 'neuf ');
      Teens: array[10..19] of string = ('dix ', 'onze ', 'douze ', 'treize ', 'quatorze ', 'quinze ', 'seize ', 'dix-sept ', 'dix-huit ', 'dix-neuf ');
      Tens: array[2..9] of string = ('vingt ', 'trente ', 'quarante ', 'cinquante ', 'soixante ', 'soixante-dix ', 'quatre-vingt ', 'quatre-vingt-dix ');
      Suffix: array[0..5] of string = ('cent ', 'mille, ', 'million, ', 'milliard, ', 'trillion, ', 'quadrillion, ');
     
    var
      fs : TFormatSettings;
     
    function ConvertToWords(const Number: double; UseCurrency: Boolean = True):
      string;
     
    implementation
     
    {-----------------------------------------------------------------------------
      Procedure: ConvertToWords
      Author:    Dave Keighan
      Date:      24-Jan-2006
      Arguments: const Number: double UseCurrency: Boolean
      Result:    string
    -----------------------------------------------------------------------------}
    {Note: This function uses the American "Short Scale" format for Names of large
     numbers See: http://en.wikipedia.org/wiki/Names_of_large_numbers .
     It's very simple to edit the code to accommodate your locale and system.
     
     The program gets two variables from the user.
     The first is: Number a double that represents the number to be formatted.
     Second is: UseCurrency a boolean value to get Currency formatting output.
    }
     
    function ConvertToWords(const Number: double; UseCurrency: Boolean): string;
    {
    There is one inline routine that occur before the main processing code:
     DoHundreds - takes a three character shortstring and returns a value.
    }
     
      function DoHundreds(const NumStr: string): string;
      var
        iCount: integer;
        iCurNum: integer;
        bFinished: Boolean;
        sLocNum: string;
      begin
        sLocNum := NumStr;
        bFinished := False;
        //buffer the string with 0s depending on length.
        case Length(sLocNum) of
          1: sLocNum := '00' + sLocNum;
          2: sLocNum := '0' + sLocNum;
        end;
        //Process the string.
        for iCount := 1 to 3 do
          begin
            iCurNum := StrToInt(sLocNum[iCount]);
            if iCurNum > 0 then // it needs to be processed.
              case iCount of
                1:
                  begin
                    Result := Result + Ones[iCurNum] + Suffix[0];
                    //append "and" if the value of last to digits is greated than 0.
                    if strtoint(copy(sLocNum, 2, 2)) > 0 then
                      Result := Result + 'et ';
                  end;
                2:
                  begin
                    if iCurNum = 1 then
                      begin
                        iCurNum := strtoint(copy(sLocNum, 2, 2));
                        Result := Result + Teens[iCurNum];
                        bFinished := True;
                      end
                    else
                      Result := Result + Tens[iCurNum];
                  end;
                3: if (not bFinished) then
                    Result := Result + Ones[iCurNum];
              end;
          end;
      end;
     
      // This is where the main routine actually starts.
    var
      sIntValue: string; //to hold the integer value.
      sDecValue: string; //value of the decimal value.
      sSectionVal: string; //the hundreds section to be processed.
      slValSections: TStringList; //array of the hundreds sections.
      iCount: integer; //counter variable.
      bIsNegative: boolean;
    begin
      //Process the integer section first.
     
      bIsNegative := False;
      if Number < 0 then
        bIsNegative := True;
     
      //Extract the integer and fractional parts of Number as strings.
      //Use Try because FormatFloat, as used, will blow up if Lenght(sIntValue) >= 19.
      try
        sIntValue := FormatFloat('#,###', trunc(abs(Number)));
        sDecValue := Copy(FormatFloat('.#########', frac(abs(Number))), 2);
        if (Pos('E', sIntValue) > 0) then //the number is too big.
        begin
          Result := 'ERROR:';
          exit;
        end;
      except
        Result := 'ERROR:';
        exit;
      end;
     
      if (Length(sIntValue) <= 3) and (Length(sIntValue) > 0) then Result := DoHundreds(sIntValue)
      else if Length(sIntValue) = 0 then Result := ''
      else
        begin
          slValSections := TStringList.Create;
          //load the values, in groups of 100 using the comma positions.
          //slValSections.CommaText := sIntValue;
          slValSections.Delimiter := fs.ThousandSeparator;
          slValSections.DelimitedText := sIntValue;
     
          //swap the order of the numbers in the StringList.
          for iCount := 1 to slValSections.Count - 1 do
            slValSections.Move(iCount, 0);
          //process all the Integer values.
          for iCount := 0 to slValSections.Count - 1 do
            begin
              sSectionVal := '';
              sSectionVal := DoHundreds(slValSections[iCount]);
              if iCount > 0 then
                Result := sSectionVal + Suffix[iCount] + Result
              else
                Result := sSectionVal
            end;
          FreeAndNil(slValSections);
        end;
     
      //Remove trailing commas if there are any.
      if LastDelimiter(fs.DecimalSeparator, Result) = (length(Result) - 1) then  Delete(Result, Length(Result) - 1, 1);
     
      //Process the decimal section of the Number.
      if (Length(sDecValue) > 0) and UseCurrency then
        begin
          if Result > '' then //there was an integer value.
            Result := Result + 'Dinars et ';
          if (Length(sDecValue) = 1) then
            sDecValue := sDecValue + '0';
          Result := Result + DoHundreds(copy(sDecValue, 1, 2)) + 'centimess.';
        end
      else if UseCurrency then
        begin
          if Result > '' then //there was an integer value.
            Result := Result + 'dinars.'
        end
      else if (Length(sDecValue) > 0) then
        begin
          Result := Result + 'decimal ';
          for iCount := 1 to Length(sDecValue) do
            Result := Result + Ones[strtoint(sDecValue[iCount])];
        end;
     
      //Add the negative indicator if needed.
      if UseCurrency and bIsNegative then
        Result := 'minus ' + Result
      else if bIsNegative then
        Result := 'negative ' + Result;
     
      { TODO : Add result formatting at this point.
        This will require changing the function call to allow format selection. }
    end;
     
    initialization
      GetLocaleFormatSettings(0, fs);
     
    finalization
     
    end.
    le problème :
    Quand je tape Exp :170 il me renvoie UN cent soixante-dix
    et j'ai fait plusieurs tentatives pour enlever le UN hélas je ne parvient a le faire .

    reconnaissant a toutes aide merci

  2. #2
    Rédacteur/Modérateur

    Avatar de Roland Chastain
    Homme Profil pro
    Enseignant
    Inscrit en
    Décembre 2011
    Messages
    4 072
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 50
    Localisation : France, Moselle (Lorraine)

    Informations professionnelles :
    Activité : Enseignant

    Informations forums :
    Inscription : Décembre 2011
    Messages : 4 072
    Points : 15 462
    Points
    15 462
    Billets dans le blog
    9
    Mon site personnel consacré à MSEide+MSEgui : msegui.net

  3. #3
    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
    merci Roland d'êtres si rapide
    j'ai bien étudié ces cas dans les liens que vous avez fourni mais ils ne me satisfaisait pas car ils ne prends pas les chiffres(nombres) flottants en considération .

  4. #4
    Rédacteur/Modérateur

    Avatar de SergioMaster
    Homme Profil pro
    Développeur informatique retraité
    Inscrit en
    Janvier 2007
    Messages
    15 038
    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 038
    Points : 40 943
    Points
    40 943
    Billets dans le blog
    62
    Par défaut
    Citation Envoyé par MIWAN Voir le message
    j'ai bien étudié ces cas dans les liens que vous avez fourni mais ils ne me satisfaisait pas car ils ne prends pas les chiffres(nombres) flottants en considération .
    pas si bien que ça, car pour un nombre flottant il suffit de prendre les deux parties (entière et décimale) et de les convertir séparément et d'ajouter les deux résultats
    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

  5. #5
    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
    Bonjour Maistro

    pas si bien que ça, car pour un nombre flottant il suffit de prendre les deux parties (entière et décimale) et de les convertir séparément et d'ajouter les deux résultats
    Je voulais dire que je suis nul en tableau ().

    Et l'unité NumberToWords me convient très bien sauf ce UN qui m'agace car je veux plus tard la convertir en arabe. Mes excuses si je me suis mal exprimé.

  6. #6
    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
    bonjours
    je tiens a ré-excuser pour les postes antérieurs
    le problème été réglé après formatage du Pc

    je tiens aussi a dire que les procédures de pop étaient impeccables .

    Merci encore pour votre aides.

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

Discussions similaires

  1. Réponses: 2
    Dernier message: 17/04/2013, 10h50
  2. Besoin d'aide, attribution d'une lettre à un nombre
    Par trentks95 dans le forum VB 6 et antérieur
    Réponses: 8
    Dernier message: 29/03/2013, 14h42
  3. besoin d'aide lettre anglais
    Par lauramae dans le forum CV
    Réponses: 3
    Dernier message: 29/01/2009, 11h53
  4. [OpenOffice][Tableur] Besoin d'aide pour une formule: colorer fond cellule suivant un chiffre
    Par bennji dans le forum OpenOffice & LibreOffice
    Réponses: 2
    Dernier message: 26/01/2009, 13h47

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