Back to "Dumb piece of code...generate ordinals for numbers (th,rd etc...)"

This is a viewer only at the moment see the article on how this works.

To update the preview hit Ctrl-Alt-R (or ⌘-Alt-R on Mac) or Enter to refresh. The Save icon lets you save the markdown file to disk

This is a preview from the server running through my markdig pipeline

Imported mostlylucidcouk

Dumb piece of code...generate ordinals for numbers (th,rd etc...)

Saturday, 03 July 2004

UPDATE: My code below of course has an error (I wrote it to test the perceptive skills of the community ;-)) anyway, Philip Reick commented with a more credible version:

Hmm.. my bet is you came across it again after forgetting it because you threw it away. It would return the wrong result for inputs like "42", "123", "101", etc. I've done that a lot - I save code for some reason even when it's incorrect (like, its the vendor supplied code, or it has some trick in it), then a year later I find it and have to test it all over again because I didn't record WHY I saved it. Useless way to save things, but I have a few bad habits.

Here's one I use often (well, sorta - it's off the top of the head, no compiler here)

private static string GetOrdinal(int number) { int place = number % 10; if( place > 3) return "th";

//special case for teens int teen = number % 100; if(teen < 20 && teen > 10) { return "th"; } if( place == 1) return "st"; if( place == 2) return "nd"; if( place == 3) return "rd"; return "th";

OK, not the most complex piece of code on the planet, came across it when reinstalling my broken machine.It strikes me as something the Framework may have built in, but I couldn't locate it...Anyway, given an integer, this snippet will return a string with the correct ordinal.

THIS CODE IS INCORRECT! Retained for amusement purposes only :0(
public class OrdinalNumber
    {
        public static string  GetOrdinal(int Number) 
        {
        
            try
            {
                string numStr = Number.ToString();
                // Accepts an integer, returns the ordinal suffix
                //  special case three digit numbers ending
                // with 11, 12 or 13 - ie, 111th, 112th, 113th, 211th, et al
                if (numStr.Length > 2 ) 
                {
                    int intEndNum = Convert.ToInt32(numStr.Substring(numStr.Length - 2, 2));
                    if ( intEndNum >= 11 && intEndNum <= 13 ) return "th";
                        
                }
                if ( Number >= 21 && Number <=23) 
                {
                    //  21st, 22nd, 23rd, et al
                    switch (Convert.ToInt32(numStr.Substring(numStr.Length - 1, 1))) 
                    {
                        case 1: return "st";
                        case 2: return "nd";
                        case 3:    return "rd";
                        default: return "th";
                    }
                } 
                else if(Number >=1 && Number <=3)
                {
                    //  1st to 20th
                    switch (Number) 
                    {
                        case 1: return "st";
                        case 2:    return "nd";
                        case 3:    return "rd";
                        default: return "th";
                    }
                }
                else
                {
                    return "th";
                }
            }
            catch(Exception)
            {
                return string.Empty;
            }
        }
    }
logo

© 2025 Scott Galloway — Unlicense — All content and source code on this site is free to use, copy, modify, and sell.