How To Convert A Number Into String Format

Here I Am Showing You A Code Which Will Convert A Given Number Into String Format Like Ten For 10, Twenty Five For 25, etc....

Here I have created one function which will accept the user's given number as input parameter to the function. after that the function will perform the operation on that user's given number & convert it into string format.

Here is the code:
 

public static string NumberToString(int num)

  {

            if (num < 0)

                return "Minus " + NumberToString(-num);

            else if (num == 0)

                return "";

            else if (num <= 19)

                return new string[] {"One", "Two", "Thiree", "Four", "Five",

                                    "Six", "Seven", "Eigh", "Nine", "Ten",

                                    "Eleven", "Twelve", "Thirteen", "Fourteen",

                                    "Fifteen", "Sixteen", "Seventeen", "Eighteen",

                                    "Nineteen"}[num - 1] + " ";

            else if (num <= 99)

                return new string[] {"Twenty", "Thirty", "Forty", "Fifty", "Sixty","Seventy",

                        "Eighty", "Ninety"}[num / 10 - 2] + " " + NumberToString(num %10);

            else if (num <= 199)

                return "One Hundred " + NumberToString(num % 100);

            else if (num <= 999)

                return NumberToString(num / 100) + "Hundreds " + NumberToString(num% 100);

            else if (num <= 1999)

                return "One Thousand " + NumberToString(num % 1000);

            else if (num <= 999999)

                return NumberToString(num / 1000) + "Thousands " + NumberToStrin(num % 1000);

            else if (num <= 1999999)

                return "One Million " + NumberToString(num % 1000000);

            else if (num <= 999999999)

                return NumberToString(num / 1000000) + "Millions " + NumberToStrin(num % 1000000);

            else if (num <= 1999999999)

                return "One Billion " + NumberToString(num % 1000000000);

            else

                return NumberToString(num / 1000000000) + "Billions " + NumberToStrin(num % 1000000000);

    }

        private void button1_Click(object sender, EventArgs e)

        {

            string tmp;

            tmp = NumberToString(226);

            MessageBox.Show(tmp);

        }

The given function will return the string that's why you have to store the return value in some temporary string variable & you can display that to the user.v

Ebook Download
View all
Learn
View all