Skip to content

Instantly share code, notes, and snippets.

@PJensen
Created October 4, 2010 15:25
Show Gist options
  • Save PJensen/609873 to your computer and use it in GitHub Desktop.
Save PJensen/609873 to your computer and use it in GitHub Desktop.
private static string nth(int n)
/// <summary>
/// Given a number (n) returns a string referring to that number (n)
/// using (st), (nd), (th), (rd) as suffixes.
/// </summary>
/// <param name="n">The number to convert to nth notation.</param>
/// <returns>The passed number in nth notation.</returns>
private static string nth(int n)
{
switch (n) {
case 11:
case 12:
case 13:
case 14:
return n + "th";
default:
switch (n % 10) {
case 1:
return n + "st";
case 2:
return n + "nd";
case 3:
return n + "rd";
default:
return n + "th";
}
}
}
@defeated
Copy link

defeated commented Oct 4, 2010

"teens" are a failing edge case here. (nth(11) => "11st" instead of "11th", and so on)

Here is Ruby's implementation of the ordinalize method.

@PJensen
Copy link
Author

PJensen commented Oct 6, 2010

Just these 4 needed to be patched up. Ty.
case 11:
case 12:
case 13:
case 14:
return n + "th";

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment