TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

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 =
    {
	"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

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.

Plurals


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf