Oops I've messed up with one problem ...
The 11st
Dang and it was so perfect ,
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)]))
How to generate an ordinal number
- BruceSteers
- Posts: 1563
- Joined: Thursday 23rd July 2020 5:20pm
- Location: Isle of Wight
- Contact:
Re: How to generate an ordinal number
If at first you don't succeed , try doing something differently.
BruceS
BruceS
- grayghost4
- Posts: 187
- Joined: Wednesday 5th December 2018 5:00am
- Location: Marengo, Illinois usa
Re: How to generate an ordinal number
It appears that " IIf " is the same as " If ", is that correct ?
I appears that IIf is a carryover form visual basic. But seem to be the same as If in Gambas.
I appears that IIf is a carryover form visual basic. But seem to be the same as If in Gambas.
- BruceSteers
- Posts: 1563
- Joined: Thursday 23rd July 2020 5:20pm
- Location: Isle of Wight
- Contact:
Re: How to generate an ordinal number
No idea, I've always used iif.
it exists in a few languages.
I'm rubbish with gambas
In the wiki iif is written before if so what's the chicken and what's the egg? , it's the same command whatever
http://gambaswiki.org/wiki/lang/iif
it exists in a few languages.
I'm rubbish with gambas
In the wiki iif is written before if so what's the chicken and what's the egg? , it's the same command whatever
http://gambaswiki.org/wiki/lang/iif
Last edited by BruceSteers on Saturday 10th July 2021 9:27pm, edited 1 time in total.
If at first you don't succeed , try doing something differently.
BruceS
BruceS
- BruceSteers
- Posts: 1563
- Joined: Thursday 23rd July 2020 5:20pm
- Location: Isle of Wight
- Contact:
Re: How to generate an ordinal number
I got home from work and was able to test and the ad-hoc string array does work
So my final input on this is this tested and working one liner that works on any number value..
oops 12 and 13 too :-\
So not a one line after all...
So my final input on this is this tested and working one liner that works on any number value..
Public Sub OrdinalString(Value As Integer) As String
Return Str(Value) & ["th", "st", "nd", "rd", "th"][If(Value % 100 = 11, 0, Min(4, Value % 10))]
End
Edit: (not my final input lol)oops 12 and 13 too :-\
So not a one line after all...
Public Sub nth(Value As Integer) As String
Select Value % 100
Case 11, 12, 13
Return Str(Value) & "th"
End Select
Return Str(Value) & ["th", "st", "nd", "rd", "th"][Min(4, Value % 10)]
End
Also note Cogiers version is twice as fast as mine If at first you don't succeed , try doing something differently.
BruceS
BruceS
- grayghost4
- Posts: 187
- Joined: Wednesday 5th December 2018 5:00am
- Location: Marengo, Illinois usa
Re: How to generate an ordinal number
Return Str(Value) & ["th", "st", "nd", "rd", "th"][Min(4, Value % 10)]
I am still learning Gambas ... I have not seen this concept till this thread.I there anywhere that it is explained, uses and limitation ?
what is it called .... string look up ?
- cogier
- Site Admin
- Posts: 1125
- Joined: Wednesday 21st September 2016 2:22pm
- Location: Guernsey, Channel Islands
Re: How to generate an ordinal number
It's just an array created between the square brackets. Consider this code below, both Print lines do exactly the same thing:-I there anywhere that it is explained, uses and limitation ?
what is it called .... string look up ?
Public Sub Form_Open()
Dim iLoop As Integer
Dim sText As String[] = ["th", "st", "nd", "rd", "th"]
For iLoop = 0 To 4
Print sText[iLoop]
Print ["th", "st", "nd", "rd", "th"][iLoop]
Next
End
- grayghost4
- Posts: 187
- Joined: Wednesday 5th December 2018 5:00am
- Location: Marengo, Illinois usa
Re: How to generate an ordinal number
Thank You ... That is a good explanation so it is just indexing an array
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
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
- BruceSteers
- Posts: 1563
- Joined: Thursday 23rd July 2020 5:20pm
- Location: Isle of Wight
- Contact:
Re: How to generate an ordinal number
I have a habit of ditching as many single use variables as I can.
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.
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.
If at first you don't succeed , try doing something differently.
BruceS
BruceS
- grayghost4
- Posts: 187
- Joined: Wednesday 5th December 2018 5:00am
- Location: Marengo, Illinois usa
Re: How to generate an ordinal number
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.
could you expain :
It's amazing the way you can use the dot . To pass all sorts of things in one line of text.
- BruceSteers
- Posts: 1563
- Joined: Thursday 23rd July 2020 5:20pm
- Location: Isle of Wight
- Contact:
Re: How to generate an ordinal number
Something that springs to.mind is using Image to stretch a Picture...grayghost4 wrote: โTuesday 13th July 2021 9:21pm 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.
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
If at first you don't succeed , try doing something differently.
BruceS
BruceS