Ordinal Numbers

topic: 

Ordinal numbers refer to a position in a series and usually are presented as a number and one of the suffixes -th, -st, -nd and -rd.


For example: 1st, 2nd, 3rd, 4th, 20th, 23rd, 52nd, 112th, 163rd, etc.

This is sample code. Add error handling and adjust to your requirements as necessary.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19

* Convert a number into Ordinal Number string
FUNCTION OrdinalNumber
LPARAMETER tiNumber 
LOCAL lcSuffix
* Only whole numbers are allowed
IF (tiNumber % 1) <> 0
	ERROR 11
ENDIF	

IF  BETWEEN(tiNumber % 10, 1,3) AND NOT BETWEEN(tiNumber % 100, 11,13)
	lcSuffix = SUBSTR("stndrd", ((tiNumber % 10) * 2) -1, 2)
ELSE	
	lcSuffix = 'th'
ENDIF
RETURN TRANSFORM(tiNumber) + lcSuffix


Examples
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12

CLEAR
FOR lnNumber = 1 TO 24
	? OrdinalNumber(lnNumber)
ENDFOR
?

FOR lnNumber = 110 TO 124
	?  OrdinalNumber(lnNumber)
ENDFOR

Comments