Community discussion forum

[3915] The Ordinal Number Function

Tags:
  • 9 years ago

    This thread is for discussions of The Ordinal Number Function.

  • Advertisement

    Simply the fastest line-level profiler for .NET ever

    “The low overhead means it has minimal impact on the execution of my program”
    Mark Everest, Development Team Leader, Renault F1 Team Ltd.

    Try out the new ANTS Profiler 4 for yourself. Download your 14-day trial now

  • 4 years ago

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

  • 3 years ago

    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


  • 2 years ago

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

  • 1 year ago

    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 a reply

Enter your message below

Sign in or Join us (it's free).