BlackWaspTM
Algorithms and Data Structures
.NET 1.1+

Convert A Number into Words (2)

When developing commercial applications, particularly when those applications perform activities such as cheque printing, it can be necessary to display numeric values using the equivalent text. This article describes an algorithm to achieve this.

The Algorithm and the Code

Preparation

Depending upon how you wish to use this algorithm, you may want to create it in a new class, incorporate it in an existing class or simply hide it behind a form event. To keep the code simple and short, this article assumes you have a class of some kind prepared and are adding a new method to that class. The code simply declares a new public method that accepts the integer to be converted as its only parameter.

To prepare, create or identify the class that you will add the method to, then add the following declaration code:

// Converts an integer value into English words
public string NumberToWords(int number)
{
}

NB: If you are creating or adding to a utilities class, you may wish to adjust the declaration to make the method static. If you do, all of the other declarations detailed below must also be marked as static.

Declaring the Names for Numbers

There are three key groups of names for numbers that must be declared. Firstly there are the small numbers between zero and nineteen that are used by the tens rule. The second group of numbers is used to represent the tens values from twenty to ninety. Finally we have the scale numbers. These are the large numbers that are used during recombination.

The number names must be stored within the program code in a manner that permits easy identification. In this example code, we will declare three arrays. An array of twenty items holds the small numbers, an array of ten items is used for the tens values (with two initial unused entries) and an array of four values contains the scale number names with the first entry containing an empty string.

As the code will eventually include private methods in addition to the public method already declared, the arrays are created as private class-level variables. This allows the arrays to be accessible by the entire class without being passed between functions. The numbers are defined in Title Case as this is aesthetically pleasing and can be easily converted to upper or lower case after the process is complete.

Add the following declarations to the class to declare and initialise the arrays:

// Single-digit and small number names
private string[] _smallNumbers = new string[]
    { "Zero", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine",
      "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen",
      "Eighteen", "Nineteen"};
     
// Tens number names from twenty upwards
private string[] _tens = new string[]
    { "", "", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"};
     
// Scale number names for use during recombination
private string[] _scaleNumbers = new string[] {"", "Thousand", "Million", "Billion"};

Applying the Zero Rule

The zero rule is the easiest to implement. All that is required is a simple if statement to test for a value of zero in the integer parameter. If the value is zero, the text returned is 'Zero' (from item 0 in the small numbers array). In all other cases, the remaining rules must all be considered.

To implement the zero rule, the following code is added within the NumberToWords method:

// Zero rule
if (number == 0)
{
    return _smallNumbers[0];
}
2 February 2007