Make it check the last digit of every number then add the suffix accordingly.
NSArray
2010-07-23 16:44:12
Make it check the last digit of every number then add the suffix accordingly.
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";
}
}