locked
Convert to letter format a number value RRS feed

  • Question

  • 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.
    Thursday, January 11, 2007 7:14 AM

Answers

  • 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 Teens
            {
                Ten = 10, //diez
                Eleven, //once
                Twelve, //doce
                Thirteen, //trece
                Fourteen, //catorce
                Fifteen, //quince
                Sixteen, //dieciseis
                Seventeen, //diecisiete
                Eighteen, //dieciocho
                Nineteen //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)
            {
                [edit] 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 += Teens.Ten;
                            break;

                        case (int)Ones.One:
                            result += Teens.Eleven;
                            break;

                        case (int)Ones.Two:
                            result += Teens.Twelve;
                            break;

                        case (int)Ones.Three:
                            result += Teens.Thirteen;
                            break;

                        case (int)Ones.Four:
                            result += Teens.Fourteen;
                            break;

                        case (int)Ones.Five:
                            result += Teens.Fifteen;
                            break;

                        case (int)Ones.Six:
                            result += Teens.Sixteen;
                            break;

                        case (int)Ones.Seven:
                            result += Teens.Seventeen;
                            break;

                        case (int)Ones.Eight:
                            result += Teens.Eighteen;
                            break;

                        case (int)Ones.Nine:
                            result += Teens.Nineteen;
                            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;
            }
        }
    }

    Friday, January 12, 2007 11:26 PM
  • Thanks guys..

    Using Mark algoritm I translate to Spanhish numeration the conversion...

    Here is my code, This works with numbers up to 999999999.99 €

    <code>

    class CNumeros

    {

    public static string representacionLiteral(decimal cantidad)

    {

    try

    {

    string numero = string.Empty;

    //Rendodeamos a dos Decimales

    cantidad = decimal.Round(cantidad, 2);

    int parteEntera = Convert.ToInt32(cantidad);

    int parteDecimal = Convert.ToInt32(100 * (cantidad - decimal.Floor(cantidad)));

    //***************************************************************

    string signoEuros = "EUROS ";

    string signoCentimos = "CENTIMOS";

    if (parteEntera == 1)

    signoEuros = "EURO ";

    if (parteDecimal == 1)

    signoCentimos = "CENTIMO";

    //*****************************************************************

    //Descomponemos el Numero

    //int centenasBillon = (int)cantidad / 100000000000;

    //cantidad -= (centenasBillon * 100000000000);

    //int decenasBillon = (int)cantidad / 10000000000;

    //cantidad -= (decenasBillon * 10000000000);

    //int unidadesBillon = (int)cantidad / 1000000000;

    //cantidad -= (unidadesBillon * 1000000000);

    int centenasMillon = (int)cantidad / 100000000;

    cantidad -= (centenasMillon * 100000000);

    int decenasMillon = (int)cantidad / 10000000;

    cantidad -= (decenasMillon * 10000000);

    int unidadesMillon = (int)cantidad / 1000000;

    cantidad -= (unidadesMillon * 1000000);

    int centenasMillar = (int)cantidad / 100000;

    cantidad -= (centenasMillar * 100000);

    int decenasMillar = (int)cantidad / 10000;

    cantidad -= (decenasMillar * 10000);

    int unidadesMillar = (int)cantidad / 1000;

    cantidad -= (unidadesMillar * 1000);

    int centenas = (int)cantidad / 100;

    cantidad -= (centenas * 100);

    int decenas = (int)cantidad / 10;

    cantidad -= (decenas * 10);

    int unidades = (int)cantidad;

    cantidad -= (unidades);

    //************ Centimos *************************

    int decenasCentimos = (int)(cantidad * 100);

    int unidadesCentimos = decenasCentimos % 10;

    decenasCentimos /= 10;

    //***********************************************

    ////Billones

    //decimal billones = (100 * centenasBillon + 10 * decenasBillon + unidadesBillon);

    //if (billones > 0)

    //{

    // numero += crearNumero(centenasBillon, decenasBillon, unidadesBillon);

    // if (billones > 1)

    // numero += "Billones ";

    // else

    // numero += "Bill¢n ";

    //}

    //Millones

    decimal millones = (100 * centenasMillon + 10 * decenasMillon + unidadesMillon);

    if (millones > 0)

    {

    numero += crearNumero(centenasMillon, decenasMillon, unidadesMillon);

    if (millones > 1)

    numero += "Millones ";

    else

    numero += "Mill¢n ";

    }

    //Millares

    decimal millares = (100 * centenasMillar + 10 * decenasMillar + unidadesMillar);

    if (millares > 0)

    {

    if (millares > 1)

    numero += crearNumero(centenasMillar, decenasMillar, unidadesMillar);

    numero += "Mil ";

    }

    //Unidades

    decimal uds = (100 * centenas + 10 * decenas + unidades);

    if (uds > 0)

    {

    numero += crearNumero(centenas, decenas, unidades);

    }

    //Cero

    if (parteEntera == 0)

    numero += "Cero ";

    numero += signoEuros;

     

    //Centimos

    decimal cents = (10 * decenasCentimos + unidadesCentimos);

    if (cents > 0)

    {

    numero += "Con ";

    numero += crearNumero(0, decenasCentimos, unidadesCentimos);

    numero += signoCentimos;

    }

    return numero;

    }

    catch (Exception)

    {

    return "N£mero Demasiado Grande";

    }

    }

    private static string crearNumero(int centenas, int decenas, int unidades)

    {

    String number = "";

    //Combrobamos si es un numero irregular

    if (es100(centenas, decenas, unidades))

    {

    number += "Cien ";

    }

    else

    {

    if (centenas > 0)

    {

    number += ImprimirCentenas(centenas);

    }

    if (decenas > 0)

    {

    number += ImprimirDecenasUnidades(decenas, unidades);

    }

    else if (unidades > 0)

    {

    number += ImprimirUnidades(unidades);

    }

    }

    return number;

    }

    private static string ImprimirDecenasUnidades(int decenas, int unidades)

    {

    string result = string.Empty;

    //Entre 30 and 90

    if (decenas > 2)

    {

    result += ImprimirDecenas30a90(decenas);

    if (unidades > 0) //A¤adimos las unidade

    {

    result += "y " + ImprimirUnidades(unidades);

    }

    }

    //Entre 20 y 29

    if (decenas == 2)

    {

    result += ImprimirDecenas20a29(unidades);

    }

    //Entre 10 y 19

    if (decenas == 1)

    {

    result += ImprimirDecenas10a19(unidades);

    }

    return result;

    }

    private static string ImprimirDecenas30a90(int decenas)

    {

    string result = string.Empty;

    switch (decenas)

    {

    case 3:

    result += "Treinta ";

    break;

    case 4:

    result += "Cuarenta ";

    break;

    case 5:

    result += "Cincuenta ";

    break;

    case 6:

    result += "Sesenta ";

    break;

    case 7:

    result += "Setenta ";

    break;

    case 8:

    result += "Ochenta ";

    break;

    case 9:

    result += "Noventa ";

    break;

    }

    return result;

    }

     

    private static string ImprimirDecenas20a29(int unidades)

    {

    string result = string.Empty;

    switch (unidades)

    {

    case 0:

    result += "Veinte ";

    break;

    case 1:

    result += "Veintiun ";

    break;

    case 2:

    result += "Veintidos ";

    break;

    case 3:

    result += "Veintitres ";

    break;

    case 4:

    result += "Veinticuatro ";

    break;

    case 5:

    result += "Veinticinco ";

    break;

    case 6:

    result += "Veintiseis ";

    break;

    case 7:

    result += "Veintisiete ";

    break;

    case 8:

    result += "Veintiocho ";

    break;

    case 9:

    result += "Veintinueve ";

    break;

    }

    return result;

    }

    private static string ImprimirDecenas10a19(int unidades)

    {

    string result = string.Empty;

    switch (unidades)

    {

    case 0:

    result += "Diez ";

    break;

    case 1:

    result += "Once ";

    break;

    case 2:

    result += "Doce ";

    break;

    case 3:

    result += "Trece ";

    break;

    case 4:

    result += "Catorce ";

    break;

    case 5:

    result += "Quince ";

    break;

    case 6:

    result += "Dieciseis ";

    break;

    case 7:

    result += "Diecisiete ";

    break;

    case 8:

    result += "Dieciocho ";

    break;

    case 9:

    result += "Diecinueve ";

    break;

    }

    return result;

    }

     

    private static string ImprimirCentenas(int centenas)

    {

    string result = string.Empty;

    switch (centenas)

    {

    case 1:

    result = "Ciento ";

    break;

    case 2:

    result = "Doscientos ";

    break;

    case 3:

    result = "Trescientos ";

    break;

    case 4:

    result = "Cuatrocientos ";

    break;

    case 5:

    result = "Quinientos ";

    break;

    case 6:

    result = "Seiscientos ";

    break;

    case 7:

    result = "Setecientos ";

    break;

    case 8:

    result = "Ochocientos ";

    break;

    case 9:

    result = "Novecientos ";

    break;

    }

    return result;

    }

     

    private static string ImprimirUnidades(int unidades)

    {

    string result = string.Empty;

    switch (unidades)

    {

    case 1:

    result = "Un ";

    break;

    case 2:

    result = "Dos ";

    break;

    case 3:

    result = "Tres ";

    break;

    case 4:

    result = "Cuatro ";

    break;

    case 5:

    result = "Cinco ";

    break;

    case 6:

    result = "Seis ";

    break;

    case 7:

    result = "Siete ";

    break;

    case 8:

    result = "Ocho ";

    break;

    case 9:

    result = "Nueve ";

    break;

    }

    return result;

    }

     

    private static bool es100(int centenas, int decenas, int unidades)

    {

    bool cien = false;

    if( ( 100*centenas + 10*decenas + unidades) == 100 )

    cien = true;

    return cien;

    }

     

    }

    </code>

    Saturday, January 13, 2007 5:50 PM

