Page 1 of 1
Ordinal number function in PureBasic?
Posted: Wed Aug 16, 2017 9:51 am
by wysardry
Is there a built-in function to add a suffix to a number to create an ordinal number? By that, I mean, is there a way to change 1, 2, 3, 4 etc. into 1st, 2nd, 3rd, 4th etc.?
This isn't for dates, so would need to work for numbers over 31 too.
Re: Ordinal number function in PureBasic?
Posted: Wed Aug 16, 2017 10:43 am
by wilbert
No, there isn't but its easy to create your own procedure.
With a little help from the internet ...
Edit:
code removed; code was broken
Re: Ordinal number function in PureBasic?
Posted: Wed Aug 16, 2017 11:31 am
by wysardry
That seems to work up to 110th, but then outputs:-
Code: Select all
111st
112nd
113rd
114th
115th
116th
117th
118th
119th
120th
It does the same from 211 onwards too.
I'm afraid that I don't know PureBasic well enough (yet) to fix it.
Re: Ordinal number function in PureBasic?
Posted: Wed Aug 16, 2017 11:37 am
by Fig
Code: Select all
Procedure.s OrdinalNumber(n.i)
Protected.s n1, n2, result
n1 = Right(Str(n),1)
n2 = Right(Str(n),2)
result="th"
Select n1
Case "1"
If n2<>"11"
result="st"
EndIf
Case "2"
If n2<>"12"
result="nd"
EndIf
Case "3"
If n2<>"13"
result="rd"
EndIf
EndSelect
ProcedureReturn Str(n)+result
EndProcedure
Debug ordinalnumber(113)
Re: Ordinal number function in PureBasic?
Posted: Wed Aug 16, 2017 11:45 am
by wysardry
I'm afraid that one is broken at 11, 12, 13, 111, 112, 113 etc. as it prints 11st, 12nd, 13rd, 111st, 112nd, 113rd etc.
Re: Ordinal number function in PureBasic?
Posted: Wed Aug 16, 2017 11:54 am
by wilbert
Can you try this modified version instead
Code: Select all
Procedure.s OrdinalNumber(n.i)
Protected.i n1, n10
n1=n%100 : n10=n1/10 : n1-n10*10
If n1=0 Or n1 >= 4 Or n10=1
ProcedureReturn Str(n)+"th"
ElseIf n1 = 1
ProcedureReturn Str(n)+"st"
ElseIf n1 = 2
ProcedureReturn Str(n)+"nd"
Else
ProcedureReturn Str(n)+"rd"
EndIf
EndProcedure
Re: Ordinal number function in PureBasic?
Posted: Wed Aug 16, 2017 12:02 pm
by wysardry
The output is now:-
Code: Select all
1st
2nd
3rd
4th
5th
6th
7th
8th
9th
10th
11th
12th
13th
14th
15th
16th
17th
18th
19th
20th
21st
22nd
23rd
24th
...
99th
100th
101st
102nd
103rd
104th
105th
106th
107th
108th
109th
110th
111th
112th
113th
114th
...
210th
211th
212th
213th
214th
215th
216th
217th
218th
219th
220th
221st
222nd
223rd
224th
225th
226th
227th
228th
229th
230th
So it looks like that did the trick. Thanks.

Re: Ordinal number function in PureBasic?
Posted: Wed Aug 16, 2017 12:10 pm
by Little John
Slightly different approach (should output the same result as wilbert's last version):
Code: Select all
EnableExplicit
Procedure.s StrO (n.q)
; return an ordinal number (English)
Protected ret$, suffix$
ret$ = Str(n)
suffix$ = "th"
Select n % 10
Case 1
If n % 100 <> 11
suffix$ = "st"
EndIf
Case 2
If n % 100 <> 12
suffix$ = "nd"
EndIf
Case 3
If n % 100 <> 13
suffix$ = "rd"
EndIf
EndSelect
ProcedureReturn ret$ + suffix$
EndProcedure
; -- Demo
Debug StrO(1)
Debug StrO(2)
Debug StrO(3)
Debug StrO(4)
Debug ""
Debug StrO(11)
Debug StrO(12)
Debug StrO(13)
Debug StrO(14)
Debug ""
Debug StrO(21)
Debug StrO(22)
Debug StrO(23)
Debug StrO(24)
Debug "-----"
Debug StrO(701)
Debug StrO(702)
Debug StrO(703)
Debug StrO(704)
Debug ""
Debug StrO(711)
Debug StrO(712)
Debug StrO(713)
Debug StrO(714)
Debug ""
Debug StrO(721)
Debug StrO(722)
Debug StrO(723)
Debug StrO(724)
Re: Ordinal number function in PureBasic?
Posted: Wed Aug 16, 2017 12:27 pm
by Fig
wysardry wrote:I'm afraid that one is broken at 11, 12, 13, 111, 112, 113 etc. as it prints 11st, 12nd, 13rd, 111st, 112nd, 113rd etc.
It's my english grammar whom (which, who ?...) is broken, that's why...
I corrected the listing.
Re: Ordinal number function in PureBasic?
Posted: Wed Aug 16, 2017 12:29 pm
by wysardry
Thanks, Little John. That also worked and is slightly easier to understand.
I tested both of the latest versions on numbers over 1000 too, and they still work as expected.
