by Alex Nunn (1 Submission)
Category: String Manipulation
Compatability: Visual Basic 3.0
Difficulty: Intermediate
Date Added: Wed 3rd February 2021
Rating: (3 Votes)
This function adds st, nd, rd, or th to the end of a string of numbers based on what the number is. For example, the following code can produce the following output, "Thursday, November 23rd, 2000" : Format(Date, "dddd, mmmm ") & FormatFancyNumber(Day(Date)) & ", " & Year(Date)
Inputs
All this function needs is an integer number in string format.
Code Returns
It returns the number in string format with st, nd, rd, or th added to the end as needed.
Side Effects
The code currently only works with integer size numbers. This should be easy to change though considering the size of code.
Public Function FormatFancyNumber(ByVal sNumber As String) As String
Dim iTemp As Integer
iTemp = Int(sNumber)
If 4 < iTemp And iTemp < 20 Then
FormatFancyNumber = sNumber & "th"
Else
Select Case iTemp Mod 10
Case 1
FormatFancyNumber = sNumber & "st"
Case 2
FormatFancyNumber = sNumber & "nd"
Case 3
FormatFancyNumber = sNumber & "rd"
Case Else
FormatFancyNumber = sNumber & "th"
End Select
End If
End Function