2010-04-08 9 views

Répondre

9

Vous devez écrire ce code vous-même. Si je devais deviner, je dirais que ce n'est pas dans le cadre car il serait presque impossible de le localiser (il y a bien plus à faire que le nom des chiffres: ordre des mots, règles de trait d'union, etc.). Cela me ramène des souvenirs parce que c'était un devoir de mon tout premier cours de programmation au collège, donc il ne devrait pas être trop difficile pour vous d'en écrire un juste en anglais.

+1

La localisation serait une douleur - je ne fais que penser aux tracas du français "98" "Quatre-Vingt dixhuit" (Littéralement quatre-vingt dix-huit) –

+0

La localisation est souvent une douleur, mais cela devrait vraiment être hors de -la boîte avec quelques personnalisations mineures comme des traits d'union. – PeterX

3

Le article here donne une solution pour convertir un entier en mots.

Bonté,

Dan

8

Voici le code pour convertir nombre de mots.

using System; 
namespace WpfApplication1 
{ 
public class NumberToEnglish 
    { 
    public String changeNumericToWords(double numb) 
    { 
    String num = numb.ToString(); 
    return changeToWords(num, false); 
    } 
    public String changeCurrencyToWords(String numb) 
    { 
    return changeToWords(numb, true); 
    } 
    public String changeNumericToWords(String numb) 
    { 
    return changeToWords(numb, false); 
    } 
    public String changeCurrencyToWords(double numb) 
    { 
    return changeToWords(numb.ToString(), true); 
    } 
    private String changeToWords(String numb, bool isCurrency) 
    { 
    String val = "", wholeNo = numb, points = "", andStr = "", pointStr=""; 
    String endStr = (isCurrency) ? ("Only") : (""); 
    try 
    { 
    int decimalPlace = numb.IndexOf("."); 
    if (decimalPlace > 0) 
    { 
    wholeNo = numb.Substring(0, decimalPlace); 
    points = numb.Substring(decimalPlace+1); 
    if (Convert.ToInt32(points) > 0) 
    { 
     andStr = (isCurrency)?("and"):("point");// just to separate whole numbers from > points/cents 
     endStr = (isCurrency) ? ("Cents "+endStr) : (""); 
     pointStr = translateCents(points); 
    } 
    } 
    val = String.Format("{0} {1}{2} {3}",translateWholeNumber(wholeNo).Trim(),andStr,pointStr,endStr); 
    } 
    catch { ;} 
    return val; 
    } 
    private String translateWholeNumber(String number) 
    { 
    string word = ""; 
    try 
    { 
    bool beginsZero = false;//tests for 0XX 
    bool isDone = false;//test if already translated 
    double dblAmt = (Convert.ToDouble(number)); 
    //if ((dblAmt > 0) && number.StartsWith("0")) 
    if (dblAmt > 0) 
    {//test for zero or digit zero in a nuemric 
     beginsZero = number.StartsWith("0"); 

     int numDigits = number.Length; 
     int pos = 0;//store digit grouping 
     String place = "";//digit grouping name:hundres,thousand,etc... 
     switch (numDigits) 
     { 
     case 1://ones' range 
     word = ones(number); 
     isDone = true; 
     break; 
     case 2://tens' range 
     word = tens(number); 
     isDone = true; 
     break; 
     case 3://hundreds' range 
     pos = (numDigits % 3) + 1; 
     place = " Hundred "; 
     break; 
     case 4://thousands' range 
     case 5: 
     case 6: 
     pos = (numDigits % 4) + 1; 
     place = " Thousand "; 
     break; 
     case 7://millions' range 
     case 8: 
     case 9: 
     pos = (numDigits % 7) + 1; 
     place = " Million "; 
     break; 
     case 10://Billions's range 
     pos = (numDigits % 10) + 1; 
     place = " Billion "; 
     break; 
     //add extra case options for anything above Billion... 
     default: 
     isDone = true; 
     break; 
     } 
     if (!isDone) 
     {//if transalation is not done, continue...(Recursion comes in now!!) 
     word = translateWholeNumber(number.Substring(0, pos)) + place + translateWholeNumber(number.Substring(pos)); 
     //check for trailing zeros 
     if (beginsZero) word = " and " + word.Trim(); 
     } 
     //ignore digit grouping names 
     if (word.Trim().Equals(place.Trim())) word = ""; 
    } 
    } 
    catch { ;} 
    return word.Trim(); 
    } 
    private String tens(String digit) 
    { 
    int digt = Convert.ToInt32(digit); 
    String name = null; 
    switch (digt) 
    { 
    case 10: 
     name = "Ten"; 
     break; 
    case 11: 
     name = "Eleven"; 
     break; 
    case 12: 
     name = "Twelve"; 
     break; 
    case 13: 
     name = "Thirteen"; 
     break; 
    case 14: 
     name = "Fourteen"; 
     break; 
    case 15: 
     name = "Fifteen"; 
     break; 
    case 16: 
     name = "Sixteen"; 
     break; 
    case 17: 
     name = "Seventeen"; 
     break; 
    case 18: 
     name = "Eighteen"; 
     break; 
    case 19: 
     name = "Nineteen"; 
     break; 
    case 20: 
     name = "Twenty"; 
     break; 
    case 30: 
     name = "Thirty"; 
     break; 
    case 40: 
     name = "Fourty"; 
     break; 
    case 50: 
     name = "Fifty"; 
     break; 
    case 60: 
     name = "Sixty"; 
     break; 
    case 70: 
     name = "Seventy"; 
     break; 
    case 80: 
     name = "Eighty"; 
     break; 
    case 90: 
     name = "Ninety"; 
     break; 
    default: 
     if (digt > 0) 
     { 
     name = tens(digit.Substring(0, 1) + "0") + "" + ones(digit.Substring(1)); 
     } 
     break; 
    } 
    return name; 
    } 
    private String ones(String digit) 
    { 
    int digt = Convert.ToInt32(digit); 
    String name = ""; 
    switch (digt) 
    { 
    case 1: 
     name = "One"; 
     break; 
    case 2: 
     name = "Two"; 
     break; 
    case 3: 
     name = "Three"; 
     break; 
    case 4: 
     name = "Four"; 
     break; 
    case 5: 
     name = "Five"; 
     break; 
    case 6: 
     name = "Six"; 
     break; 
    case 7: 
     name = "Seven"; 
     break; 
    case 8: 
     name = "Eight"; 
     break; 
    case 9: 
     name = "Nine"; 
     break; 
    } 
    return name; 
    } 
    private String translateCents(String cents) 
    { 
    String cts = "", digit = "", engOne = ""; 
    for (int i = 0; i < cents.Length; i++) 
    { 
     digit = cents[i].ToString(); 
     if (digit.Equals("0")) 
     { 
     engOne = "Zero"; 
     } 
     else 
     { 
     engOne = ones(digit); 
     } 
     cts += " " + engOne; 
    } 
    return cts; 
    } 
    } 
} 

