Ordinal number function in PureBasic?
Ordinal number function in PureBasic?
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.
This isn't for dates, so would need to work for numbers over 31 too.
Re: Ordinal number function in PureBasic?
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
With a little help from the internet ...
Edit:
code removed; code was broken
Last edited by wilbert on Wed Aug 16, 2017 12:02 pm, edited 1 time in total.
Windows (x64)
Raspberry Pi OS (Arm64)
Raspberry Pi OS (Arm64)
Re: Ordinal number function in PureBasic?
That seems to work up to 110th, but then outputs:-
It does the same from 211 onwards too.
I'm afraid that I don't know PureBasic well enough (yet) to fix it.
Code: Select all
111st
112nd
113rd
114th
115th
116th
117th
118th
119th
120th
I'm afraid that I don't know PureBasic well enough (yet) to fix it.
Re: Ordinal number function in PureBasic?
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)
Last edited by Fig on Wed Aug 16, 2017 12:33 pm, edited 1 time in total.
There are 2 methods to program bugless.
But only the third works fine.
Win10, Pb x64 5.71 LTS
But only the third works fine.
Win10, Pb x64 5.71 LTS
Re: Ordinal number function in PureBasic?
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?
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
Windows (x64)
Raspberry Pi OS (Arm64)
Raspberry Pi OS (Arm64)
Re: Ordinal number function in PureBasic?
The output is now:-
So it looks like that did the trick. Thanks.
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
-
- Addict
- Posts: 4527
- Joined: Thu Jun 07, 2007 3:25 pm
- Location: Berlin, Germany
Re: Ordinal number function in PureBasic?
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?
It's my english grammar whom (which, who ?...) is broken, that's why...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.
I corrected the listing.
Last edited by Fig on Wed Aug 16, 2017 12:33 pm, edited 2 times in total.
There are 2 methods to program bugless.
But only the third works fine.
Win10, Pb x64 5.71 LTS
But only the third works fine.
Win10, Pb x64 5.71 LTS
Re: Ordinal number function in PureBasic?
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.
I tested both of the latest versions on numbers over 1000 too, and they still work as expected.