How to generate an ordinal number

Post your Gambas programming questions here.
bill-lancaster
Posts: 135
Joined: Tuesday 26th September 2017 3:17pm
Location: NW England

How to generate an ordinal number

Post by bill-lancaster » Saturday 10th July 2021 9:25am

Is there an available method to convert say "5" to "5th" or "23" to "23rd"?

bill-lancaster
Posts: 135
Joined: Tuesday 26th September 2017 3:17pm
Location: NW England

Re: How to generate an ordinal number

Post by bill-lancaster » Saturday 10th July 2021 11:15am

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

User avatar
BruceSteers
Posts: 652
Joined: Thursday 23rd July 2020 5:20pm

Re: How to generate an ordinal number

Post by BruceSteers » Saturday 10th July 2021 11:18am

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
Wishing well
Bruce

If at first you don't succeed , try it differently.

User avatar
BruceSteers
Posts: 652
Joined: Thursday 23rd July 2020 5:20pm

Re: How to generate an ordinal number

Post by BruceSteers » Saturday 10th July 2021 11:24am

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)]
Wishing well
Bruce

If at first you don't succeed , try it differently.

User avatar
cogier
Site Admin
Posts: 756
Joined: Wednesday 21st September 2016 2:22pm
Location: Guernsey, Channel Islands

Re: How to generate an ordinal number

Post by cogier » Saturday 10th July 2021 11:51am

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

bill-lancaster
Posts: 135
Joined: Tuesday 26th September 2017 3:17pm
Location: NW England

Re: How to generate an ordinal number

Post by bill-lancaster » Saturday 10th July 2021 11:58am

I like the not so dirty solution, thanks Bruce and Cogier.

User avatar
grayghost4
Posts: 119
Joined: Wednesday 05th December 2018 5:00am
Location: Marengo, illinois usa

Re: How to generate an ordinal number

Post by grayghost4 » Saturday 10th July 2021 12:45pm

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

User avatar
cogier
Site Admin
Posts: 756
Joined: Wednesday 21st September 2016 2:22pm
Location: Guernsey, Channel Islands

Re: How to generate an ordinal number

Post by cogier » Saturday 10th July 2021 1:00pm

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
Thanks for the kind words, but I have never read that book!

User avatar
grayghost4
Posts: 119
Joined: Wednesday 05th December 2018 5:00am
Location: Marengo, illinois usa

Re: How to generate an ordinal number

Post by grayghost4 » Saturday 10th July 2021 1:05pm


User avatar
BruceSteers
Posts: 652
Joined: Thursday 23rd July 2020 5:20pm

Re: How to generate an ordinal number

Post by BruceSteers » Saturday 10th July 2021 2:16pm

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)
😉
Wishing well
Bruce

If at first you don't succeed , try it differently.

Post Reply