|
-
Aug 2nd, 2003, 04:37 AM
#1
Thread Starter
Lively Member
How to display the number abbreviations as 1st, 2nd, 3rd [Resolved]
I'm trying to add the abbreviations to the following numbers 1, 2 ,3 and 4 so that they come out like this 1st, 2nd, 3rd and 4th, ect.
I'm just using a simple Select Case statement and it works ok, but i was trying to make it more generic for example, how do you deal with any numbers so they display like this in a label or something.
923rd or 62nd or 101st or 333rd. ect, ect
It's not very practical to write out ALL the numbers in the Case statement EG: 1, 21, 31, 41...........991, 1001 ect, ect
Here's the code that i am using, it works ok and i'd proberly only ever want to go up to 200 anyway but that's not the point. It would be better to make it generic so that it displays all the numbers properly. I can't work out how to do it, can anyone help?
Code:
'Place the "st", "nd", "rd" and "th" indicators after the numbers
Dim nPostion As Short
Dim strPlace As String
nPostion = CInt(Me.txtPostions.Text)
Select Case nPostion
Case 1, 21, 31, 41, 51, 61, 71, 81, 91, 101
strPlace = "st"
Case 2, 22, 32, 42, 52, 62, 72, 82, 92, 102
strPlace = "nd"
Case 3, 23, 33, 43, 53, 63, 73, 83, 93, 103
strPlace = "rd"
Case Else
strPlace = "th"
End Select
'Place the result in the label
Me.lblPostions.Text = nPostion & strPlace
Last edited by Wallabie; Aug 3rd, 2003 at 05:13 AM.
-
Aug 2nd, 2003, 12:18 PM
#2
Does this do what you want?
VB Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Static x As Long
x += 1
Label1.Text = GetPlace(x)
End Sub
Private Function GetPlace(ByVal Number As Long) As String
Dim s As String
s = CType(Number, String)
If s.Length >= 2 Then
Select Case s.Substring(s.Length - 2, 2)
Case "11", "12", "13"
Return (Number & "th")
End Select
End If
Select Case (Number Mod 10)
Case 1 : s = Number & "st"
Case 2 : s = Number & "nd"
Case 3 : s = Number & "rd"
Case Else : s = Number & "th"
End Select
Return s
End Function
Laugh, and the world laughs with you. Cry, and you just water down your vodka.
Take credit, not responsibility
-
Aug 3rd, 2003, 05:08 AM
#3
Thread Starter
Lively Member
Hi crptcblade
It's not quite what i had in mind BUT! it does work a different way and something that will proberly be useful later on, so don't worry.
Below is the a very slightly different button event
Code:
Static x As Long
'NOTE!!!! JUST UNCOMMENT WHICH WAY YOU WANT TO DISPLAY IT!!!!
'To set the number inputed as a finishing number like in horse racing
'use x += 1 and then write it out to the list box like so:
x += 1
Me.lstPlacing.Items.Add(GetPlace(x) & vbTab & "No:" & Me.txtPlacing.Text)
'To set the number as either "st" or "nd" or "rd" or "th" use x = txtPlacing.Text
'and then write it to the label like so:
'x = Me.txtPlacing.Text
'Me.lblPlacing.Text = GetPlace(x)
Doing it the way you had it and placing it in a listbox (just for an example), it displays something like a race finish standings.
1st No:10
2nd No:4
3rd No:18 ect, ect
No problem thats ok
If i replace the += 1 and point it to the textbox to get the number it displays it in the label the way i wanted. If you typed in 82 it would show 82nd as in 82nd Airbourne for example. I've tried a few numbers and it seems to work very well either way!
Thank you very much for that. Killed two birds with one stone there!
NOTE:
No animals were hurt in the making of this post, a cat got sick....but THAT'S ALL!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|