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

C# Discussion :

Conversion Nombre Vers Texte


Sujet :

C#

  1. #1
    Membre très actif
    Homme Profil pro
    Autodidacte
    Inscrit en
    Mars 2016
    Messages
    154
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Congo-Brazzaville

    Informations professionnelles :
    Activité : Autodidacte
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Mars 2016
    Messages : 154
    Par défaut Conversion Nombre Vers Texte
    Bonjour,
    Je me suis resolu à écrire une classe de conversion de Nombres entiers vers le texte et elle fonctionne relativement bien.
    seulement elle est limitée à 06 chiffres mais peut facilement etre etendue pour qui le veut.
    Le code est un peut desordonné. Il faut aussi se mefier des (0) de debut de chiffres et des (,) qui ne sont pas gérés.

    voici le code si ça peut aider quelqu'un.
    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
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305
    306
    307
    308
    309
    310
    311
    312
    313
    314
    315
    316
    317
    318
    319
    320
    321
    322
    323
    324
    325
    326
    327
    328
    329
    330
    331
    332
    333
    334
    335
    336
    337
    338
    339
    340
    341
    342
    343
    344
    345
    346
    347
    348
    349
    350
    351
    352
    353
    354
    355
    356
    357
    358
    359
    360
    361
    362
    363
    364
    365
    366
    367
    368
    369
    370
    371
    372
    373
    374
    375
    376
    377
    378
    379
    380
    381
    382
    383
    384
    385
    386
    387
    388
    389
    390
    391
    392
    393
    394
    395
    396
    397
    398
    399
    400
    401
    402
    403
    404
    405
    406
    407
    408
    409
    410
    411
    412
    413
    414
    415
    416
    417
    418
    419
    420
    421
    422
    423
    424
    425
    426
    427
    428
    429
    430
    431
    432
    433
    434
    435
    436
    437
    438
    439
    440
    441
    442
    443
    444
    445
    446
    447
    448
    449
    450
    451
    452
    453
    454
    455
    456
    457
    458
    459
    460
    public  class NombreVersTexte 
        {
           public static string NumberToText(string Nomb)
           {
     
               string[] NumbList ={
    "1:un", "2:deux", "3:trois","4:quatre", "5:cinq", "6:six", "7:sept", "8:huit", "9:neuf","10:dix","11:onze", "12:douze",
    "13:treize", "14:quatorze", "15:quinze", "16:seize", "17:dix-sept", "18:dix-huit", "19:dix-neuf", "20:vingt",
    "30:trente","40:quarante", "50:cinquante", "60:soixante", "70:soixante-dix", "80:quatre-vint", "90:quatre-vingt-dix",
    "100:cent", "1000:mille", "1000000:un million", "1000000000:un milliard"
     
     
                                   };
               string Resultat = "";
               string[] champs = null;
               char[] séparateurs = new char[] { ':' };
               Dictionary<int, string> dico = new Dictionary<int, string>();
               for (int i = 0; i < NumbList.Length; i++)
               {
                   champs = NumbList[i].Split(séparateurs);
                   //dico[champs[0]]= int.Parse(champs[1]);
                   dico.Add(int.Parse(champs[0]), champs[1]);
               }
     
     
     
               Nomb = Nomb.Trim();
               string[] Num = Nomb.Split(null);
               string Numb2 = System.String.Join(null, Num);
               // string champ;
               //int pd = val.Length;
     
     
               int pd = Numb2.Length;
     
               try
               {
                   //def nbre separateurs de milliers
                   string[] ListNum = null;
                   if (pd > 2)
                   {
                       do
                       {
                           pd--;
                           pd--;
                           pd--;
                           //Nomb = n .Insert(pd, " ");
                           Numb2 = Numb2.Insert(pd, " ");
     
                       } while (pd > 3);
                       Numb2 = Numb2.Trim();
                       ListNum = Numb2.Split(null);
     
     
     
                       //char[] ListNum = Numb2.ToCharArray();
                       int t = ListNum.Length;
     
     
     
                       // def nbre de chiffre de la première colonne
                       char[] ListNumb22 = ListNum[0].ToCharArray();
                       int p2 = ListNumb22.Length;
                       string value = "";
     
                       switch (t)
                       {
                           case 1:
                               switch (p2)
                               {
                                   case 1:
     
                                       dico.TryGetValue(int.Parse(Convert.ToString(ListNumb22[0])), out value);
                                       if (dico.TryGetValue(int.Parse(Convert.ToString(ListNumb22[0])), out value))
                                       {
                                           if (int.Parse(Convert.ToString(ListNumb22[0])) == 1)
                                           { Resultat = "Cent " + " " + Resultat; }
                                           else if (int.Parse(Convert.ToString(ListNumb22[0])) == 0)
                                           {
                                               Resultat = Resultat;
                                           }
                                           else
                                           {
                                               Resultat = value + " " + "Cent " + " " + Resultat;
                                           }
                                       }
                                       break;
     
                                   case 2:
                                       //MessageBox.Show("11");
                                       //string value = "";
                                       dico.TryGetValue(int.Parse(Convert.ToString(ListNumb22[1] * 10)), out value);
                                       if (dico.TryGetValue(int.Parse(Convert.ToString(ListNumb22[1])) * 10, out value))
                                       {
                                           if (int.Parse(Convert.ToString(ListNumb22[1])) == 1)
                                           {
                                               string pad = Convert.ToString(ListNumb22[1]) + Convert.ToString(ListNumb22[2]);
                                               if (dico.TryGetValue(int.Parse(pad), out value))
                                               {
                                                   Resultat = value;
                                               }
                                           }
                                           else if (int.Parse(Convert.ToString(ListNumb22[1])) == 0)
                                           {
                                               Resultat = Resultat;
                                           }
                                           else if (int.Parse(Convert.ToString(ListNumb22[1])) == 7)
                                           {
                                               if (dico.TryGetValue(int.Parse(Convert.ToString(ListNumb22[2])) + 10, out value))
                                               {
                                                   Resultat = "Soixante" + " " + value;
                                               }
                                           }
                                           else if (int.Parse(Convert.ToString(ListNumb22[1])) == 9)
                                           {
                                               if (dico.TryGetValue(int.Parse(Convert.ToString(ListNumb22[2])) + 10, out value))
                                               {
                                                   Resultat = "Quatre vingt" + " " + value;
                                               }
                                           }
                                           else
                                           {
                                               Resultat = value + " " + Resultat;
                                           }
                                       }
                                       goto case 1;
                                   //break;
     
                                   case 3:
     
                                       dico.TryGetValue(int.Parse(Convert.ToString(ListNumb22[2])), out value);
                                       if (dico.TryGetValue(int.Parse(Convert.ToString(ListNumb22[2])), out value)) { Resultat = value; }
     
                                       goto case 2;
                                   //break;
                               }
     
                               break;
     
                           //cas de plus de 3 chiffres mais moins de 7
                           case 2:
                               char[] ListNumb33 = ListNum[1].ToCharArray();
                               int p3 = ListNumb33.Length;
                               string value21 = "";
                               string value22 = "";
     
                               if (int.Parse(Convert.ToString(ListNumb33[0])) != 0)
                               {
                                   if (int.Parse(Convert.ToString(ListNumb33[0])) != 1)
                                   {
     
                                       if (dico.TryGetValue(int.Parse(Convert.ToString(ListNumb33[0])), out value)) { Resultat = value + " " + "Cent"; }
                                       if (int.Parse(Convert.ToString(ListNumb33[1])) != 7)
                                       {
     
                                           if (int.Parse(Convert.ToString(ListNumb33[1])) != 9)
                                           {
                                               //string value21 = "";
                                               //string value22 = "";
                                               if (dico.TryGetValue(int.Parse(Convert.ToString(ListNumb33[1])) * 10, out value21)) { Resultat = Resultat + " " + value21; }
                                               if (dico.TryGetValue(int.Parse(Convert.ToString(ListNumb33[2])), out value22)) { Resultat = Resultat + " " + value22; }
                                           }
                                           else
                                           {
                                               if (dico.TryGetValue(int.Parse(Convert.ToString(ListNumb33[2])) + 10, out value22)) { Resultat = Resultat + "Quatre Vingt" + " " + value22; }
                                           }
                                       }
                                       else
                                       {
                                           if (dico.TryGetValue(int.Parse(Convert.ToString(ListNumb33[2])) + 10, out value22)) { Resultat = Resultat + "Soixante dix" + " " + value22; }
                                       }
                                   }
     
                                   else
                                   {
                                       if (dico.TryGetValue(int.Parse(Convert.ToString(ListNumb33[0])), out value)) { Resultat = "Cent"; }
                                   }
     
                               }
                               else
                               {
                                   if (int.Parse(Convert.ToString(ListNumb33[1])) != 7)
                                   {
     
                                       if (int.Parse(Convert.ToString(ListNumb33[1])) != 9)
                                       {
                                           //string value21 = "";
                                           //string value22 = "";
                                           if (dico.TryGetValue(int.Parse(Convert.ToString(ListNumb33[1])) * 10, out value21)) { Resultat = Resultat + " " + value21; }
                                           if (int.Parse(Convert.ToString(ListNumb33[2])) != 0)
                                           {
                                               if (dico.TryGetValue(int.Parse(Convert.ToString(ListNumb33[2])), out value22)) { Resultat = Resultat + " " + value22; }
                                           }
                                       }
                                       else
                                       {
                                           if (dico.TryGetValue(int.Parse(Convert.ToString(ListNumb33[2])) + 10, out value22)) { Resultat = Resultat + "Quatre Vingt" + " " + value22; }
                                       }
                                   }
                                   else
                                   {
                                       if (dico.TryGetValue(int.Parse(Convert.ToString(ListNumb33[2])) + 10, out value22)) { Resultat = Resultat + "Soixante" + " " + value22; }
                                   }
                               }
     
                               //cas colonne de milliers
                               switch (p2)
                               {
                                   case 1:
     
                                       dico.TryGetValue(int.Parse(Convert.ToString(ListNumb22[0])), out value);
                                       if (dico.TryGetValue(int.Parse(Convert.ToString(ListNumb22[0])), out value))
                                       {
                                           if (int.Parse(Convert.ToString(ListNumb22[0])) == 1)
                                           { Resultat = "Mille " + " " + Resultat; }
                                           else if (int.Parse(Convert.ToString(ListNumb22[0])) == 0)
                                           {
                                               Resultat = Resultat;
                                           }
                                           else
                                           {
                                               Resultat = value + " " + "Mille " + " " + Resultat;
                                           }
                                       }
                                       break;
     
                                   // Dizaines de milliers
                                   case 2:
     
                                       if (dico.TryGetValue(int.Parse(Convert.ToString(ListNumb22[0])) * 10, out value))
                                       {
                                           if (int.Parse(Convert.ToString(ListNumb22[0])) == 1)
                                           {
                                               string pad = Convert.ToString(ListNumb22[0]) + Convert.ToString(ListNumb22[1]);
                                               if (dico.TryGetValue(int.Parse(pad), out value))
                                               {
                                                   Resultat = value + " " + "Mille" + " " + Resultat;
                                               }
                                           }
                                           else if (int.Parse(Convert.ToString(ListNumb22[1])) == 0)
                                           {
                                               Resultat = value + " " + "Mille" + " " + Resultat;
                                           }
                                           else if (int.Parse(Convert.ToString(ListNumb22[0])) == 7)
                                           {
                                               if (dico.TryGetValue(int.Parse(Convert.ToString(ListNumb22[1])) + 10, out value))
                                               {
                                                   Resultat = "Soixante" + " " + value + " " + "Mille" + " " + Resultat;
                                               }
                                           }
                                           else if (int.Parse(Convert.ToString(ListNumb22[0])) == 9)
                                           {
                                               if (dico.TryGetValue(int.Parse(Convert.ToString(ListNumb22[1])) + 10, out value))
                                               {
                                                   Resultat = "Quatre vingt" + " " + value + " " + "Mille" + " " + Resultat;
                                               }
                                           }
                                           else
                                           {
                                               string val221 = "";
                                               if (dico.TryGetValue(int.Parse(Convert.ToString(ListNumb22[1])), out val221))
                                                   Resultat = value + " " + val221 + " " + "Mille" + " " + Resultat;
                                           }
                                       }
     
                                       break;
     
                                   //Centaines de milliers
                                   case 3:
                                       if (int.Parse(Convert.ToString(ListNumb22[0])) != 0)
                                       {
                                           string value4;
                                           if (dico.TryGetValue(int.Parse(Convert.ToString(ListNumb22[0])), out value4))
                                           {
                                               if (dico.TryGetValue(int.Parse(Convert.ToString(ListNumb22[1])) * 10, out value))
                                               {
                                                   if (int.Parse(Convert.ToString(ListNumb22[1])) == 1)
                                                   {
                                                       string pad = Convert.ToString(ListNumb22[1]) + Convert.ToString(ListNumb22[2]);
                                                       if (dico.TryGetValue(int.Parse(pad), out value))
                                                       {
                                                           Resultat = value4 + " " + "Cent" + " " + value + " " + "Mille" + " " + Resultat;
                                                       }
                                                   }
                                                   else if (int.Parse(Convert.ToString(ListNumb22[2])) == 0)
                                                   {
                                                       Resultat = value4 + " " + "Cent" + " " + value + " " + "Mille" + " " + Resultat;
                                                   }
                                                   else if (int.Parse(Convert.ToString(ListNumb22[1])) == 7)
                                                   {
                                                       if (dico.TryGetValue(int.Parse(Convert.ToString(ListNumb22[2])) + 10, out value))
                                                       {
                                                           Resultat = value4 + " " + "Cent" + " " + "Soixante" + " " + value + " " + "Mille" + " " + Resultat;
                                                       }
                                                   }
                                                   else if (int.Parse(Convert.ToString(ListNumb22[1])) == 9)
                                                   {
                                                       if (dico.TryGetValue(int.Parse(Convert.ToString(ListNumb22[2])) + 10, out value))
                                                       {
                                                           Resultat = value4 + " " + "Cent" + " " + "Quatre vingt" + " " + value + " " + "Mille" + " " + Resultat;
                                                       }
                                                   }
                                                   else
                                                   {
                                                       string val221 = "";
                                                       if (dico.TryGetValue(int.Parse(Convert.ToString(ListNumb22[2])), out val221))
                                                           Resultat = value4 + " " + "Cent" + " " + value + " " + val221 + " " + "Mille" + " " + Resultat;
                                                   }
                                               }
     
                                           }
                                       }
                                       else
                                       {
                                           //Gestion de zero dans la colonne des entaines de milliers
                                           if (dico.TryGetValue(int.Parse(Convert.ToString(ListNumb22[1])) * 10, out value))
                                           {
                                               if (int.Parse(Convert.ToString(ListNumb22[1])) == 1)
                                               {
                                                   string pad = Convert.ToString(ListNumb22[1]) + Convert.ToString(ListNumb22[2]);
                                                   if (dico.TryGetValue(int.Parse(pad), out value))
                                                   {
                                                       Resultat = value + " " + "Mille" + " " + Resultat;
                                                   }
                                               }
                                               else if (int.Parse(Convert.ToString(ListNumb22[2])) == 0)
                                               {
                                                   Resultat = value + " " + "Mille" + " " + Resultat;
                                               }
                                               else if (int.Parse(Convert.ToString(ListNumb22[1])) == 7)
                                               {
                                                   if (dico.TryGetValue(int.Parse(Convert.ToString(ListNumb22[2])) + 10, out value))
                                                   {
                                                       Resultat = "Soixante" + " " + value + " " + "Mille" + " " + Resultat;
                                                   }
                                               }
                                               else if (int.Parse(Convert.ToString(ListNumb22[1])) == 9)
                                               {
                                                   if (dico.TryGetValue(int.Parse(Convert.ToString(ListNumb22[2])) + 10, out value))
                                                   {
                                                       Resultat = "Quatre vingt" + " " + value + " " + "Mille" + " " + Resultat;
                                                   }
                                               }
                                               else
                                               {
                                                   string val221 = "";
                                                   if (dico.TryGetValue(int.Parse(Convert.ToString(ListNumb22[2])), out val221))
                                                   { Resultat = value + " " + val221 + " " + "Mille" + " " + Resultat; }
                                               }
     
                                           }
                                       }
     
     
                                       break;
                               }
     
                               break;
                           default: MessageBox.Show("Depassement de Capacité");
                               break;
                       }
                   }
                   else
                   {
                       Numb2 = Numb2.Trim();
                       char[] ListNumb1 = Numb2.ToCharArray();
                       int ps = ListNumb1.Length;
                       if (int.Parse(Convert.ToString(ListNumb1[0])) != 0)
                       {
     
     
     
                           switch (ps)
                           {
                               case 1:
                                   string value = "";
                                   dico.TryGetValue(int.Parse(Convert.ToString(ListNumb1[0])), out value);
                                   if (dico.TryGetValue(int.Parse(Convert.ToString(ListNumb1[0])), out value))
     
                                   //if (int.Parse(Convert.ToString(ListNumb1 [0])) == 1)
                                   {
                                       Resultat = value;
     
                                   }
     
                                   break;
     
                               case 2:
                                   MessageBox.Show("11");
                                   //string value = "";
                                   string pad2 = Convert.ToString(ListNumb1[0]) + Convert.ToString(ListNumb1[1]);
                                   //dico.TryGetValue(int.Parse(Convert.ToString(ListNumb1[1] )), out value);
                                   if (int.Parse(Convert.ToString(ListNumb1[0])) == 1)
                                   {
                                       if (dico.TryGetValue(int.Parse(pad2), out value)) { Resultat = value; }
                                   }
                                   else if (int.Parse(Convert.ToString(ListNumb1[0])) == 7)
                                   {
                                       if (dico.TryGetValue(int.Parse(Convert.ToString(ListNumb1[1])) + 10, out value))
                                       {
                                           Resultat = "Soixante" + " " + value;
                                       }
                                   }
                                   else if (int.Parse(Convert.ToString(ListNumb1[0])) == 9)
                                   {
                                       if (dico.TryGetValue(int.Parse(Convert.ToString(ListNumb1[1])) + 10, out value))
                                       {
                                           Resultat = "Quatre Vingt" + " " + value;
                                       }
                                   }
                                   else if (int.Parse(Convert.ToString(ListNumb1[1])) == 0)
                                   {
                                       if (dico.TryGetValue(int.Parse(pad2), out value))
                                       {
                                           Resultat = value;
                                       }
                                   }
                                   else
                                   {
                                       string value2 = "";
                                       if (dico.TryGetValue(int.Parse(Convert.ToString(ListNumb1[0])) * 10, out value))
                                       {
                                           if (dico.TryGetValue(int.Parse(Convert.ToString(ListNumb1[1])), out value2))
                                           {
                                               Resultat = value + " " + value2;
                                           }
                                       }
                                   }
     
     
                                   break;
     
                           }
     
                       }
     
                       else if (int.Parse(Convert.ToString(ListNumb1[0])) != 0)
                       {
                           MessageBox.Show("Supprimer les zero de debut d'expression", "EasyM@nager");
     
                       }
     
                       else { MessageBox.Show("Supprimer les zero de debut d'expression", "EasyM@nager"); }
     
                   }
     
               }
               catch (Exception e)
               {
                   MessageBox.Show("Verifier le format du texte saisi et recommencer", "EasyM@nager");
     
               }
     
     
               return Resultat;
               //return (Convert .ToString(ps));
           }
     
     
           }
    Je me rejouis dejà de savoir que j'ai pu vous etre utile.

  2. #2
    Membre Expert
    Avatar de Sehnsucht
    Homme Profil pro
    Développeur .NET
    Inscrit en
    Octobre 2008
    Messages
    847
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 41
    Localisation : France, Lot et Garonne (Aquitaine)

    Informations professionnelles :
    Activité : Développeur .NET

    Informations forums :
    Inscription : Octobre 2008
    Messages : 847
    Par défaut
    Ça existe déjà dans la lib DVP.Net avec la classe NumberConverter

  3. #3
    Membre très actif
    Homme Profil pro
    Autodidacte
    Inscrit en
    Mars 2016
    Messages
    154
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Congo-Brazzaville

    Informations professionnelles :
    Activité : Autodidacte
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Mars 2016
    Messages : 154
    Par défaut
    Merci vrment,
    je l'ai longtemps cherché.
    je vais pouvoir m'ameliorer en consultant l'original.

    Merci bien et ke God te benisse.

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

Discussions similaires

  1. filtre avec conversion numérique vers texte
    Par petitbison dans le forum VBA Access
    Réponses: 7
    Dernier message: 13/07/2009, 08h15
  2. Conversion Nombre en Texte
    Par le_dilem dans le forum Macros et VBA Excel
    Réponses: 0
    Dernier message: 27/06/2009, 14h18
  3. Algorithme conversion PDU vers Texte
    Par begyves dans le forum Algorithmes et structures de données
    Réponses: 1
    Dernier message: 16/02/2009, 15h10
  4. Conversion nombre vers texte
    Par lebossejames dans le forum Assembleur
    Réponses: 1
    Dernier message: 23/02/2007, 16h08
  5. [Windev Mobile 10] Conversion RTF vers Texte
    Par LordBob dans le forum Windev Mobile
    Réponses: 2
    Dernier message: 12/07/2006, 14h37

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