Ordinal number function in PureBasic?

Just starting out? Need help? Post your questions and find answers here.
wysardry
User
User
Posts: 37
Joined: Sun May 27, 2012 4:58 pm

Ordinal number function in PureBasic?

Post 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.
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3870
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: Ordinal number function in PureBasic?

Post 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
Last edited by wilbert on Wed Aug 16, 2017 12:02 pm, edited 1 time in total.
Windows (x64)
Raspberry Pi OS (Arm64)
wysardry
User
User
Posts: 37
Joined: Sun May 27, 2012 4:58 pm

Re: Ordinal number function in PureBasic?

Post 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.
User avatar
Fig
Enthusiast
Enthusiast
Posts: 351
Joined: Thu Apr 30, 2009 5:23 pm
Location: Côtes d'Azur, France

Re: Ordinal number function in PureBasic?

Post 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)
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
wysardry
User
User
Posts: 37
Joined: Sun May 27, 2012 4:58 pm

Re: Ordinal number function in PureBasic?

Post 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.
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3870
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: Ordinal number function in PureBasic?

Post 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
Windows (x64)
Raspberry Pi OS (Arm64)
wysardry
User
User
Posts: 37
Joined: Sun May 27, 2012 4:58 pm

Re: Ordinal number function in PureBasic?

Post 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. :)
Little John
Addict
Addict
Posts: 4527
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: Ordinal number function in PureBasic?

Post 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)
User avatar
Fig
Enthusiast
Enthusiast
Posts: 351
Joined: Thu Apr 30, 2009 5:23 pm
Location: Côtes d'Azur, France

Re: Ordinal number function in PureBasic?

Post 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... :lol:

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
wysardry
User
User
Posts: 37
Joined: Sun May 27, 2012 4:58 pm

Re: Ordinal number function in PureBasic?

Post 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. :)
Post Reply