Results 1 to 5 of 5

Thread: Easy Function to add a suffix to a number ie, 1st, 2nd, 3rd, etc..

  1. #1

    Thread Starter
    Frenzied Member conipto's Avatar
    Join Date
    Jun 2005
    Location
    Chicago
    Posts
    1,175

    Easy Function to add a suffix to a number ie, 1st, 2nd, 3rd, etc..

    Hey, this isn't rocket science, but it took me a good twenty minutes of thought.. I think this should cover all positive integers from 0-infinity.

    VB Code:
    1. Private Function addsuffix(ByVal num As Integer) As String
    2.         Dim suff As String
    3.         If num < 0 Then Return "Error"
    4.         If num < 20 Then
    5.             Select Case num
    6.                 Case 1 : suff = "st"
    7.                 Case 2 : suff = "nd"
    8.                 Case 3 : suff = "rd"
    9.                 Case 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19 : suff = "th"
    10.             End Select
    11.         Else
    12.             Select Case Convert.ToString(num).Chars(Convert.ToString(num).Length - 1)
    13.                 Case "1" : suff = "st"
    14.                 Case "2" : suff = "nd"
    15.                 Case "3" : suff = "rd"
    16.                 Case Else : suff = "th"
    17.             End Select
    18.         End If
    19.         AddSuffix = Convert.ToString(num) + suff
    20. End Function

    Bill
    Hate Adobe Acrobat? My Codebank Sumbissions - Easy CodeDom Expression evaluator: (VB / C# ) -- C# Scrolling Text Display

    I Like to code when drunk. Don't say you weren't warned.

  2. #2
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,683

    Re: Easy Function to add a suffix to a number ie, 1st, 2nd, 3rd, etc..

    I rewrote this for C# here...

    The VB version of this rewrite is thus:

    VB Code:
    1. Private Function AddOrdinalSuffix(ByVal num As Integer) As String
    2.  
    3.         'can handle negative numbers (-1st, -12th, -21st)
    4.  
    5.         Dim last2Digits As Integer = Math.Abs(num Mod 100)
    6.         Dim lastDigit As Integer = last2Digits Mod 10
    7.  
    8.         'the only nonconforming set is numbers ending in <...eleventh, ...twelfth, ...thirteenth>
    9.         'all other numbers are as 1 thru 10 */
    10.         If ((last2Digits >= 11 AndAlso last2Digits <= 13) OrElse lastDigit > 3) Then lastDigit = 0
    11.  
    12.         'lastdigit is now a number in range <   0,    1,    2,    3>
    13.         Return num.ToString() + New String() {"th", "st", "nd", "rd"}(lastDigit)
    14.  
    15.     End Function

    Can anyone do this in less code?
    Last edited by wossname; Jul 4th, 2005 at 05:35 AM.
    I don't live here any more.

  3. #3

    Thread Starter
    Frenzied Member conipto's Avatar
    Join Date
    Jun 2005
    Location
    Chicago
    Posts
    1,175

    Re: Easy Function to add a suffix to a number ie, 1st, 2nd, 3rd, etc..

    (num mod 100) was a good idea to get the last two digits. Much cleaner code then mine. I'm still learning the power of OOP.. Great feedback for something so trivial, it helps me sharpen my grasp of good programming practices.

    Bill
    Hate Adobe Acrobat? My Codebank Sumbissions - Easy CodeDom Expression evaluator: (VB / C# ) -- C# Scrolling Text Display

    I Like to code when drunk. Don't say you weren't warned.

  4. #4

    Thread Starter
    Frenzied Member conipto's Avatar
    Join Date
    Jun 2005
    Location
    Chicago
    Posts
    1,175

    Re: Easy Function to add a suffix to a number ie, 1st, 2nd, 3rd, etc..

    Also, I left the negatives out on purpose.. but the absolute value thing did pop into my mind.. I just couldn't say "negative 21st place" out loud without it seeming to me that I was violating Grammar.Spoken.English

    Bill
    Hate Adobe Acrobat? My Codebank Sumbissions - Easy CodeDom Expression evaluator: (VB / C# ) -- C# Scrolling Text Display

    I Like to code when drunk. Don't say you weren't warned.

  5. #5
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,683

    Re: Easy Function to add a suffix to a number ie, 1st, 2nd, 3rd, etc..

    I don't let petty trivialities like grammar get in the way of kickass code

    I'm just wondering if its possible to do it in less than 4 lines. The C# version is more up to date and is slightly shorter than this one.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width