Home | Contact Us

CSharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript

C# Number Words

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

Number words. All numbers have English word representations.

We convert the number "1" to the word "one" using the C# language. Numbers can be represented as digits. But displaying English words for small numbers is desirable.

Example. 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.

Static Method

And: This calls into the NumberString.GetString method to convert the integers to words if appropriate.

C# program that converts some numbers to words

using System;

static class NumberString
    static string[] _words =

    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.



The NumberString class is a static class. The GetString method receives one formal parameter, an int. It sees if this int is between 0 and 10 inclusive. If the test evaluates to true, the English word "zero", "one", "two" are returned.

Discussion. In writing, editing guidelines will require that you type out numbers such as 0 to 10 as the words "zero" to "ten". The main benefit here is clarity. It is easier to tell the letter "l" from the word "one" and not the number "1".

Summary. We implemented text substitutions for small integers. The program implements this using a static array and lookup method, and then substitutes the result for certain parameters for an element in the lookup table.