Created
October 4, 2010 15:25
-
-
Save PJensen/609873 to your computer and use it in GitHub Desktop.
private static string nth(int n)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// <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"; | |
} | |
} | |
} |
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
"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.