C# Number Words

String type

All numbers have English word representations. We convert the number "1" to the word "one" using the C# programming language. While numbers can be represented as digits, displaying English words for small numbers is desirable for readability and usability.

Example

Note

First we introduce two classes: the NumberString type contains the static data and static method used to convert integers to English words. The Program class introduces the Main entry point, which calls into the NumberString.GetString method to convert the integers to words if appropriate.

This C# example converts numbers to English words with a static string array.

Program that converts some numbers to words [C#]

using System;

static class NumberString
{
    static string[] _words =
    {
	"zero",
	"one",
	"two",
	"three",
	"four",
	"five",
	"six",
	"seven",
	"eight",
	"nine",
	"ten"
    };
    public static string GetString(int value)
    {
	// See if the number can easily be represented by an English word.
	// ... Otherwise, return the ToString result.
	if (value >= 0 &&
	    value <= 10)
	{
	    return _words[value];
	}
	return value.ToString();
    }
}

class Program
{
    static void Main()
    {
	// Call the GetString method to get number words.
	Console.WriteLine(NumberString.GetString(0));
	Console.WriteLine(NumberString.GetString(5));
	Console.WriteLine(NumberString.GetString(10));
	Console.WriteLine(NumberString.GetString(100));
    }
}

Output

zero
five
ten
100

Description. The NumberString class is a static class, which prohibits instantiation with an instance constructor. The GetString method receives one formal parameter, an integer; it tests to see if this integer is between 0 and 10 inclusive. If the test evaluates to true, the English word "zero", "one", "two", etc. are returned.

Editing guidelines

Programming tip

When composing texts, editing guidelines will require that you type out numbers such as 0 to 10 as the words "zero" to "ten". The main benefit here, besides convention, is that it is easier to tell the letter "l" from the word "one" and not the number "1".

Summary

The C# programming language

We examined a static class that implements text substitutions for small integers when converting them to strings. The program implements this using a static array and a public static lookup method, and then substitutes the result for certain parameters for an element in the lookup table. Another mechanism you can use to modify strings based on numbers is pluralization of nouns, which this site also describes.

Plural Words Number Examples
.NET