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 to an Ordinal Number

Ordinal numbers represent the rank of a cardinal number, such as 1st, 2nd and 3rd. These numbers define the position or rank of a value, rather than just the size or quantity. In this article we will convert integers into English ordinal numbers.

Ordinal Numbers

An ordinal number indicates the position of an item in a sequence, such as first, second or third. Ordinal numbers are related to their cardinal number counterparts. In English, it is usual to add two letters to a cardinal number to represent an ordinal number. For example, the cardinal numbers 1, 2 and 3 have the ordinal versions 1st, 2nd and 3rd.

In this article we will create a simple function to convert an integer to an ordinal number. We will achieve this by selecting the correct two-character suffix and adding it to the ordinal number in a string.

Converting Numbers to Ordinal Numbers

Creating the Method

There are several parts to the process for converting a cardinal number to an ordinal number in a C# method. We will begin by creating a method that accepts an integer and returns a string. This method could be included directly in a program or in a class library. In this case, for simplicity we will use a console application. Start by creating a new console application and add a static method to the Program class as follows:

static string Ordinal(int cardinal)
{
}

Checking for Invalid Numbers

As ordinal numbers are used to specify the position of an item in a sequence, logically they can only represent positive integers. We therefore need to decide upon the expected result if the cardinal number being converted is zero or negative. In this case, we will throw an exception if such a value is passed to the method. A suitable exception type is the built-in ArgumentException.

To add the test and the exception, add the following line to the method:

if (cardinal <= 0) throw new ArgumentException("Cardinal must be positive.");

Extracting the Last Two Digits of the Number

The suffix that will be applied to the cardinal number to produce the ordinal version will be selected based upon the last two digits of the number. In some cases both digits will be required. In other cases only the last digit is significant. We can obtain the last two digits of the number and the last digit of the number using the modulus operator (%). We will assign these two values to two variables.

To add the variables and obtain the values, add the following lines to the method:

int lastTwoDigits = cardinal % 100);
int lastDigit = lastTwoDigits % 10;

Checking the Final Digit

In most cases, the final digit of the number will determine the suffix that is selected. If the last digit is one, the suffix will almost always be "st". For a final digit of two, the suffix will generally be "nd". If the last digit is three, a suffix of "rd" will usually be correct. For any other final digit, we will add "th" to the cardinal number.

We can create a variable to hold the suffix and use a switch statement to assign its value by adding the following:

string suffix;
switch (lastDigit)
{
    case 1:
        suffix = "st";
        break;

    case 2:
        suffix = "nd";
        break;

    case 3:
        suffix = "rd";
        break;

    default:
        suffix = "th";
        break;
}

Special Cases

There are three special cases where the final digit does not control the suffix. These are for the numbers 11, 12 and 13. In each case, a suffix of "th" is preferred, as in eleventh, twelfth and thirteenth. Add the following lines to the code to check for these values and change the suffix accordingly. The return statement combines the cardinal number with the selected suffix using the string.Format method and returns it to the calling function. This code completes the method.

if (11 <= lastTwoDigits && lastTwoDigits <= 13)
{
    suffix = "th";
}

return string.Format("{0}{1}", cardinal, suffix);

Testing the Method

We can test the conversion method by modifying the Main method of the program. The following Main method outputs all of the ordinal number representations of the values between one and two hundred.

static void Main()
{
    for (int i = 1; i <= 200; i++)
    {
        Console.Write("{0}\t", Ordinal(i));
    }
}
17 March 2009