Home >Backend Development >C++ >How Can I Convert Numbers to Words in C#?

How Can I Convert Numbers to Words in C#?

Barbara Streisand
Barbara StreisandOriginal
2025-01-24 14:11:08526browse

How Can I Convert Numbers to Words in C#?

Number to text conversion in C#

Converting numeric values ​​to their corresponding literal form is a common task in programming. This conversion is particularly useful in a variety of scenarios, such as generating invoices, automating voice announcements, or processing voice commands.

One approach is to use a combination of modular arithmetic, conditional statements, and predefined arrays or dictionaries to map numbers and groups of numbers to their corresponding literal representations. Here is an example implemented in C#:

<code class="language-csharp">public static string NumberToWords(int number)
{
    // 处理零和负数的特殊情况
    if (number == 0)
        return "zero";
    else if (number < 0)
        return "minus " + NumberToWords(-number);

    string words = "";

    // 处理百万
    if (number >= 1000000)
    {
        words += NumberToWords(number / 1000000) + " million ";
        number %= 1000000;
    }

    // 处理千位
    if (number >= 1000)
    {
        words += NumberToWords(number / 1000) + " thousand ";
        number %= 1000;
    }

    // 处理百位
    if (number >= 100)
    {
        words += NumberToWords(number / 100) + " hundred ";
        number %= 100;
    }

    // 处理剩余数字
    if (number > 0)
    {
        if (words != "")
            words += "and ";

        string[] units = { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen" };
        string[] tens = { "zero", "ten", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety" };

        if (number < 20)
            words += units[number];
        else
        {
            words += tens[number / 10];
            if (number % 10 > 0)
                words += "-" + units[number % 10];
        }
    }

    return words.Trim();
}</code>

To use this method, simply provide an integer as a parameter and it will return the literal equivalent of that number. For example, NumberToWords(123) will return "one hundred and twenty-three" and NumberToWords(-543) will return "minus five hundred and forty-three". The code has been improved to handle the use of the connective "and" and clearer handling of negative numbers and zero. Trim() method is used to remove extra spaces.

The above is the detailed content of How Can I Convert Numbers to Words in C#?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn