Converting Integers to Written Numbers in C#

Have you ever found yourself needing to convert integers to their written form? This is a common requirement in various applications, such as generating reports, writing checks, or simply making numbers easier to read in textual content. For instance, you might need to convert the number 21 to "Twenty One".

But what if you want a solution that doesn’t rely on a huge lookup table? In this blog post, we’ll explore a tidy and efficient method in C# for converting integers into written numbers.

The Problem

To convert integers to written form, many developers typically resort to using sizable lookup tables, which can be cumbersome and challenging to maintain. Instead, we’ll explore a more elegant solution using basic string manipulation and recursion.

The Solution

We can achieve our goal with a custom class that uses arrays to categorize numbers and a recursive function to build the written form incrementally. Let’s break it down step-by-step.

The HumanFriendlyInteger Class

The following class contains the method to convert integers into their written equivalents:

public static class HumanFriendlyInteger
{
    static string[] ones = new string[] { "", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine" };
    static string[] teens = new string[] { "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen" };
    static string[] tens = new string[] { "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety" };
    static string[] thousandsGroups = { "", " Thousand", " Million", " Billion" };

    private static string FriendlyInteger(int n, string leftDigits, int thousands)
    {
        // Handling special cases based on the number's value
        if (n == 0)
        {
            return leftDigits;
        }

        string friendlyInt = leftDigits;

        if (friendlyInt.Length > 0)
        {
            friendlyInt += " ";
        }

        if (n < 10)
        {
            friendlyInt += ones[n];
        }
        else if (n < 20)
        {
            friendlyInt += teens[n - 10];
        }
        else if (n < 100)
        {
            friendlyInt += FriendlyInteger(n % 10, tens[n / 10 - 2], 0);
        }
        else if (n < 1000)
        {
            friendlyInt += FriendlyInteger(n % 100, (ones[n / 100] + " Hundred"), 0);
        }
        else
        {
            friendlyInt += FriendlyInteger(n % 1000, FriendlyInteger(n / 1000, "", thousands + 1), 0);
            if (n % 1000 == 0)
            {
                return friendlyInt;
            }
        }

        return friendlyInt + thousandsGroups[thousands];
    }

    public static string IntegerToWritten(int n)
    {
        if (n == 0)
        {
            return "Zero";
        }
        else if (n < 0)
        {
            return "Negative " + IntegerToWritten(-n);
        }

        return FriendlyInteger(n, "", 0);
    }
}

Breakdown of the Code

  • Static Arrays: The class uses four static arrays to store written forms of numbers:

    • ones: Contains words for numbers 1-9.
    • teens: Contains words for numbers 10-19.
    • tens: Contains words for multiples of ten (20, 30, etc.).
    • thousandsGroups: Holds words for higher values like thousand, million, etc.
  • FriendlyInteger Method: This recursive method breaks down the number into manageable parts:

    • It handles the constructs of the numbers based on their sizes (ones, teens, tens, hundreds, and thousands).
    • Each call recursively builds the string representation by checking which category the number falls into.
  • IntegerToWritten Method: This is the public method for converting integers. It handles zero and negative numbers before calling the FriendlyInteger method.

Conclusion

By using this method, you can efficiently convert integers to their written form in C#. This solution is not only elegant but also avoids the hassle of maintaining vast lookup tables.

Next time you need to display numbers in text, give this approach a try, and enjoy the clean, human-friendly output!