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 Tens Rules

To apply the tens rules we must first determine the number of tens and units. This is achieved in a similar manner to determining the hundreds in the previous rules; the remainder from the previous rule is split using division and modulus operators. Once the number of tens and units are known, several nested if statements are used to add the correct text.

This code should be added after the hundreds rules to apply the tens rules and return the result:

// Determine the tens and units
int tens = tensUnits / 10;
int units = tensUnits % 10;

// Tens rules
if (tens >= 2)
    groupText += _tens[tens];
    if (units != 0)
        groupText += " " + _smallNumbers[units];
else if (tensUnits != 0)
    groupText += _smallNumbers[tensUnits];

return groupText;

Recombining the Three-Digit Groups

Now that the three-digit groups have all individually been converted into words, they must be recombined. This is achieved by starting with the lower value groups and adding each larger group to the combined string in turn. Where a group is not blank, the correct large number text from the large numbers array is appended. If a non-blank group has already been added, a comma or the word 'and' is also appended.

To control the decision of whether to add 'and' to the combined string, a Boolean variable is initialised after adding the first (smallest) three-digit group. If this group represents a number between one and ninety nine, the Boolean value is set to true, otherwise is it set to false. Whenever another group of digits is added that is non-zero the Boolean is checked. If true, the word 'and' is added. If the Boolean is false, a comma is appended instead.

To implement the recombination rules add the following code after the for loop in the public NumberToWords method:

// Recombine the three-digit groups
string combined = groupText[0];
bool appendAnd;

// Determine whether an 'and' is needed
appendAnd = (digitGroups[0] > 0) && (digitGroups[0] < 100);

// Process the remaining groups in turn, smallest to largest
for (int i = 1; i < 4; i++)
    // Only add non-zero items
    if (digitGroups[i] != 0)
        // Build the string to add as a prefix
        string prefix = groupText[i] + " " + _scaleNumbers[i];
        if (combined.Length != 0)
            prefix += appendAnd ? " and " : ", ";
        // Opportunity to add 'and' is ended
        appendAnd = false;

        // Add the three-digit group to the combined string
        combined = prefix + combined;

Applying the Negative Rule

The method is now almost complete. The remaining process is to apply the negative rule. To achieve this, the original value is rechecked to determine if it is positive or negative. If negative, the text is prefixed accordingly. The text is then returned from the method using a standard return statement.

Add the final lines of code to the NumberToWords method:

// Negative rule
if (number < 0)
    combined = "Negative " + combined;

return combined;
2 February 2007