Posted By

DaveChild on 09/11/08


Tagged

ordinal


Versions (?)

VB.NET Add Ordinal


 / Published in: VB.NET
 

  1. Public Function ordinal(ByVal intNumber As Integer) As String
  2. If CType(intNumber, String).Length > 2 Then
  3. Dim intEndNum As Integer = CType(CType(intNumber, String).Substring(CType(intNumber, String).Length - 2, 2), Integer)
  4. If intEndNum >= 11 And intEndNum <= 13 Then
  5. Select Case intEndNum
  6. Case 11, 12, 13
  7. Return "th"
  8. End Select
  9. End If
  10. End If
  11. If intNumber >= 21 Then
  12. ' Handles 21st, 22nd, 23rd, et al
  13. Select Case CType(intNumber.ToString.Substring(intNumber.ToString.Length - 1, 1), Integer)
  14. Case 1
  15. Return "st"
  16. Case 2
  17. Return "nd"
  18. Case 3
  19. Return "rd"
  20. Case 0, 4 To 9
  21. Return "th"
  22. End Select
  23. Else
  24. ' Handles 1st to 20th
  25. Select Case intNumber
  26. Case 1
  27. Return "st"
  28. Case 2
  29. Return "nd"
  30. Case 3
  31. Return "rd"
  32. Case 4 To 20
  33. Return "th"
  34. End Select
  35. End If
  36. ' If here, no match was found. Should be impossible. In fact, if we're here the day of the month is a non-number. Best not return an ordinal at all.
  37. Return ""
  38. End Function

Report this snippet  

You need to login to post a comment.