Click here to Skip to main content
15,921,643 members
Home / Discussions / C#
   

C#

 
QuestionMax Number of Processes limit Pin
Subby Dev8-Sep-06 8:59
Subby Dev8-Sep-06 8:59 
Questionhow to create a System.data.datatabe from interop.excel.worksheet? Pin
qahwah8-Sep-06 7:54
qahwah8-Sep-06 7:54 
QuestionDigit To text Converter [modified] Pin
Syed Shahid Hussain8-Sep-06 6:50
Syed Shahid Hussain8-Sep-06 6:50 
AnswerRe: Digit To text Converter Pin
leppie8-Sep-06 7:11
leppie8-Sep-06 7:11 
GeneralRe: Digit To text Converter Pin
Syed Shahid Hussain8-Sep-06 8:09
Syed Shahid Hussain8-Sep-06 8:09 
GeneralRe: Digit To text Converter Pin
Syed Shahid Hussain8-Sep-06 9:12
Syed Shahid Hussain8-Sep-06 9:12 
GeneralRe: Digit To text Converter Pin
Nader Elshehabi8-Sep-06 9:18
Nader Elshehabi8-Sep-06 9:18 
AnswerRe: Digit To text Converter Pin
Nader Elshehabi8-Sep-06 9:54
Nader Elshehabi8-Sep-06 9:54 
Hello friend.

Here is your code. I hope you like it;).

It consists of 3 methods. You call the ToLiteral() method and supply it with the string you want to parse. It will subsequently call the other two methods to retreive the literals and the segments names. Forgive me for the poor comment -never been good in commenting-. I included a button click even handler to show you how to test for output and how to use the ToLiteral() method.

private void TestButton_Click(object sender, EventArgs e)
    {
        //Random numbers in a MessageBox to test output
        MessageBox.Show("4\n" + ToLiterals("4"));
        MessageBox.Show("57\n" + ToLiterals("57"));
        MessageBox.Show("209\n" + ToLiterals("209"));
        MessageBox.Show("8734\n" + ToLiterals("8734"));
        MessageBox.Show("24567\n" + ToLiterals("24567"));
        MessageBox.Show("973654\n" + ToLiterals("973654"));
        MessageBox.Show("2315736\n" + ToLiterals("2315736"));
        MessageBox.Show("27065154\n" + ToLiterals("27065154"));
        MessageBox.Show("827464876\n" + ToLiterals("827464876"));
        MessageBox.Show("1675376283\n" + ToLiterals("1675376283"));
    }

    private string ToLiterals(string Input)
    {
        String Output = "";
        while (Input.Length > 0)
        {
            if (Input[0] == '0')//Ignore zeros
            {
                Input = Input.Remove(0, 1);
                Output += "And ";
                continue;
            }
            if (Input.Length % 3 != 2)
            {
                // We are at the Xs if our number is XYX,XYX,XYX
                // Either first, or third position in each segment.
                Output += ConvertChar(Input[0].ToString());
                if (Input.Length % 3 == 0 && Input.Length > 2) //third position in a segment
                    Output += "Hundred ";
                else
                    Output += GetSegment(Input.Length / 3); //first position
                    Input = Input.Remove(0, 1);
                continue;
            }
            else
            {
                //We are talking about first and second positions togerther :YXX,YXX,YXX
                if (Input[0] == '1') //Is it Xteen? ie. 11-19
                {
                    Output += ConvertChar(Input.Substring(0, 2));

                }
                else //Or above 19?
                {
                    Output += ConvertChar(Input[0] + "0");
                    Output += ConvertChar(Input[1].ToString());
                }
                Output += GetSegment(Input.Length / 3);
                Input = Input.Remove(0, 2);
            }
        }
        return Output;
    }

    private string GetSegment(int seg)
    {
        switch (seg)
        {
            case 1: { return "Thousand "; }
            case 2: { return "Million "; }
            case 3: { return "Billion "; }
            case 4: { return "Trillion "; }
            default: { return ""; }
                //The list can grow
        }
    }

    private string ConvertChar(string ToConvert)
    {
        switch (ToConvert)
        {
            case "1": { return "One "; }
            case "2": { return "Two "; }
            case "3": { return "Three "; }
            case "4": { return "Four "; }
            case "5": { return "Five "; }
            case "6": { return "Six "; }
            case "7": { return "Seven "; }
            case "8": { return "Eight "; }
            case "9": { return "Nine "; }
            case "10": { return "Ten "; }
            case "11": { return "Eleven "; }
            case "12": { return "Twelve "; }
            case "13": { return "Thirteen "; }
            case "14": { return "Fourteen "; }
            case "15": { return "Fifteen "; }
            case "16": { return "Sixteen "; }
            case "17": { return "Seventeen "; }
            case "18": { return "Eighteen "; }
            case "19": { return "Nineteen "; }
            case "20": { return "Twenty "; }
            case "30": { return "Thirty "; }
            case "40": { return "Fourty "; }
            case "50": { return "Fifty "; }
            case "60": { return "Sixty "; }
            case "70": { return "Seventy "; }
            case "80": { return "Eighty "; }
            case "90": { return "Ninety "; }
            default: { return ""; }
        }
    }


