Convert to letter format a number value
Author Message
tonhinbm

 Posted: .NET Base Class Library, Convert to letter format a number value Top
 Is there any function to convert a letter format a number value Samples: 7 -> Seven. 5 -> Five. 20 -> Twelve......I'm from Spain, so I'm interesd in spanish translation...Thanks. .NET Development10

Mattias Sjogren

 Posted: .NET Base Class Library, Convert to letter format a number value Top
 Not in the framework, no. You'll have to write your own or look for an existing solution online.

nobugz

 Posted: .NET Base Class Library, Convert to letter format a number value Top
 I kinda doubt that this is what you're looking for:using System;class Program { public enum NumberMapper { One = 1, Five = 5, Seven = 7, TheAnswer = 42 } public static string MapNumber(int value) { string name = System.Enum.GetName(typeof(NumberMapper), value); return name; } static void Main(string[] args) { int value = 42; Console.WriteLine("{0} is {1}", value, MapNumber(value)); Console.ReadLine(); }}

tonhinbm

 Posted: .NET Base Class Library, Convert to letter format a number value Top
 First of all thanks for the answers.I need print pay cheks, so the posible values are so much to save in a enum.I'm from Spain, and my English is very poor, I try to search in google but I can't form a phrase that defines that I need.Someone knows what is the name of the algoritm, or some phrase that is in relation with this, like 'literal representation of a number value'...

nobugz

 Posted: .NET Base Class Library, Convert to letter format a number value Top
 Ah, I think I got you: \$1234.56 = "One thousand two hundred thirty four dollars and fifty six cents". Yeah, nothing in the framework but the algorithm is not that hard. Split the integer portion into groups of 3 so you know when to say "million", "thousand" and "dollar". For each 3 digit group, say the hundred amount (if any), look for irregular forms (English: 10..19). If none, say the tens and the units. I'm not sure how to do this Spanish but you're a lot closer to the fire than we are...

Mark Benningfield

 Posted: .NET Base Class Library, Convert to letter format a number value Top

Hello All.

tonhinbm:

The enums required are really not that long.  The long part is the list of conditional tests to decide what to put where.  I dug out an old Basic routine that I wrote quite a while ago and ported it to C#.  I tried to provide translation hints, but it's been a long time since I used my Spanish, so you'll have to bear with me.

This works with numbers up to \$99999.99.  Also, there's not much in the way of validation, and no error checking.

