Function to get the Ordinal Numbers

        public string GetOrdinal(int Number)
        {
            if (((Number % 100) / 10) != 1)
            {
                if ((Number % 10) == 1)
                    return "st";
                else if ((Number % 10) == 2)
                    return "nd";
                else if ((Number % 10) == 3)
                    return "rd";
                else
                    return "th";
            }
            else
                return "th";
        }

Comments

# re: Function to get the Ordinal Numbers

Wednesday, September 30, 2009 8:22 AM by Sonu

This would have been a perfect fit for our code-snippet library ;)

dotnetslackers.com/.../snippetlist.aspx

# re: Function to get the Ordinal Numbers

Monday, October 05, 2009 7:30 AM by Sangam Uprety

I tried the code and it works well. Interestingly it works without this line also:

if (((Number % 100) / 10) != 1)

Could you illustrate the meaning of this line please? Thank you.