créer un fichier de classe dans votre projet, Copiez ce code à votre fichier de classe. Modifiez l'espace de noms de votre espace de noms de projet .

créer obj de ce quelque chose de classe comme celui-ci

NumberToEnglish objnumber = new NumberToEnglish(); 

et objnumber.changeNumericToWords(100);

avec cette fonction vous obtiendrez le numéro dans le mot.

source: http://social.msdn.microsoft.com/Forums/en/wpf/thread/42b5bb54-dfd6-4c5b-8d51-82e5fc29f8e8 Auteur: Hiran Repakula

Il utilise la méthode de l'instance, mais vous pouvez faire ces méthodes static.

6

Voici un moyen supplémentaire, juste pour les coups de pied.

public static class NumericSpelling 
{ 
    private const long Quadrillion = Trillion * 1000; 
    private const long Trillion = Billion * 1000; 
    private const long Billion = Million * 1000; 
    private const long Million = Thousand * 1000; 
    private const long Thousand = Hundred * 10; 
    private const long Hundred = 100; 

    public static string ToVerbal(this int value) { return ToVerbal((long)value); } 

    public static string ToVerbal(this long value) 
    { 
     if (value == 0) return "zero"; 

     if (value < 0) 
     { 
      return "negative " + ToVerbal(Math.Abs(value)); 
     } 

     System.Text.StringBuilder builder = new StringBuilder(); 

     int unit = 0; 

     if (value >= Quadrillion) 
     { 
      unit = (int)(value/Quadrillion); 
      value -= unit * Quadrillion; 

      builder.AppendFormat("{0}{1} quadrillion", builder.Length > 0 ? ", " : string.Empty, ToVerbal(unit)); 
     } 

     if (value >= Trillion) 
     { 
      unit = (int)(value/Trillion); 
      value -= unit * Trillion; 

      builder.AppendFormat("{0}{1} trillion", builder.Length > 0 ? ", " : string.Empty, ToVerbal(unit)); 
     } 

     if (value >= Billion) 
     { 
      unit = (int)(value/Billion); 
      value -= unit * Billion; 

      builder.AppendFormat("{0}{1} billion", builder.Length > 0 ? ", " : string.Empty, ToVerbal(unit)); 
     } 

     if (value >= Million) 
     { 
      unit = (int)(value/Million); 
      value -= unit * Million; 

      builder.AppendFormat("{0}{1} million", builder.Length > 0 ? ", " : string.Empty, ToVerbal(unit)); 
     } 

     if (value >= Thousand) 
     { 
      unit = (int)(value/Thousand); 
      value -= unit * Thousand; 

      builder.AppendFormat("{0}{1} thousand", builder.Length > 0 ? ", " : string.Empty, ToVerbal(unit)); 
     } 

     if (value >= Hundred) 
     { 
      unit = (int)(value/Hundred); 
      value -= unit * Hundred; 

      builder.AppendFormat("{0}{1} hundred", builder.Length > 0 ? ", " : string.Empty, ToVerbal(unit)); 
     } 

     if (builder.Length > 0 && value > 0) builder.AppendFormat(" and"); 

     if (value >= 90) 
     { 
      value -= 90; 

      builder.AppendFormat("{0}ninety", builder.Length > 0 ? " " : string.Empty); 
     } 

     if (value >= 80) 
     { 
      value -= 80; 

      builder.AppendFormat("{0}eighty", builder.Length > 0 ? " " : string.Empty); 
     } 

     if (value >= 70) 
     { 
      value -= 70; 

      builder.AppendFormat("{0}seventy", builder.Length > 0 ? " " : string.Empty); 
     } 

     if (value >= 60) 
     { 
      value -= 60; 

      builder.AppendFormat("{0}sixty", builder.Length > 0 ? " " : string.Empty); 
     } 

     if (value >= 50) 
     { 
      value -= 50; 

      builder.AppendFormat("{0}fifty", builder.Length > 0 ? " " : string.Empty); 
     } 

     if (value >= 40) 
     { 
      value -= 40; 

      builder.AppendFormat("{0}forty", builder.Length > 0 ? " " : string.Empty); 
     } 

     if (value >= 30) 
     { 
      value -= 30; 

      builder.AppendFormat("{0}thirty", builder.Length > 0 ? " " : string.Empty); 
     } 

     if (value >= 20) 
     { 
      value -= 20; 

      builder.AppendFormat("{0}twenty", builder.Length > 0 ? " " : string.Empty); 
     } 

     if (value == 19) builder.AppendFormat("{0}nineteen", builder.Length > 0 ? " " : string.Empty); 
     if (value == 18) builder.AppendFormat("{0}eighteen", builder.Length > 0 ? " " : string.Empty); 
     if (value == 17) builder.AppendFormat("{0}seventeen", builder.Length > 0 ? " " : string.Empty); 
     if (value == 16) builder.AppendFormat("{0}sixteen", builder.Length > 0 ? " " : string.Empty); 
     if (value == 15) builder.AppendFormat("{0}fifteen", builder.Length > 0 ? " " : string.Empty); 
     if (value == 14) builder.AppendFormat("{0}fourteen", builder.Length > 0 ? " " : string.Empty); 
     if (value == 13) builder.AppendFormat("{0}thirteen", builder.Length > 0 ? " " : string.Empty); 
     if (value == 12) builder.AppendFormat("{0}twelve", builder.Length > 0 ? " " : string.Empty); 
     if (value == 11) builder.AppendFormat("{0}eleven", builder.Length > 0 ? " " : string.Empty); 
     if (value == 10) builder.AppendFormat("{0}ten", builder.Length > 0 ? " " : string.Empty); 
     if (value == 9) builder.AppendFormat("{0}nine", builder.Length > 0 ? " " : string.Empty); 
     if (value == 8) builder.AppendFormat("{0}eight", builder.Length > 0 ? " " : string.Empty); 
     if (value == 7) builder.AppendFormat("{0}seven", builder.Length > 0 ? " " : string.Empty); 
     if (value == 6) builder.AppendFormat("{0}six", builder.Length > 0 ? " " : string.Empty); 
     if (value == 5) builder.AppendFormat("{0}five", builder.Length > 0 ? " " : string.Empty); 
     if (value == 4) builder.AppendFormat("{0}four", builder.Length > 0 ? " " : string.Empty); 
     if (value == 3) builder.AppendFormat("{0}three", builder.Length > 0 ? " " : string.Empty); 
     if (value == 2) builder.AppendFormat("{0}two", builder.Length > 0 ? " " : string.Empty); 
     if (value == 1) builder.AppendFormat("{0}one", builder.Length > 0 ? " " : string.Empty); 

     return builder.ToString(); 
    } 
} 