PS.
It took me about 35mins to write and test the code. It may need some adjustments, but it should give you a good start.

RegardsRose | [Rose]

GeneralRe: Digit To text Converter Pin
Syed Shahid Hussain9-Sep-06 5:56
Syed Shahid Hussain9-Sep-06 5:56 
JokeRe: Digit To text Converter Pin
Nader Elshehabi9-Sep-06 6:59
Nader Elshehabi9-Sep-06 6:59 
GeneralRe: Digit To text Converter Pin
Syed Shahid Hussain10-Sep-06 8:50
Syed Shahid Hussain10-Sep-06 8:50 
AnswerRe: Digit To text Converter Pin
Arjun "Mjolnir" Bahree10-Sep-06 4:31
Arjun "Mjolnir" Bahree10-Sep-06 4:31 
GeneralRe: Digit To text Converter Pin
Syed Shahid Hussain10-Sep-06 8:54
Syed Shahid Hussain10-Sep-06 8:54 
QuestionHow to convert string Pin
papa808-Sep-06 6:25
papa808-Sep-06 6:25 
AnswerRe: How to convert string Pin
Not Active8-Sep-06 6:38
mentorNot Active8-Sep-06 6:38 
GeneralRe: How to convert string Pin
papa808-Sep-06 6:42
papa808-Sep-06 6:42 
GeneralRe: How to convert string Pin
KevinMac8-Sep-06 6:54
KevinMac8-Sep-06 6:54 
GeneralRe: How to convert string Pin
papa808-Sep-06 7:34
papa808-Sep-06 7:34 
QuestionDuplicate GUIDs? Pin
Xiangyang Liu 刘向阳8-Sep-06 6:15
Xiangyang Liu 刘向阳8-Sep-06 6:15 
AnswerRe: Duplicate GUIDs? Pin
Dustin Metzgar8-Sep-06 9:05
Dustin Metzgar8-Sep-06 9:05 
Questioncode to access the windows scheduled task ? Pin
Sunil B Muthanna8-Sep-06 5:47
Sunil B Muthanna8-Sep-06 5:47 
QuestionDesign Time issue of Windows Control? Pin
MudkiSekhon8-Sep-06 5:46
MudkiSekhon8-Sep-06 5:46 
AnswerRe: Design Time issue of Windows Control? Pin
ic3b3rg38-Sep-06 11:50
ic3b3rg38-Sep-06 11:50 
GeneralRe: Design Time issue of Windows Control? Pin
MudkiSekhon8-Sep-06 19:47
MudkiSekhon8-Sep-06 19:47 
QuestionHow to implement a singleton in c# generic classes Pin
Chris Richner8-Sep-06 5:19
Chris Richner8-Sep-06 5:19 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.