mostlylucid

scott galloway's personal blog...
posts - 911, comments - 723, trackbacks - 11

My Links

News

Archives

Post Categories

Misc. Coding

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

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;
            }
        }
    }

Print | posted on Saturday, November 22, 2003 7:32 AM | Filed Under [ .NET Code Snippets ]

Feedback

Gravatar

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

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";
}
11/25/2003 12:00 AM | Philip Rieck
Gravatar

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

Tell me about it, you're completely correct of course...updated the post to reflect the problem (and include your version)
11/25/2003 12:26 AM | Scott Galloway
Gravatar

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

When the errors flow in from that version, you can blame me :)
11/25/2003 7:29 AM | Philip Rieck
Comments have been closed on this topic.

Powered by: