Home
C#
Number Words
Updated Sep 12, 2023
Dot Net Perls
intMain argsstatic Array

Number words

Consider that all numbers have English word representations. We convert the number "1" to the word "one" using the C# language.

Problem notes

Numbers can be represented as digits. But displaying English words for small numbers is desirable. This can help user interfaces.

Input and output

This class handles small numbers specifically—large numbers like "six thousand thirty seven" are not supported.

INPUT:  1
OUTPUT: "one"

Example code

The NumberString class contains the static data and static method used to convert integers to English words. We invoke a method from NumberString in Main().

Info The NumberString.GetString method converts the integers to words if appropriate.
Warning This class does not support every case, and it (most importantly) only supports small integers.
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));
    }
}
zero five ten 100

Method notes

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.

And If the test evaluates to true, the English word "zero", "one", "two" are returned.

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

Info 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. We used a static array and a lookup method. The program handles certain parameters with the lookup table.

Dot Net Perls is a collection of pages with code examples, which are updated to stay current. Programming is an art, and it can be learned from examples.
Donate to this site to help offset the costs of running the server. Sites like this will cease to exist if there is no financial support for them.
Sam Allen is passionate about computer languages, and he maintains 100% of the material available on this website. He hopes it makes the world a nicer place.
No updates found for this page.
Home
Changes
© 2007-2025 Sam Allen