Created
May 8, 2012 07:56
-
-
Save jmcd/2633365 to your computer and use it in GitHub Desktop.
Ordinal suffix of an integer
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
public static class IntExtensions | |
{ | |
public static string GetOrdinalSuffix(this int @this) | |
{ | |
switch (@this % 100) | |
{ | |
case 11: | |
case 12: | |
case 13: | |
return "th"; | |
default: | |
switch (@this % 10) | |
{ | |
case 1: | |
return "st"; | |
case 2: | |
return "nd"; | |
case 3: | |
return "rd"; | |
default: | |
return "th"; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment