Add Ordinal Extension to a Number

This little code snippet adds extensions such as st, nd, rd, th to integer numbers. Useful also with SSRS under Report Properties > Code

 Public Function ChangeToOrdinal(ByVal digit As Integer) As String
        Dim residual As Integer = digit Mod 10
        Dim extension As String
        If digit > 20 Or digit < 10 Then
            Select Case residual
                Case Is = 1
                    extension = "st"
                Case Is = 2
                    extension = "nd"
                Case Is = 3
                    extension = "rd"
                Case Else
                    extension = "th"
            End Select
            extension = "th"
        End If
        Return CStr(digit) & extension
    End Function

Related Posts

Comments are closed.