Android Code Snippet Ordinal Formating

I recently need to format numbers as 1st, 2nd, ... 121st, ...

So I wrote these routines. Figured someone else may need them

Call Ordinal passing number and it will returns its Suffix and just the Suffix
Call FullOrdinal passing number it will return the number with the suffix

B4X:
Public  Sub Ordinal(Number As Int) As String
               
           Dim Mod100 As Int = Number Mod 100
           Dim Mod10  As Int = Number Mod 10
           
           If  Mod10 = 1 And Mod100 <> 11 Then
               Return "st"           
           End If
           
           If  Mod10 = 2 And Mod100 <> 12 Then
               Return "nd"
           End If
           
           If  Mod10 = 3 And Mod100 <> 13 Then
               Return "rd"
           End If
           
           Return "th"
End Sub

Public  Sub FullOrdinal(Number As Int) As String
               
           Dim Mod100 As Int = Number Mod 100
           Dim Mod10  As Int = Number Mod 10
           
           If  Mod10 = 1 And Mod100 <> 11 Then
               Return Number &"st"           
           End If
           
           If  Mod10 = 2 And Mod100 <> 12 Then
               Return Number &"nd"
           End If
           
           If  Mod10 = 3 And Mod100 <> 13 Then
               Return Number &"rd"
           End If
           
           Return Number &"th"
End Sub

BobVal
 

Star-Dust

Expert
Licensed User
try this
B4X:
Public  Sub Ordinal(Number As Int) As String
    Dim Mod100 As Int = Number Mod 100
    If Mod100<11 Or Mod100>13 Then
        Dim Mod10  As Int = (Number Mod 10)-1
        If Mod10>=0 And Mod10<=2 Then Return "stndrd".SubString2(Mod10*2,Mod10*2+2)
    End If
 
    Return "th"
End Sub

Public  Sub FullOrdinal(Number As Int) As String
    Dim Mod100 As Int = Number Mod 100
    If Mod100<11 Or Mod100>13 Then
        Dim Mod10  As Int = (Number Mod 10)-1
        If Mod10>=0 And Mod10<=2 Then Return Number & "stndrd".SubString2(Mod10*2,Mod10*2+2)
    End If

    Return Number & "th"
End Sub
 
Last edited:

Star-Dust

Expert
Licensed User
Try also this:
B4X:
Public  Sub Ordinal(Number As Int) As String
    Dim Mod10 As Int = 0
    Dim Mod100 As Int = Number Mod 100
    If Mod100<11 Or Mod100>13 Then Mod10 = Min((Number Mod 10),4)

    Return "thstndrdth".SubString2(Mod10*2,Mod10*2+2)
End Sub

Public  Sub FullOrdinal(Number As Int) As String
    Dim Mod10 As Int = 0
    Dim Mod100 As Int = Number Mod 100
    If Mod100<11 Or Mod100>13 Then Mod10 = Min((Number Mod 10),4)

    Return Number & "thstndrdth".SubString2(Mod10*2,Mod10*2+2)
End Sub
 
Last edited:
Top