How to generate an ordinal number
Posted
#1
(In Topic #700)
Regular

Posted
Regular

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
Posted
Guru

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
Posted
Guru

Dim ss as string[] = ["th","st","nd","rd","th"]
Dim v as Integer = value % 10
Return Str(value) & ss[Min(v,4)]
Posted
Guru

Code (gambas)
- sOrdinal = "st"
- sOrdinal = "nd"
- sOrdinal = "rd"
- Return sOrdinal
The result for today is Saturday 10th July 2021
Posted
Regular

Posted
Enthusiast

I can tell you have been reading " 'Clean Code' by Robert C. Martin "
I think everyone should read that book
Posted
Guru

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!
Posted
Posted
Guru

I got it to a one liner <EMOJI seq="1f642" tseq="1f642">🙂</EMOJI>
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)
<EMOJI seq="1f609" tseq="1f609">😉</EMOJI>
Posted
Guru

The 11st <EMOJI seq="1f631" tseq="1f631">😱</EMOJI>
Dang and it was so perfect <EMOJI seq="1f923" tseq="1f923">🤣</EMOJI>,
easily fixed with an iif()
So a 2 liner..
Dim ss As String = ["th","st","nd","rd","th"]
Return(Iif(v % 100 = 11, "th", ss[Min(4, v % 10)]))
<EMOJI seq="1f60a" tseq="1f60a">😊</EMOJI>
Posted
Enthusiast

I appears that IIf is a carryover form visual basic. But seem to be the same as If in Gambas.
Posted
Guru

it exists in a few languages.
I'm rubbish with gambas <EMOJI seq="1f609" tseq="1f609">😉</EMOJI>
In the wiki iif is written before if so what's the chicken and what's the egg? , it's the same command whatever
/lang/iif - Gambas Documentation
<EMOJI seq="1f609" tseq="1f609">😉</EMOJI>
Posted
Guru

So my final input on this is this tested and working one liner that works on any number value..
Edit: (not my final input lol)
oops 12 and 13 too :-\
So not a one line after all…
Also note Cogiers version is twice as fast as mine
Posted
Enthusiast

Posted
Guru

I there anywhere that it is explained, uses and limitation ?
what is it called …. string look up ?
It's just an array created between the square brackets. Consider this code below, both Print lines do exactly the same thing:-
Posted
Enthusiast

I think I understand better now
Edit :
Another question comes to mind, is either method more efficient or faster, to assign the array to a variable or create it on the fly ?
Edit Again
I decided to test it my self and creating the array is about twice as fast and the other method
I guess that is because it is only creating the array once as opposed to creating it 4 times
Posted
Guru

It's amazing the way you can use the dot . To pass all sorts of things in one line of text.
If you need to re-use the variable a lot then it makes sense to create one.
The big speed increase in cogiers ordinal method was in the use of Select.
Fewer conditions to meet I think.
Posted
Enthusiast

could you expain :
It's amazing the way you can use the dot . To pass all sorts of things in one line of text.
Posted
Guru

grayghost4 said
Bruce :
could you expain :
It's amazing the way you can use the dot . To pass all sorts of things in one line of text.
Something that springs to.mind is using Image to stretch a Picture…
PictureBox1.Picture = PictureBox1.Picture.Image.Stretch(PictureBox1.Picture.Width / times, PictureBox1.Picture.Height / times).Picture
That could be done using an Image assignment variable pointer but theres no need.
I've bunched all sorts of things up into one liners <EMOJI seq="1f642" tseq="1f642">🙂</EMOJI>
1 guest and 0 members have just viewed this.