HTH.

 using System;using System.Collections.Generic;using System.Text; namespace ConsoleApplication1{    class Program    {        static void Main(string[] args)        {            while (true)            {                PayrollAmount pay = new PayrollAmount();                Console.Write("Enter Number: ");                decimal num = Convert.ToDecimal(Console.ReadLine());                Console.WriteLine(pay.ToText(num));            }        }    }     class PayrollAmount    {        private enum ****s        {            Ten = 10, //diez            Eleven, //once            Twelve, //doce            Thir****, //trece            Four****, //catorce            Fif****, //quince            Six****, //dieciseis            Seven****, //diecisiete            Eigh****, //dieciocho            Nine**** //diecinueve        };         private enum Tens        {            Twenty = 20, //veinte            Thirty = 30, //trenta            Forty = 40, //cuarenta            Fifty = 50, //cincuenta            Sixty = 60, //sesenta            Seventy = 70, //setenta            Eighty = 80, //ochenta            Ninety = 90 //noventa        };         private enum Ones        {            Zero, // cero            One, // uno            Two, // dos            Three, //tres            Four, //quatro            Five, //cinco            Six, // seis            Seven, //siete            Eight, //ocho            Nine //nueve        };         internal string ToText(decimal amount)        {             moved these down where they belong            string hundred = " Hundred"; //ciento            string thousand = " Thousand"; //mil            string dollars = " Dollars"; //dolares            string pennies = " Cents"; //centavos            string zero = "Zero"; //cero            string number = string.Empty;             if (amount >= 1 && amount < 2)                dollars = " Dollar"; //dolar             int tenThous = (int)amount / 10000;            amount -= (tenThous * 10000);             int thous = (int)amount / 1000;            amount -= (thous * 1000);             int hundreds = (int)amount / 100;            amount -= (hundreds * 100);             int tens = (int)amount / 10;            amount -= (tens * 10);             int ones = (int)amount;            amount -= (ones);             int tenCents = (int)(amount * 100);            int cents = tenCents % 10;            tenCents /= 10;             if (tenCents == 0 && cents == 1)                pennies = " Cent"; //centavo             if (tenThous > 0)            {                number += BuildTens(tenThous, thous);                number += thousand;            }            else if (thous > 0)            {                number += BuildSingles(thous, thousand);            }            if (hundreds > 0)            {                if (number != string.Empty)                    number += " ";                 number += BuildSingles(hundreds, hundred);                number += " ";            }            else if(number != string.Empty)            {                number += " ";            }             if (tens > 0)            {                number += BuildTens(tens, ones);            }            else if (ones > 0)            {                number += BuildSingles(ones, string.Empty);            }            if (number == string.Empty)                number = zero;             number += dollars + " and "; //" y "             if (tenCents > 0)            {                number += BuildTens(tenCents, cents);            }            else if (cents > 0)            {                number += BuildSingles(cents, string.Empty);            }             if (tenCents == 0 && cents == 0)            {                number += zero + pennies;            }            else            {                number += pennies;            }            return number;        }         private string BuildTens(int tens, int singles)        {            string result = string.Empty;             if (tens > 1) //between 20 and 90            {                switch (tens)                {                    case (int)Ones.Two:                        result += Tens.Twenty;                        break;                     case (int)Ones.Three:                        result += Tens.Thirty;                        break;                     case (int)Ones.Four:                        result += Tens.Forty;                        break;                     case (int)Ones.Five:                        result += Tens.Fifty;                        break;                     case (int)Ones.Six:                        result += Tens.Sixty;                        break;                     case (int)Ones.Seven:                        result += Tens.Seventy;                        break;                     case (int)Ones.Eight:                        result += Tens.Eighty;                        break;                     case (int)Ones.Nine:                        result += Tens.Ninety;                        break;                }                 if (singles > 0) //include any singles                {                    switch (singles)                    {                        case (int)Ones.One:                            result += "-" + Ones.One;                            break;                         case (int)Ones.Two:                            result += "-" + Ones.Two;                            break;                         case (int)Ones.Three:                            result += "-" + Ones.Three;                            break;                         case (int)Ones.Four:                            result += "-" + Ones.Four;                            break;                         case (int)Ones.Five:                            result += "-" + Ones.Five;                            break;                         case (int)Ones.Six:                            result += "-" + Ones.Six;                            break;                         case (int)Ones.Seven:                            result += "-" + Ones.Seven;                            break;                         case (int)Ones.Eight:                            result += "-" + Ones.Eight;                            break;                         case (int)Ones.Nine:                            result += "-" + Ones.Nine;                            break;                    }                }            }            else //between 10 and 19            {                switch (singles)                {                    case (int)Ones.Zero:                        result += ****s.Ten;                        break;                     case (int)Ones.One:                        result += ****s.Eleven;                        break;                     case (int)Ones.Two:                        result += ****s.Twelve;                        break;                     case (int)Ones.Three:                        result += ****s.Thir****;                        break;                     case (int)Ones.Four:                        result += ****s.Four****;                        break;                     case (int)Ones.Five:                        result += ****s.Fif****;                        break;                     case (int)Ones.Six:                        result += ****s.Six****;                        break;                     case (int)Ones.Seven:                        result += ****s.Seven****;                        break;                     case (int)Ones.Eight:                        result += ****s.Eigh****;                        break;                     case (int)Ones.Nine:                        result += ****s.Nine****;                        break;               }            }            return result;        }         private string BuildSingles(int singles, string tag)        {            string result = string.Empty;             switch (singles)            {                case (int)Ones.One:                    result = Ones.One + tag;                    break;                 case (int)Ones.Two:                    result = Ones.Two + tag;                    break;                 case (int)Ones.Three:                    result = Ones.Three + tag;                    break;                 case (int)Ones.Four:                    result = Ones.Four + tag;                    break;                 case (int)Ones.Five:                    result = Ones.Five + tag;                    break;                 case (int)Ones.Six:                    result = Ones.Six + tag;                    break;                 case (int)Ones.Seven:                    result = Ones.Seven + tag;                    break;                 case (int)Ones.Eight:                    result = Ones.Eight + tag;                    break;                 case (int)Ones.Nine:                    result = Ones.Nine + tag;                    break;            }             return result;        }    }}

OmegaMan

 Posted: .NET Base Class Library, Convert to letter format a number value Top

I respect the work Mark did, but humbly submit to you a different way to do it without killing any enums in the process. <g> As with Marks it goes up to \$99999.99

 static string[] verbage = new string[]     { "zero", "one", "two", "three", "four", "five", "six",       "seven", "eight", "nine", "ten", "eleven", "twelve",       "thir****", "four****", "fif****", "six****", "seven****",       "eigh****", "nine****", "twenty", "thrity", "forty",       "fifty", "sixty", "seventy", "eighty", "ninety",      "hundred", "thousand"    };static int[] offset = new int[] { 0, 20, 28, 29, 20 };public static string ShowMe(string money, bool addDollars){    StringBuilder result = new StringBuilder();    int decimalPoint = money.IndexOf('.');    string dollars = (decimalPoint == -1) money        : money.Substring(0, decimalPoint);    string cents   = (decimalPoint == -1) string.Empty : money.Substring(decimalPoint + 1);    int position = dollars.Length - 1;    int previous = -1;    int value;    if (string.IsNullOrEmpty(dollars))        result.AppendFormat("{0} ", verbage[0]);    for (int index = 0; index < dollars.Length; index++, position--)    {        value = int.Parse(dollars[index].ToString());        if (index -1 >= 0)            previous = int.Parse(dollars[index -1].ToString());        switch (position)        {            case 4 :                if (value != 1)                    result.AppendFormat("{0} ", verbage[offset[position] + (value - 2)]);                break;            case 3 :                if (value == 0)                    continue;                if (previous == 1)                    value += 10;                result.AppendFormat("{0} {1} ", verbage[value], verbage[offset[position]]);                break;            case 2 :                if (value == 0)                    continue;                result.AppendFormat("{0} {1} ", verbage[value], verbage[offset[position]]);                break;            case 1 :                if (value == 0)                    continue;                if (value != 1)                    result.AppendFormat("{0} ", verbage[offset[position] + (value - 2)]);                break;            case 0 :                if (value == 0)                {                    if (previous != 1)                         continue;                    value += 10;                }                result.AppendFormat("{0} ", verbage[value]);                break;        }    }    if (addDollars)        result.Append("dollars ");    if (string.IsNullOrEmpty(cents) == false)        result.AppendFormat("and {0}cents", ShowMe(cents, false));    return result.ToString();}

Output15000 = fif**** thousand dollars
21000 = twenty one thousand dollars
95000 = ninety five thousand dollars
15000 = fif**** thousand dollars
.55 = zero dollars and fifty five cents
.05 = zero dollars and five cents
3000.99 = three thousand dollars and ninety nine cents
3000 = three thousand dollars
320 = three hundred twenty dollars
300 = three hundred dollars
12345.67 = twelve thousand three hundred forty five dollars and sixty seven cents

Mark Benningfield

 Posted: .NET Base Class Library, Convert to letter format a number value Top
 Hello All. OmegaMan: There you go, picking on my poor, gray-haired Basic code. Actually, I'm kinda proud of the poor thing; over 100,000 calls, it's only about 4 seconds slower than your string-parse routine. Please notice that I did edit my post to re-locate the string ini to where it belongs. You can plainly see the procedural origins in my code sample, and it wasn't originally part of a persistent object instance, so the ini was not being picked up on repeated calls. And now the curiosity is just killing me. I realize we're getting close to going off-topic, but if you would be willing, could you post modifications to your code to insert the "-" where it goes, (like "ninety-eight"), and handle the single dollar and cent instances, (like "one dollar" instead of "one dollars") I'd really like to see how the old routine stands up to a closer comparison. I suppose I could do it myself if you're not interested, but I'd rather avoid the possibility that I might unconciously slow down your code. I wonder if perhaps this shouldn't be handled in a new thread, because I would also need to post the test routine to get your input as well. What do you think

tonhinbm

 Posted: .NET Base Class Library, Convert to letter format a number value Top

OmegaMan

 Posted: .NET Base Class Library, Convert to letter format a number value Top

 And now the curiosity is just killing me. I realize we're getting close to going off-topic, but if you would be willing, could you post modifications to your code to insert the "-" where it goes, (like "ninety-eight"), and handle the single dollar and cent instances, (like "one dollar" instead of "one dollars") I'd really like to see how the old routine stands up to a closer comparison.

Sorry for the shot across your bow...I have my own code toolbox with some old code as well, so I should be the one who talks. <g> I will take another look at it tonight and post a version for you to tinker around with sometime tomorrow. Maybe even soup-it up a bit. <g>

Thanks Mark.

OmegaMan

 Posted: .NET Base Class Library, Convert to letter format a number value Top
 Mark and all, I have posted the code to the Show and Tell forum as a challenge. I updated the code as requested and fixed some other bugs and added other features. See the code here at this thread Convert Money value to its Textual Representation . I posted it as a challenge...so maybe there is another way to skin the cat....