Members

Technology Zones

IBM Learning Center

Articles

Hosted By

MaximumASP

Info

[3915] The Ordinal Number Function

Last post 07-13-2008 11:09 AM by anonymous1. 6 replies.
Page 1 of 1 (7 items)
Sort Posts: Previous Next
  • 01-01-1999 12:00 AM

    [3915] The Ordinal Number Function

    This thread is for discussions of The Ordinal Number Function.

    • Post Points: 0
  • Advertisement

    • Red Gate Software

    Advertisement

    Want to boost your .NET application performance?

    Some developers always seem to write efficient and lightening-fast code. What is their secret? It’s ANTS Profiler. “We improved the performance of the application up to 10 times” Dan Ports, Intrigma.

    Try it for yourself now.

  • 12-29-2003 1:24 PM In reply to

    C# version

    public string GetOrdinal(int Number)
    {
       //Adapted from VB.NET version posted at http://www.developerfusion.com/show/3915/
       //The Ordinal Number Function
       //Karl Moore - http://www.karlmoore.com

       string suffix = String.Empty;
       // Accepts an integer, returns the ordinal suffix
       // Handles special case three digit numbers ending
       // with 11, 12 or 13 - ie, 111th, 112th, 113th, 211th, et al
       if(Number.ToString().Length > 2)
       {
           int intEndNum = Convert.ToInt32(Number.ToString().Substring(Number.ToString().Length - 2,2));
           if(intEndNum >=11 && intEndNum <= 13)
               switch(intEndNum)
               {
                   case 11:
                   case 12:
                   case 13:
                       suffix = "th";
                       break;
               }
       }

       if(Number >= 21)
       {
           //Handles 21st, 22nd, 23rd, et al
           int Number21 = Convert.ToInt32(Number.ToString().Substring(Number.ToString().Length -1,1));
           switch(Number21)
           {
               case 1:
                   suffix = "st";
                   break;
               case 2:
                   suffix = "nd";
                   break;
               case 3:
                   suffix = "rd";
                   break;
               case 0:
                   suffix = "th";
                   break;
               default:
                   for(int i = 4;i<=9;i++)
                   {
                       if(Number21 == i)
                       {
                           suffix = "th";
                           break;
                       }
                       else
                           suffix = String.Empty;
                   }
                   break;
           }
       }
       else
       {
           switch(Number)
           {
               case 1:
                   suffix = "st";
                   break;
               case 2:
                   suffix = "nd";
                   break;
               case 3:
                   suffix = "rd";
                   break;
               default:
                   for(int i = 4;i<=21;i++)
                   {
                       if(Number == i)
                       {
                           suffix = "th";
                           break;
                       }
                       else
                           suffix = String.Empty;
                   }
                   break;
           }
       }

       return suffix;
    }
    • Post Points: 10
  • 05-28-2005 3:48 PM In reply to

    • ianw
    • Not Ranked
    • Joined on 05-28-2005
    • New Member
    • Points 10

    A shorter version

    Here's a shorter version (that also returns 'th' for zero):

    Code:

       Private Function GetOrdinal2(ByVal Number As Integer) As String

           If ((Number Mod 100) \ 10) <> 1 Then
               If (Number Mod 10) = 1 Then
                   Return "st"
               ElseIf (Number Mod 10) = 2 Then
                   Return "nd"
               ElseIf (Number Mod 10) = 3 Then
                   Return "rd"
               Else
                   Return "th"
               End If
           Else
               Return "th"
           End If

       End Function

    • Post Points: 10
  • 02-09-2006 4:03 PM In reply to

    • jenisys
    • Not Ranked
    • Joined on 02-09-2006
    • New Member
    • Points 5

    Extra if block?

    First, thanks for already doing what I was about to do -- creating a C# version.

    In the C# version, you added a separate if for 21st, 22nd, 23rd.  Is there a reason?  It seems the code for 1st, 2nd, and 3rd would work just as well for 21st, 22nd , or 23rd.

    Just curious.

    Quote:
    [1]Posted by basicgeoff on 29 Dec 2003 01:24 PM[/1]
    public string GetOrdinal(int Number)
    {
       //Adapted from VB.NET version posted at http://www.developerfusion.com/show/3915/
       //The Ordinal Number Function
       //Karl Moore - http://www.karlmoore.com

       string suffix = String.Empty;
       // Accepts an integer, returns the ordinal suffix
       // Handles special case three digit numbers ending
       // with 11, 12 or 13 - ie, 111th, 112th, 113th, 211th, et al
       if(Number.ToString().Length > 2)
       {
           int intEndNum = Convert.ToInt32(Number.ToString().Substring(Number.ToString().Length - 2,2));
           if(intEndNum >=11 && intEndNum <= 13)
               switch(intEndNum)
               {
                   case 11:
                   case 12:
                   case 13:
                       suffix = "th";
                       break;
               }
       }

       if(Number >= 21)
       {
           //Handles 21st, 22nd, 23rd, et al
           int Number21 = Convert.ToInt32(Number.ToString().Substring(Number.ToString().Length -1,1));
           switch(Number21)
           {
               case 1:
                   suffix = "st";
                   break;
               case 2:
                   suffix = "nd";
                   break;
               case 3:
                   suffix = "rd";
                   break;
               case 0:
                   suffix = "th";
                   break;
               default:
                   for(int i = 4;i<=9;i++)
                   {
                       if(Number21 == i)
                       {
                           suffix = "th";
                           break;
                       }
                       else
                           suffix = String.Empty;
                   }
                   break;
           }
       }
       else
       {
           switch(Number)
           {
               case 1:
                   suffix = "st";
                   break;
               case 2:
                   suffix = "nd";
                   break;
               case 3:
                   suffix = "rd";
                   break;
               default:
                   for(int i = 4;i<=21;i++)
                   {
                       if(Number == i)
                       {
                           suffix = "th";
                           break;
                       }
                       else
                           suffix = String.Empty;
                   }
                   break;
           }
       }

       return suffix;
    }
    • Post Points: 0
  • 06-28-2007 6:59 PM In reply to

    • CMercs
    • Not Ranked
    • Joined on 06-28-2007
    • United States
    • New Member
    • Points 5

    Re: C# version

    Here is a shorter C# version;

    /// <summary>
    /// Method to properly suffix numbers with "st", "nd", "rd" or "th".
    /// </summary>
    /// <param name="iNumber">Number to add the suffix to.</param>
    /// <returns>Number as a properly suffixed string (i.e. 1st, 2nd, 3rd, 4th, 11th, 21st, etc.)</returns>
    /// <example><code>
    /// for (int i = 1; i &lt; 1305; i++)
    /// {
    ///     Console.WriteLine(Utility.GetOrdinal(i));
    /// }
    /// </code></example>
    public static string GetOrdinal(int iNumber)
    {
       string suf = "th";
       if (((iNumber % 100) / 10) != 1) //Handles 11, 12 & 13.  Only equals one if iNumber has a one in the ten digit.
       {
          switch (iNumber % 10) //Returns digit in the 1 column to evaluate.
          {
             case 1:
                suf = "st";
                break;
             case 2:
                suf = "nd";
                break;
             case 3:
                suf = "rd";
                break;
          }
       }
       return iNumber.ToString() + suf;
    }































     

    • Post Points: 5
  • 03-17-2008 3:17 AM In reply to

    • alexgn
    • Not Ranked
    • Joined on 03-17-2008
    • Kenya
    • New Member
    • Points 10

    Re: A shorter version

    ianw,

    You've done a very elegant version of the function. Here is the Transact SQL version.... 

    CREATE  FUNCTION GetOrdinalNumber(@number int)
    RETURNS nvarchar(100)
    BEGIN
             DECLARE @positiveNumber int
             DECLARE @ordinalNumber nvarchar(100)
             SET @positiveNumber = ABS(@number)
     
             SELECT @ordinalNumber = CASE WHEN ((@positiveNumber % 100) / 10) = 1  THEN 
                                                                  CONVERT(nvarchar(100),@positiveNumber) + 'th' 
                                                       ELSE 
                                                                  CASE WHEN (@number % 10) = 1 THEN 
                                                                              CONVERT(nvarchar(100),@positiveNumber) + 'st' 
                                                                  ELSE 
                                                                              CASE WHEN (@number % 10) = 2 THEN 
                                                                                      CONVERT(nvarchar(100),@positiveNumber) + 'nd' 
                                                                              ELSE 
                                                                                      CASE WHEN (@number % 10) = 3 THEN 
                                                                                                CONVERT(nvarchar(100),@positiveNumber) + 'rd' 
                                                                                       ELSE 
                                                                                                CONVERT(nvarchar(100),@positiveNumber) + 'th' 
                                                                                      END
                                                                               END
                                                                   END  
                                                        END
              RETURN @ordinalNumber
    END

    ianw:
    Here's a shorter version (that also returns 'th' for zero):

    Code:

       Private Function GetOrdinal2(ByVal Number As Integer) As String

           If ((Number Mod 100) \ 10) <> 1 Then
               If (Number Mod 10) = 1 Then
                   Return "st"
               ElseIf (Number Mod 10) = 2 Then
                   Return "nd"
               ElseIf (Number Mod 10) = 3 Then
                   Return "rd"
               Else
                   Return "th"
               End If
           Else
               Return "th"
           End If

       End Function

    • Post Points: 10
  • 07-13-2008 11:09 AM In reply to

    Re: A shorter version

    A shorter version of the TSQL code
    CREATE FUNCTION udf_GetOrdinalNumber(@number int)
    RETURNS nvarchar(100)
    BEGIN
    	DECLARE @positiveNumber int
    	DECLARE @ordinalNumber nvarchar(100)
    	SET @positiveNumber = ABS(@number)
    		SET @ordinalNumber = CAST(@positiveNumber as nvarchar(100))
     
    	SET @ordinalNumber = @ordinalNumber +
    			CASE 
    			WHEN ((@positiveNumber % 100) / 10) = 1  
    			THEN 
    				'th' 
    			ELSE 
    				CASE (@number % 10) 
    				WHEN 1 THEN 'st' 
    				WHEN 2 THEN 'nd' 
    				WHEN 3 THEN 'rd' 
    				ELSE 'th' 
    				END
    			END
    										  
    	RETURN @ordinalNumber
    END 

    • Post Points: 5
Page 1 of 1 (7 items)