0

Now I am writing a form. There is a textBox and Label. When I push a button, I want label text became int value from textBox. If I wrote 8, I want it to say eight, If I wrote 832, I want it to say eight hundered thirty two. How can I do that?

  • https://www.nuget.org/packages/numbers-to-words/ try this package or some of the similar ones – Sten Petrov Apr 08 '22 at 16:41
  • Does this answer your question? [converting numbers in to words C#](https://stackoverflow.com/questions/2729752/converting-numbers-in-to-words-c-sharp) – Gnyasha Apr 08 '22 at 17:17
  • See this method which is short and simple using my SLST method (hope it helps): https://stackoverflow.com/questions/554314/how-can-i-convert-an-integer-into-its-verbal-representation/71742126#71742126 – Mohsen Alyafei Apr 12 '22 at 21:08

1 Answers1

1

You could use this code from this source: Program to convert a given number to words

// C# program to print a given

// number in words. The program

// handles numbers from 0 to 9999

using System;

class GFG {

// A function that prints
// given number in words
static void convert_to_words(char[] num)
{
    // Get number of digits
    // in given number
    int len = num.Length;

    // Base cases
    if (len == 0) {
        Console.WriteLine("empty string");
        return;
    }
    if (len > 4) {
        Console.WriteLine("Length more than "
                        + "4 is not supported");
        return;
    }

    /* The first string is not used,
    it is to make array indexing simple */
    string[] single_digits = new string[] {
        "zero", "one", "two", "three", "four",
        "five", "six", "seven", "eight", "nine"
    };

    /* The first string is not used,
    it is to make array indexing simple */
    string[] two_digits = new string[] {
        "",      "ten",  "eleven", "twelve",
        "thirteen", "fourteen", "fifteen", "sixteen",
        "seventeen", "eighteen", "nineteen"
    };

    /* The first two string are not used,
    they are to make array indexing simple*/
    string[] tens_multiple = new string[] {
        "",  "",     "twenty", "thirty", "forty",
        "fifty", "sixty", "seventy", "eighty", "ninety"
    };

    string[] tens_power
        = new string[] { "hundred", "thousand" };

    /* Used for debugging purpose only */
    Console.Write((new string(num)) + ": ");

    /* For single digit number */
    if (len == 1) {
        Console.WriteLine(single_digits[num[0] - '0']);
        return;
    }

    /* Iterate while num
        is not '\0' */
    int x = 0;
    while (x < num.Length) {

        /* Code path for first 2 digits */
        if (len >= 3) {
            if (num[x] - '0' != 0) {
                Console.Write(
                    single_digits[num[x] - '0'] + " ");
                Console.Write(tens_power[len - 3]
                            + " ");

                // here len can be 3 or 4
            }
            --len;
        }

        /* Code path for last 2 digits */
        else {
            /* Need to explicitly handle
            10-19. Sum of the two digits
            is used as index of "two_digits"
            array of strings */
            if (num[x] - '0' == 1) {
                int sum = num[x] - '0' + num[x + 1] - '0';
                Console.WriteLine(two_digits[sum]);
                return;
            }

            /* Need to explicitly handle 20 */
            else if (num[x] - '0' == 2
                    && num[x + 1] - '0' == 0) {
                Console.WriteLine("twenty");
                return;
            }

            /* Rest of the two digit
            numbers i.e., 21 to 99 */
            else {
                int i = (num[x] - '0');
                if (i > 0)
                    Console.Write(tens_multiple[i]
                                + " ");
                else
                    Console.Write("");
                ++x;
                if (num[x] - '0' != 0)
                    Console.WriteLine(
                        single_digits[num[x] - '0']);
            }
        }
        ++x;
    }
}

// Driver Code
 public static void Main()
 {
    convert_to_words("9923".ToCharArray());
    convert_to_words("523".ToCharArray());
    convert_to_words("89".ToCharArray());
    convert_to_words("8".ToCharArray());
  }
}
// This code is contributed

// by Mits

This is some outputs based on the code :

9923: nine thousand nine hundred twenty three
523: five hundred twenty three
89: eighty nine
8989: eight thousand nine hundred eighty nine
Ahmad Javadi Nezhad
  • 527
  • 1
  • 5
  • 23
  • 1
    I added a little bit so I can use numbers with 5 digits. I am gonna try to add fractional numbers. Thanks. – Kardok_Delikaya Apr 09 '22 at 10:39
  • 1
    If we want to be correct, the correct English grammar when writing the compounded numbers from 21 to 99 is with a hyphen: "Twenty-One", "Thirty-Five", "Ninety-Nine", etc. The code above does not comply 100% with English grammar rules. Good job though. – Mohsen Alyafei Apr 12 '22 at 21:05
  • @AhmadJavadiNejad Please see this method which is short and simple using my SLST method (hope it helps): https://stackoverflow.com/questions/554314/how-can-i-convert-an-integer-into-its-verbal-representation/71742126#71742126 – Mohsen Alyafei Apr 12 '22 at 21:09
  • @MohsenAlyafei Of course, but that must not be hard to change and add these extra things. – Ahmad Javadi Nezhad Apr 12 '22 at 21:55
  • @AhmadJavadiNejad true, but that adds additional code because you have to check for the difference between 20 and 21/22/23 and between 30 and 31/32/33, etc. – Mohsen Alyafei Apr 12 '22 at 23:45