How to generate an ordinal number
-
- Posts: 195
- Joined: Tuesday 26th September 2017 3:17pm
- Location: NW England
How to generate an ordinal number
Is there an available method to convert say "5" to "5th" or "23" to "23rd"?
-
- Posts: 195
- Joined: Tuesday 26th September 2017 3:17pm
- Location: NW England
Re: How to generate an ordinal number
Since I'm looking at day values of a date,
Select Case day_number MOD 10
Case 1
st
Case 2
nd
Case 3
rd
Case else
th
End Select
I guess this will do it, a bit messy though
Select Case day_number MOD 10
Case 1
st
Case 2
nd
Case 3
rd
Case else
th
End Select
I guess this will do it, a bit messy though
- BruceSteers
- Posts: 1569
- Joined: Thursday 23rd July 2020 5:20pm
- Location: Isle of Wight
- Contact:
Re: How to generate an ordinal number
No, 5 is Integer, 5th is String.
It's a simple function...
Public nth(v as integer, Textonly as Boolean)
Dim s as string =iif(textonly,"",str(v))
Dim n as Integer = v ٪ 10
Select n
Case 1
S &= "st"
Case 2
S &= "nd"
Case 3
S &= "rd"
Case else
S &= "th"
Return s
End
It's a simple function...
Public nth(v as integer, Textonly as Boolean)
Dim s as string =iif(textonly,"",str(v))
Dim n as Integer = v ٪ 10
Select n
Case 1
S &= "st"
Case 2
S &= "nd"
Case 3
S &= "rd"
Case else
S &= "th"
Return s
End
If at first you don't succeed , try doing something differently.
BruceS
BruceS
- BruceSteers
- Posts: 1569
- Joined: Thursday 23rd July 2020 5:20pm
- Location: Isle of Wight
- Contact:
Re: How to generate an ordinal number
Not so dirty...
Dim ss as string[] = ["th","st","nd","rd","th"]
Dim v as Integer = value % 10
Return Str(value) & ss[Min(v,4)]
Dim ss as string[] = ["th","st","nd","rd","th"]
Dim v as Integer = value % 10
Return Str(value) & ss[Min(v,4)]
If at first you don't succeed , try doing something differently.
BruceS
BruceS
- cogier
- Site Admin
- Posts: 1125
- Joined: Wednesday 21st September 2016 2:22pm
- Location: Guernsey, Channel Islands
Re: How to generate an ordinal number
Here is my version: -
Public Sub Form_Open()
Dim dDate As Date = Date(Now)
Dim sOrdinal As String = GetOrdinal(Format(dDate, "dd"))
Print Format(dDate, "dddd d") & sOrdinal & Format(dDate, " mmmm yyyy")
End
Public Sub GetOrdinal(sDay As String) As String
Dim iDay As Integer = Val(sDay)
Dim sOrdinal As String = "th"
Select Case iDay
Case 1, 21, 31
sOrdinal = "st"
Case 2, 22
sOrdinal = "nd"
Case 3, 23
sOrdinal = "rd"
End Select
Return sOrdinal
End
The result for today is Saturday 10th July 2021-
- Posts: 195
- Joined: Tuesday 26th September 2017 3:17pm
- Location: NW England
Re: How to generate an ordinal number
I like the not so dirty solution, thanks Bruce and Cogier.
- grayghost4
- Posts: 187
- Joined: Wednesday 5th December 2018 5:00am
- Location: Marengo, Illinois usa
Re: How to generate an ordinal number
I like Cogier's solution very well done
I can tell you have been reading " 'Clean Code' by Robert C. Martin "
I think everyone should read that book
I can tell you have been reading " 'Clean Code' by Robert C. Martin "
I think everyone should read that book
- cogier
- Site Admin
- Posts: 1125
- Joined: Wednesday 21st September 2016 2:22pm
- Location: Guernsey, Channel Islands
Re: How to generate an ordinal number
Thanks for the kind words, but I have never read that book!I like Cogier's solution very well done
I can tell you have been reading " 'Clean Code' by Robert C. Martin "
I think everyone should read that book
- grayghost4
- Posts: 187
- Joined: Wednesday 5th December 2018 5:00am
- Location: Marengo, Illinois usa
- BruceSteers
- Posts: 1569
- Joined: Thursday 23rd July 2020 5:20pm
- Location: Isle of Wight
- Contact:
Re: How to generate an ordinal number
I like shorter, as short as can be is best for me...
I got it to a one liner
Public Sub GetOrdinal(v as Integer) As String
Return(["th","st","nd","rd","th"][Min(4, v % 10)])
End
Not sure if defining a string[] ad-hoc like that works though (cant test as am at work)
I got it to a one liner
Public Sub GetOrdinal(v as Integer) As String
Return(["th","st","nd","rd","th"][Min(4, v % 10)])
End
Not sure if defining a string[] ad-hoc like that works though (cant test as am at work)
If at first you don't succeed , try doing something differently.
BruceS
BruceS