All replies

  • Not in the framework, no. You'll have to write your own or look for an existing solution online.

     

    Thursday, January 11, 2007 7:48 AM
  • 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();
      }
    }

    Thursday, January 11, 2007 1:39 PM
  • 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'...
    Friday, January 12, 2007 7:23 AM
  • 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...
    Friday, January 12, 2007 12:51 PM
  • 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 Teens
            {
                Ten = 10, //diez
                Eleven, //once
                Twelve, //doce
                Thirteen, //trece
                Fourteen, //catorce
                Fifteen, //quince
                Sixteen, //dieciseis
                Seventeen, //diecisiete
                Eighteen, //dieciocho
                Nineteen //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)
            {
                [edit] 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 += Teens.Ten;
                            break;

                        case (int)Ones.One:
                            result += Teens.Eleven;
                            break;

                        case (int)Ones.Two:
                            result += Teens.Twelve;
                            break;

                        case (int)Ones.Three:
                            result += Teens.Thirteen;
                            break;

                        case (int)Ones.Four:
                            result += Teens.Fourteen;
                            break;

                        case (int)Ones.Five:
                            result += Teens.Fifteen;
                            break;

                        case (int)Ones.Six:
                            result += Teens.Sixteen;
                            break;

                        case (int)Ones.Seven:
                            result += Teens.Seventeen;
                            break;

                        case (int)Ones.Eight:
                            result += Teens.Eighteen;
                            break;

                        case (int)Ones.Nine:
                            result += Teens.Nineteen;
                            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;
            }
        }
    }

    Friday, January 12, 2007 11:26 PM
  • 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",
          "thirteen", "fourteen", "fifteen", "sixteen", "seventeen",
          "eighteen", "nineteen", "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 = fifteen thousand dollars
    21000 = twenty one thousand dollars
    95000 = ninety five thousand dollars
    15000 = fifteen 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

    Saturday, January 13, 2007 6:37 AM
  • 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?

    Saturday, January 13, 2007 3:14 PM
  • Thanks guys..

    Using Mark algoritm I translate to Spanhish numeration the conversion...

    Here is my code, This works with numbers up to 999999999.99 €

    <code>

    class CNumeros

    {

    public static string representacionLiteral(decimal cantidad)

    {

    try

    {

    string numero = string.Empty;

    //Rendodeamos a dos Decimales

    cantidad = decimal.Round(cantidad, 2);

    int parteEntera = Convert.ToInt32(cantidad);

    int parteDecimal = Convert.ToInt32(100 * (cantidad - decimal.Floor(cantidad)));

    //***************************************************************

    string signoEuros = "EUROS ";

    string signoCentimos = "CENTIMOS";

    if (parteEntera == 1)

    signoEuros = "EURO ";

    if (parteDecimal == 1)

    signoCentimos = "CENTIMO";

    //*****************************************************************

    //Descomponemos el Numero

    //int centenasBillon = (int)cantidad / 100000000000;

    //cantidad -= (centenasBillon * 100000000000);

    //int decenasBillon = (int)cantidad / 10000000000;

    //cantidad -= (decenasBillon * 10000000000);

    //int unidadesBillon = (int)cantidad / 1000000000;

    //cantidad -= (unidadesBillon * 1000000000);

    int centenasMillon = (int)cantidad / 100000000;

    cantidad -= (centenasMillon * 100000000);

    int decenasMillon = (int)cantidad / 10000000;

    cantidad -= (decenasMillon * 10000000);

    int unidadesMillon = (int)cantidad / 1000000;

    cantidad -= (unidadesMillon * 1000000);

    int centenasMillar = (int)cantidad / 100000;

    cantidad -= (centenasMillar * 100000);

    int decenasMillar = (int)cantidad / 10000;

    cantidad -= (decenasMillar * 10000);

    int unidadesMillar = (int)cantidad / 1000;

    cantidad -= (unidadesMillar * 1000);

    int centenas = (int)cantidad / 100;

    cantidad -= (centenas * 100);

    int decenas = (int)cantidad / 10;

    cantidad -= (decenas * 10);

    int unidades = (int)cantidad;

    cantidad -= (unidades);

    //************ Centimos *************************

    int decenasCentimos = (int)(cantidad * 100);

    int unidadesCentimos = decenasCentimos % 10;

    decenasCentimos /= 10;

    //***********************************************

    ////Billones

    //decimal billones = (100 * centenasBillon + 10 * decenasBillon + unidadesBillon);

    //if (billones > 0)

    //{

    // numero += crearNumero(centenasBillon, decenasBillon, unidadesBillon);

    // if (billones > 1)

    // numero += "Billones ";

    // else

    // numero += "Bill¢n ";

    //}

    //Millones

    decimal millones = (100 * centenasMillon + 10 * decenasMillon + unidadesMillon);

    if (millones > 0)

    {

    numero += crearNumero(centenasMillon, decenasMillon, unidadesMillon);

    if (millones > 1)

    numero += "Millones ";

    else

    numero += "Mill¢n ";

    }

    //Millares

    decimal millares = (100 * centenasMillar + 10 * decenasMillar + unidadesMillar);

    if (millares > 0)

    {

    if (millares > 1)

    numero += crearNumero(centenasMillar, decenasMillar, unidadesMillar);

    numero += "Mil ";

    }

    //Unidades

    decimal uds = (100 * centenas + 10 * decenas + unidades);

    if (uds > 0)

    {

    numero += crearNumero(centenas, decenas, unidades);

    }

    //Cero

    if (parteEntera == 0)

    numero += "Cero ";

    numero += signoEuros;

     

    //Centimos

    decimal cents = (10 * decenasCentimos + unidadesCentimos);

    if (cents > 0)

    {

    numero += "Con ";

    numero += crearNumero(0, decenasCentimos, unidadesCentimos);

    numero += signoCentimos;

    }

    return numero;

    }

    catch (Exception)

    {

    return "N£mero Demasiado Grande";

    }

    }

    private static string crearNumero(int centenas, int decenas, int unidades)

    {

    String number = "";

    //Combrobamos si es un numero irregular

    if (es100(centenas, decenas, unidades))

    {

    number += "Cien ";

    }

    else

    {

    if (centenas > 0)

    {

    number += ImprimirCentenas(centenas);

    }

    if (decenas > 0)

    {

    number += ImprimirDecenasUnidades(decenas, unidades);

    }

    else if (unidades > 0)

    {

    number += ImprimirUnidades(unidades);

    }

    }

    return number;

    }

    private static string ImprimirDecenasUnidades(int decenas, int unidades)

    {

    string result = string.Empty;

    //Entre 30 and 90

    if (decenas > 2)

    {

    result += ImprimirDecenas30a90(decenas);

    if (unidades > 0) //A¤adimos las unidade

    {

    result += "y " + ImprimirUnidades(unidades);

    }

    }

    //Entre 20 y 29

    if (decenas == 2)

    {

    result += ImprimirDecenas20a29(unidades);

    }

    //Entre 10 y 19

    if (decenas == 1)

    {

    result += ImprimirDecenas10a19(unidades);

    }

    return result;

    }

    private static string ImprimirDecenas30a90(int decenas)

    {

    string result = string.Empty;

    switch (decenas)

    {

    case 3:

    result += "Treinta ";

    break;

    case 4:

    result += "Cuarenta ";

    break;

    case 5:

    result += "Cincuenta ";

    break;

    case 6:

    result += "Sesenta ";

    break;

    case 7:

    result += "Setenta ";

    break;

    case 8:

    result += "Ochenta ";

    break;

    case 9:

    result += "Noventa ";

    break;

    }

    return result;

    }

     

    private static string ImprimirDecenas20a29(int unidades)

    {

    string result = string.Empty;

    switch (unidades)

    {

    case 0:

    result += "Veinte ";

    break;

    case 1:

    result += "Veintiun ";

    break;

    case 2:

    result += "Veintidos ";

    break;

    case 3:

    result += "Veintitres ";

    break;

    case 4:

    result += "Veinticuatro ";

    break;

    case 5:

    result += "Veinticinco ";

    break;

    case 6:

    result += "Veintiseis ";

    break;

    case 7:

    result += "Veintisiete ";

    break;

    case 8:

    result += "Veintiocho ";

    break;

    case 9:

    result += "Veintinueve ";

    break;

    }

    return result;

    }

    private static string ImprimirDecenas10a19(int unidades)

    {

    string result = string.Empty;

    switch (unidades)

    {

    case 0:

    result += "Diez ";

    break;

    case 1:

    result += "Once ";

    break;

    case 2:

    result += "Doce ";

    break;

    case 3:

    result += "Trece ";

    break;

    case 4:

    result += "Catorce ";

    break;

    case 5:

    result += "Quince ";

    break;

    case 6:

    result += "Dieciseis ";

    break;

    case 7:

    result += "Diecisiete ";

    break;

    case 8:

    result += "Dieciocho ";

    break;

    case 9:

    result += "Diecinueve ";

    break;

    }

    return result;

    }

     

    private static string ImprimirCentenas(int centenas)

    {

    string result = string.Empty;

    switch (centenas)

    {

    case 1:

    result = "Ciento ";

    break;

    case 2:

    result = "Doscientos ";

    break;

    case 3:

    result = "Trescientos ";

    break;

    case 4:

    result = "Cuatrocientos ";

    break;

    case 5:

    result = "Quinientos ";

    break;

    case 6:

    result = "Seiscientos ";

    break;

    case 7:

    result = "Setecientos ";

    break;

    case 8:

    result = "Ochocientos ";

    break;

    case 9:

    result = "Novecientos ";

    break;

    }

    return result;

    }

     

    private static string ImprimirUnidades(int unidades)

    {

    string result = string.Empty;

    switch (unidades)

    {

    case 1:

    result = "Un ";

    break;

    case 2:

    result = "Dos ";

    break;

    case 3:

    result = "Tres ";

    break;

    case 4:

    result = "Cuatro ";

    break;

    case 5:

    result = "Cinco ";

    break;

    case 6:

    result = "Seis ";

    break;

    case 7:

    result = "Siete ";

    break;

    case 8:

    result = "Ocho ";

    break;

    case 9:

    result = "Nueve ";

    break;

    }

    return result;

    }

     

    private static bool es100(int centenas, int decenas, int unidades)

    {

    bool cien = false;

    if( ( 100*centenas + 10*decenas + unidades) == 100 )

    cien = true;

    return cien;

    }

     

    }

    </code>

    Saturday, January 13, 2007 5:50 PM
  •  Mark Benningfield wrote:
    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.

    Sunday, January 14, 2007 12:48 AM
  • 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....<g>

    Sunday, January 14, 2007 4:46 AM