BlackWaspTM

This web site uses cookies. By using the site you accept the cookie policy.This message is for compliance with the UK ICO law.

Algorithms and Data Structures
.NET 1.1+

Convert A Number into Words

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.

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];
}

Separating the Number into Three-Digit Groups

Once it has been determined that the value is non-zero, it is necessary to split the number into its three-digit groups. This is relatively easy using a mixture of the modulus (%) and division (/) operators. When any integer is divided by one thousand, the remainder supplied by the modulus operator is equivalent to the last three digits of the original value. This can be repeated to extract the three-digit groups working from right to left.

One problem with this method is that if the original value is negative the modulus values will also be negative. This can be rectified by using the Abs method from the standard Math class. This method converts negative values to their positive equivalents but leaves positive values unchanged.

The range provided by the integer data type ensures that there will never be more than four three-digit groups. We can therefore create an array with four elements to hold the extracted values. Theis array is populated using a simple for loop that executes once for each group.

The separation is achieved by adding the following code after the zero rule:

// Array to hold four three-digit groups
int[] digitGroups = new int[4];

// Ensure a positive number to extract from
int positive = Math.Abs(number);

// Extract the three-digit groups
for (int i = 0; i < 4; i++)
{
    digitGroups[i] = positive % 1000;
    positive /= 1000;
}

NB: The three-digit group array index numbers can be matched against the large number array indices. This will be useful for recombination later.

Converting a Three-Digit Group

The next rules that need to be applied are those concerning the individual three-digit groups. Each group in turn needs to be converted to text. To achieve this with efficient code, a private method is to be created. This method, named ThreeDigitGroupToWords, accepts an integer value of no more than three digits and returns the equivalent English text.

The new subroutine is called once for each of the three-digit groups using another for loop. For each pass, the returned text is stored in a new array of strings. The index numbers of the three-digit group values and their new text versions are kept synchronised.

Add the following code after the digit extraction loop to call the new private method:

// Convert each three-digit group to words
string[] groupText = new string[4];

for (int i = 0; i < 4; i++)
{
    groupText[i] = ThreeDigitGroupToWords(digitGroups[i]);
}

To declare the new method, add the following declaration to the class:

// Converts a three-digit group into English words
private string ThreeDigitGroupToWords(int threeDigits)
{
}

Applying The Hundreds Rules

In order to apply the hundreds rules, there are two key values that must be known. The first value is the number of hundreds that exist in the three-digit group. This can be determines by simply dividing the three-digit value by one hundred. The second number that is required is the remainder of this division. This value, extracted using the modulus operator, represents the other two digits in the group.

The first of the hundreds rules determines if the number of hundreds is included in the returned text. A simple if statement checks if this value is non-zero and adds text accordingly. Otherwise, the digit is converted to text, using the appropriate string from the small numbers array, and the word 'Hundred' is appended.

A second conditional statement determines if there are any tens or units within the three-digit group. If there are, these will be applied by the tens rules later. However, the word 'and' will be required between the hundreds and the tens and units and this is added immediately. Where the three-digit group represents an exact number of hundreds only, the remainder is zero and the 'and' is note required.

This code is added within the ThreeDigitGroupToWords method to apply the hundreds rules:

// Initialise the return text
string groupText = "";

// Determine the hundreds and the remainder
int hundreds = threeDigits / 100;
int tensUnits = threeDigits % 100;

// Hundreds rules
if (hundreds != 0)
{
    groupText += _smallNumbers[hundreds] + " Hundred";

    if (tensUnits != 0)
    {
        groupText += " and ";
    }
}
2 February 2007