Entrée:

int first = 10447; 
long second = 10576749323475; 
int third = 0; 
int fourth = -1095; 
int fifth = 100; 
int sixth = 102; 
int seventh = 10004; 
int eight = 100025; 

Console.WriteLine(first.ToVerbal()); 
Console.WriteLine(second.ToVerbal()); 
Console.WriteLine(third.ToVerbal()); 
Console.WriteLine(fourth.ToVerbal()); 
Console.WriteLine(fifth.ToVerbal()); 
Console.WriteLine(sixth.ToVerbal()); 
Console.WriteLine(seventh.ToVerbal()); 
Console.WriteLine(eight.ToVerbal()); 

Sortie:

ten thousand, four hundred and forty seven 
ten trillion, five hundred and seventy six billion, seven hundred and forty nine million, three hundred and twenty three thousand, four hundred and seventy five 
zero 
negative one thousand and ninety five 
one hundred 
one hundred and two 
ten thousand and four 
one hundred thousand and twenty five 
+1

Vous en avez vingt deux fois. Bon travail J'ai testé ton code. –

+0

@MarkWithers: Merci, bonne prise; corrigée! –

0

Oui, il y a une façon de le faire. Le projet International Components for Unicode a un RuleBasedNumberFormatter avec une option "épeler". Il prend même en charge la localisation complète.

Le seul hic est qu'il est uniquement disponible en C, C++ et Java. Une initiative est en cours dans le projet icu.net (pour .NET Framework et .NET Standard 1.6), mais cette fonctionnalité et bien d'autres ne sont pas encore portées. Cependant, une contribution pourrait résoudre cela très bien.

Il ya aussi a tool qui est supposé être capable de générer automatiquement une bibliothèque C# wrapper autour de la bibliothèque ICU4C, mais je ne l'ai pas essayé.

Other options sont disponibles pour d'autres langages de programmation.

Questions connexes