# Android Code SnippetOrdinal 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
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
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:

Android Question Conversion in a clock format
Replies
2
Views
180
Replies
14
Views
1K
Android Question Doubt on Number Format?
Replies
36
Views
1K
Android Question X MOD Y
Replies
13
Views
2K
Replies
196
Views
88K