views:

56

answers:

2
+1  A: 

Make it check the last digit of every number then add the suffix accordingly.

NSArray
It's not just the last digit that gives the ordination. 3 is different than 13, but the same as 133
iWasRobbed
+1  A: 

Here's a short snippet in another language: http://www.bytechaser.com/en/functions/b6yhfyxh78/convert-number-to-ordinal-like-1st-2nd-in-c-sharp.aspx

/// <summary>
/// Create an ordinal number from any number 
/// e.g. 1 becomes 1st and 22 becomes 22nd
/// </summary>
/// <param name="number">Number to convert</param>
/// <returns>Ordinal value as string</returns>
public static string FormatOrdinalNumber(int number)
{
    //0 remains just 0
    if (number == 0) return "0";
    //test for number between 3 and 21 as they follow a 
    //slightly different rule and all end with th            
    if (number > 3 && number < 21)
    {
        return number + "th";
    }
    //return the last digit of the number e.g. 102 is 2
    var lastdigit = number % 10;
    //append the correct ordinal val
    switch (lastdigit)
    {
        case 1: return number + "st";
        case 2: return number + "nd";
        case 3: return number + "rd";
        default: return number + "th";
    }
}
Kurbz
This fails for 112, etc. This would return 112nd, but it should be 112th.
Dave DeLong