N'th: Difference between revisions
(PascalABC.NET) |
|||
(175 intermediate revisions by 69 users not shown) | |||
Line 1: | Line 1: | ||
{{task|Ordinal numbers}} |
{{task|Ordinal numbers}} |
||
[[Category:String manipulation]] |
|||
Write a function/method/subroutine/... that when given an integer greater than or equal to zero returns a string of the number followed by an apostrophe then the [[wp:Ordinal number (linguistics)|ordinal suffix]]. |
Write a function/method/subroutine/... that when given an integer greater than or equal to zero returns a string of the number followed by an apostrophe then the [[wp:Ordinal number (linguistics)|ordinal suffix]]. |
||
<br>Example returns would include <code>1'st 2'nd 3'rd 11'th 111'th 1001'st 1012'th</code> |
|||
;Example: |
|||
Returns would include <code>1'st 2'nd 3'rd 11'th 111'th 1001'st 1012'th</code> |
|||
;Task: |
;Task: |
||
Use your routine to show here the output for ''at least'' the following (inclusive) ranges of integer inputs: |
Use your routine to show here the output for ''at least'' the following (inclusive) ranges of integer inputs: |
||
<code>0..25, 250..265, 1000..1025</code> |
<code>0..25, 250..265, 1000..1025</code> |
||
'''Note:''' apostrophes are now ''optional'' to allow correct apostrophe-less English. |
'''Note:''' apostrophes are now ''optional'' to allow correct apostrophe-less English. |
||
<br><br> |
|||
=={{header|11l}}== |
|||
{{trans|Python}} |
|||
<syntaxhighlight lang="11l">V _suffix = [‘th’, ‘st’, ‘nd’, ‘rd’, ‘th’, ‘th’, ‘th’, ‘th’, ‘th’, ‘th’] |
|||
F nth(n) |
|||
R ‘#.'#.’.format(n, I n % 100 <= 10 | n % 100 > 20 {:_suffix[n % 10]} E ‘th’) |
|||
L(j) (0..1000).step(250) |
|||
print_elements(Array(j.+25).map(i -> nth(i)))</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
0'th 1'st 2'nd 3'rd 4'th 5'th 6'th 7'th 8'th 9'th 10'th 11'th 12'th 13'th 14'th 15'th 16'th 17'th 18'th 19'th 20'th 21'st 22'nd 23'rd 24'th |
|||
250'th 251'st 252'nd 253'rd 254'th 255'th 256'th 257'th 258'th 259'th 260'th 261'st 262'nd 263'rd 264'th 265'th 266'th 267'th 268'th 269'th 270'th 271'st 272'nd 273'rd 274'th |
|||
500'th 501'st 502'nd 503'rd 504'th 505'th 506'th 507'th 508'th 509'th 510'th 511'th 512'th 513'th 514'th 515'th 516'th 517'th 518'th 519'th 520'th 521'st 522'nd 523'rd 524'th |
|||
750'th 751'st 752'nd 753'rd 754'th 755'th 756'th 757'th 758'th 759'th 760'th 761'st 762'nd 763'rd 764'th 765'th 766'th 767'th 768'th 769'th 770'th 771'st 772'nd 773'rd 774'th |
|||
1000'th 1001'st 1002'nd 1003'rd 1004'th 1005'th 1006'th 1007'th 1008'th 1009'th 1010'th 1011'th 1012'th 1013'th 1014'th 1015'th 1016'th 1017'th 1018'th 1019'th 1020'th 1021'st 1022'nd 1023'rd 1024'th |
|||
</pre> |
|||
=={{header|68000 Assembly}}== |
|||
The function itself: |
|||
<syntaxhighlight lang="68000devpac">Nth: |
|||
MOVEQ #1,D1 |
|||
.loop: |
|||
MOVE.L D1,-(SP) |
|||
MOVE.L D0,-(SP) |
|||
MOVE.B (2,SP),D0 |
|||
JSR printhex |
|||
MOVE.B (3,SP),D0 |
|||
JSR printhex |
|||
MOVE.L (SP)+,D0 |
|||
MOVE.L D0,-(SP) |
|||
AND.W #$00FF,D0 |
|||
;HANDLE SPECIAL CASES |
|||
CMP.B #$11,D0 |
|||
BEQ .fourth |
|||
CMP.B #$12,D0 |
|||
BEQ .fourth |
|||
CMP.B #$13,D0 |
|||
BEQ .fourth |
|||
;HANDLE THE REST |
|||
AND.W #$000F,D0 |
|||
CMP.B #1,D0 |
|||
BEQ .first |
|||
CMP.B #2,D0 |
|||
BEQ .second |
|||
CMP.B #3,D0 |
|||
BEQ .third |
|||
.fourth: |
|||
LEA th,A3 |
|||
bra .print |
|||
.third: |
|||
LEA rd,a3 |
|||
bra .print |
|||
.second: |
|||
LEA nd,a3 |
|||
bra .print |
|||
.first: |
|||
LEA est,a3 |
|||
.print: |
|||
JSR PrintString |
|||
JSR newline |
|||
MOVE.L (SP)+,d0 |
|||
MOVE.L (SP)+,d1 |
|||
ABCD D1,D0 |
|||
DBRA D7,.loop |
|||
rts |
|||
th: |
|||
dc.b "th",255 |
|||
even |
|||
est: |
|||
dc.b "st",255 |
|||
even |
|||
nd: |
|||
dc.b "nd",255 |
|||
even |
|||
rd: |
|||
dc.b "rd",255 |
|||
even</syntaxhighlight> |
|||
And the test cases (each was executed in separate builds of the Sega Genesis cartridge to have enough room to see them all at once) |
|||
<syntaxhighlight lang="68000devpac">MOVE.w #0,D0 |
|||
MOVE.W #25,D7 |
|||
JSR Nth |
|||
MOVE.w #$250,D0 ;since we're working with binary-coded decimal, this number is stored as hex. |
|||
MOVE.W #15,D7 |
|||
JSR Nth |
|||
MOVE.w #$1000,D0 ;since we're working with binary-coded decimal, this number is stored as hex. |
|||
MOVE.W #25,D7 |
|||
JSR Nth |
|||
jmp * ;stop the cpu - we're done.</syntaxhighlight> |
|||
{{out}} |
|||
<pre>0th 1st 2nd 3rd 4th 5th 6th 7th 8th 9th 10th 11th 12th 13th 14th 15th 16th 17th 18th 19th 20th 21st 22nd 23rd 24th 25th |
|||
250th 251st 252nd 253rd 254th 255th 256th 257th 258th 259th 260th 261st 262nd 263rd 264th 265th |
|||
1000th 1001st 1002nd 1003rd 1004th 1005th 1006th 1007th 1008th 1009th 1010th 1011th 1012th 1013th 1014th 1015th 1016th 1017th 1018th 1019th 1020th 1021st 1022nd 1023rd 1024th 1025th</pre> |
|||
=={{header|8080 Assembly}}== |
|||
<syntaxhighlight lang="asm"> org 100h |
|||
jmp demo |
|||
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
|||
;; Given a 16-bit unsigned integer in HL, return a string |
|||
;; consisting of its ASCII representation plus an ordinal |
|||
;; suffix in HL. |
|||
nth: lxi b,-10 ; Divisor for digit loop |
|||
push h ; Push integer to stack |
|||
lxi h,nthsfx ; Pointer to end of digit area |
|||
xthl ; Swap digit pointer with integer |
|||
nthdigit: lxi d,-1 ; Get current digit - DE holds quotient |
|||
nthdiv10: inx d ; Increment quotient, |
|||
dad b ; subtract 10 from integer, |
|||
jc nthdiv10 ; Keep going until HL<0 |
|||
mvi a,'0'+10 ; Calculate ASCII value for digit |
|||
add l ; Modulus is in (H)L |
|||
xthl ; Swap integer with pointer |
|||
dcx h ; Point to one digit further left |
|||
mov m,a ; Store the digit |
|||
xthl ; Swap pointer with integer |
|||
xchg ; Put quotient in HL |
|||
mov a,h ; Is it zero? |
|||
ora l |
|||
jnz nthdigit ; If not, go calculate next digit |
|||
mvi e,0 ; Default suffix is 'th'. |
|||
lxi h,nthnum+3 ; Look at tens digit |
|||
mov a,m |
|||
cpi '1' ; Is it '1'? |
|||
jz nthsetsfx ; Then it is always 'th'. |
|||
inx h ; Look at zeroes digit |
|||
mov a,m |
|||
sui '0' ; Subtract ASCII '0' |
|||
cpi 4 ; 4 or higher? |
|||
jnc nthsetsfx ; Then it is always 'th'. |
|||
rlc ; Otherwise, suffix is at N*2+nthord |
|||
mov e,a |
|||
nthsetsfx: mvi d,0 ; Look up suffix in list |
|||
lxi h,nthord |
|||
dad d ; Pointer to suffix in HL |
|||
lxi d,nthsfx ; Pointer to space for suffix in DE |
|||
mov a,m ; Get first letter of suffix |
|||
stax d ; Store it in output |
|||
inx h ; Increment both pointers |
|||
inx d |
|||
mov a,m ; Get second letter of suffix |
|||
stax d ; Store it in output |
|||
pop h ; Return pointer to leftmost digit |
|||
ret |
|||
nthord: db 'thstndrd' ; Ordinal suffixes |
|||
nthnum: db '*****' ; Room for digits |
|||
nthsfx: db '**$' ; Room for suffix |
|||
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
|||
demo: ;; Demo code |
|||
lxi h,0 ; Starting at 0... |
|||
mvi b,26 ; ...print 26 numbers [0..25] |
|||
call printnums |
|||
lxi h,250 ; Starting at 250... |
|||
mvi b,16 ; ...print 16 numbers [250..265] |
|||
call printnums |
|||
lxi h,1000 ; Starting at 1000... |
|||
mvi b,26 ; ...print 26 numbers [1000..1025] |
|||
;; Print values from HL up to HL+B |
|||
printnums: push h ; Keep number |
|||
push b ; Keep counter |
|||
call nth ; Get string for current HL |
|||
xchg ; Put it in DE |
|||
mvi c,9 ; CP/M print string |
|||
call 5 |
|||
mvi e,' ' ; Separate numbers with spaces |
|||
mvi c,2 ; CP/M print character |
|||
call 5 |
|||
pop b ; Restore counter |
|||
pop h ; Restore number |
|||
inx h ; Increment number |
|||
dcr b ; Decrement counter |
|||
jnz printnums ; If not zero, print next number |
|||
ret</syntaxhighlight> |
|||
{{out}} |
|||
Line breaks added for clarity. |
|||
<pre>0th 1st 2nd 3rd 4th 5th 6th 7th 8th 9th 10th 11th 12th 13th 14th 15th |
|||
16th 17th 18th 19th 20th 21st 22nd 23rd 24th 25th 250th 251st 252nd |
|||
253rd 254th 255th 256th 257th 258th 259th 260th 261st 262nd 263rd 264th |
|||
265th 1000th 1001st 1002nd 1003rd 1004th 1005th 1006th 1007th 1008th |
|||
1009th 1010th 1011th 1012th 1013th 1014th 1015th 1016th 1017th 1018th |
|||
1019th 1020th 1021st 1022nd 1023rd 1024th 1025th</pre> |
|||
=={{header|8086 Assembly}}== |
|||
{{trans|8080 Assembly}} |
|||
<syntaxhighlight lang="asm"> bits 16 |
|||
cpu 8086 |
|||
segment .text |
|||
org 100h |
|||
jmp demo |
|||
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
|||
;; Given a number in AX, return string with ordinal suffix |
|||
;; in DX. |
|||
nth: mov cx,10 ; Divisor |
|||
mov bx,.sfx ; Pointer to end of number |
|||
.digit: xor dx,dx ; Zero DX |
|||
div cx ; AX = DX:AX/10; DX=remainder |
|||
add dl,'0' ; Make digit |
|||
dec bx ; Back up the pointer |
|||
mov [bx],dl ; Store digit |
|||
and ax,ax ; Done yet? (AX=0?) |
|||
jnz .digit ; If not, get next digit |
|||
mov dx,bx ; Keep string pointer in DX |
|||
xor bx,bx ; Default suffix is 'th'. |
|||
cmp byte [.num+3],'1' ; Is the tens digit '1'? |
|||
je .setsfx ; Then the suffix is 'th'. |
|||
mov cl,[.num+4] ; Get the ones digit |
|||
cmp cl,'4' ; Is it '4' or higher? |
|||
jae .setsfx ; Then the suffix is 'th'. |
|||
mov bl,cl |
|||
sub bl,'0' ; Calculate offset. |
|||
shl bl,1 ; [0..3]*2 + .ord |
|||
.setsfx: mov ax,[bx+.ordsfx] ; Set suffix |
|||
mov [.sfx],ax |
|||
ret |
|||
.ordsfx: db 'thstndrd' |
|||
.num: db '*****' |
|||
.sfx: db '**$' |
|||
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
|||
;; Print numbers. |
|||
demo: mov ax,0 ; Starting at 0... |
|||
mov bl,26 ; ...print 26 numbers [0..25] |
|||
call printn |
|||
mov ax,250 ; Starting at 250... |
|||
mov bl,16 ; ...print 16 numbers [250..265] |
|||
call printn |
|||
mov ax,1000 ; Starting at 1000... |
|||
mov bl,26 ; ...print 26 numbers [1000..1025] |
|||
;; Print BL numbers starting at AX |
|||
printn: push ax ; Keep number |
|||
push bx ; Keep counter |
|||
call nth ; Get string for current AX |
|||
mov ah,9 ; MS-DOS print string |
|||
int 21h |
|||
mov dl,' ' ; Separate numbers by spaces |
|||
mov ah,2 ; MS-DOS print character |
|||
int 21h |
|||
pop bx ; Restore counter |
|||
pop ax ; Restore number |
|||
inc ax ; Next number |
|||
dec bl ; Are we done yet? |
|||
jnz printn |
|||
ret</syntaxhighlight> |
|||
{{out}} |
|||
Line breaks added for clarity. |
|||
<pre>0th 1st 2nd 3rd 4th 5th 6th 7th 8th 9th 10th 11th 12th 13th 14th 15th |
|||
16th 17th 18th 19th 20th 21st 22nd 23rd 24th 25th 250th 251st 252nd |
|||
253rd 254th 255th 256th 257th 258th 259th 260th 261st 262nd 263rd 264th |
|||
265th 1000th 1001st 1002nd 1003rd 1004th 1005th 1006th 1007th 1008th |
|||
1009th 1010th 1011th 1012th 1013th 1014th 1015th 1016th 1017th 1018th |
|||
1019th 1020th 1021st 1022nd 1023rd 1024th 1025th</pre> |
|||
=={{header|Action!}}== |
|||
<syntaxhighlight lang="action!">PROC Nth(CARD val,CHAR ARRAY s) |
|||
CHAR ARRAY sfx |
|||
BYTE d |
|||
StrC(val,s) |
|||
s(0)=s(0)+1 |
|||
s(s(0))='' |
|||
d=val MOD 100 |
|||
IF d>10 AND d<14 THEN |
|||
sfx="th" |
|||
ELSE |
|||
d=val MOD 10 |
|||
IF d=1 THEN sfx="st" |
|||
ELSEIF d=2 THEN sfx="nd" |
|||
ELSEIF d=3 THEN sfx="rd" |
|||
ELSE sfx="th" |
|||
FI |
|||
FI |
|||
s(0)=s(0)+2 |
|||
SAssign(s,sfx,s(0)-1,s(0)+1) |
|||
RETURN |
|||
PROC Main() |
|||
CARD ARRAY n=[0 250 1000] |
|||
CHAR ARRAY s(10) |
|||
CARD i,j |
|||
FOR i=0 TO 2 |
|||
DO |
|||
FOR j=n(i) TO n(i)+25 |
|||
DO |
|||
Nth(j,s) |
|||
PrintF("%S ",s) |
|||
OD |
|||
PutE() PutE() |
|||
OD |
|||
RETURN</syntaxhighlight> |
|||
{{out}} |
|||
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/N'th.png Screenshot from Atari 8-bit computer] |
|||
<pre> |
|||
0'th 1'st 2'nd 3'rd 4'th 5'th 6'th 7'th 8'th 9'th 10'th 11'th 12'th 13'th 14'th 15'th 16'th 17'th 18'th 19'th 20'th 21'st 22'nd 23'rd 24'th 25'th |
|||
250'th 251'st 252'nd 253'rd 254'th 255'th 256'th 257'th 258'th 259'th 260'th 261'st 262'nd 263'rd 264'th 265'th 266'th 267'th 268'th 269'th 270'th 271'st 272'nd 273'rd 274'th 275'th |
|||
1000'th 1001'st 1002'nd 1003'rd 1004'th 1005'th 1006'th 1007'th 1008'th 1009'th 1010'th 1011'th 1012'th 1013'th 1014'th 1015'th 1016'th 1017'th 1018'th 1019'th 1020'th 1021'st 1022'nd 1023'rd 1024'th 1025'th |
|||
</pre> |
|||
=={{header|Ada}}== |
=={{header|Ada}}== |
||
< |
<syntaxhighlight lang="ada">with Ada.Text_IO; |
||
procedure Nth is |
procedure Nth is |
||
Line 36: | Line 359: | ||
Print_Images( 250, 265); |
Print_Images( 250, 265); |
||
Print_Images(1000, 1025); |
Print_Images(1000, 1025); |
||
end Nth;</ |
end Nth;</syntaxhighlight> |
||
{{Out}} |
{{Out}} |
||
Line 45: | Line 368: | ||
=={{header|ALGOL 68}}== |
=={{header|ALGOL 68}}== |
||
{{works with|ALGOL 68G|Any - tested with release 2.6.win32}} |
{{works with|ALGOL 68G|Any - tested with release 2.6.win32}} |
||
< |
<syntaxhighlight lang="algol68"># PROC to suffix a number with st, nd, rd or th as appropriate # |
||
PROC nth = ( INT number )STRING: |
PROC nth = ( INT number )STRING: |
||
BEGIN |
BEGIN |
||
Line 93: | Line 416: | ||
test nth( 1000, 1025 ) |
test nth( 1000, 1025 ) |
||
)</ |
)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 110: | Line 433: | ||
</pre> |
</pre> |
||
=={{header|Applesoft BASIC}}== |
|||
<lang ApplesoftBasic>0 OP = 1 |
|||
10 FOR N = 0 TO 25 : GOSUB 100 : NEXT |
|||
20 FOR N = 250 TO 265 : GOSUB 100 : NEXT |
|||
30 FOR N = 1000 TO 1025 : GOSUB 100 : NEXT |
|||
40 END |
|||
=={{header|ALGOL W}}== |
|||
100 GOSUB 200"NTH |
|||
<syntaxhighlight lang="algolw"> |
|||
110 PRINT NTH$ " "; |
|||
begin % suffix number with st, nd, rd or th as appropriate % |
|||
120 RETURN |
|||
string(2) procedure ordinalSuffix ( integer value number ) ; |
|||
200 M1 = N - INT(N / 10) * 10 |
|||
begin |
|||
210 M2 = N - INT(N / 100) * 100 |
|||
integer numberRem100; |
|||
220 NTH$ = "TH" |
|||
numberRem100 := number rem 100; |
|||
if numberRem100 >= 10 and numberRem100 <= 20 then begin |
|||
240 IF M1 = 2 AND M2 <> 12 THEN NTH$ = "ND" |
|||
% numbers in the range 10 .. 20 always have "th" % |
|||
250 IF M1 = 3 AND M2 <> 13 THEN NTH$ = "RD" |
|||
"th" |
|||
end |
|||
270 NTH$ = STR$(N) + NTH$ |
|||
else begin |
|||
280 RETURN</lang> |
|||
% not in the range 10 .. 20, suffix is st, nd, rd or th % |
|||
% depending on the final digit % |
|||
integer numberRem10; |
|||
numberRem10 := number rem 10; |
|||
if numberRem10 = 1 then "st" |
|||
else if numberRem10 = 2 then "nd" |
|||
else if numberRem10 = 3 then "rd" |
|||
else "th" |
|||
end if_numberRem100_in_10_to_20__ |
|||
end ordinalSuffix ; |
|||
% tests ordinalSuffix, displays the suffix for all numbers in from .. to % |
|||
procedure testSuffix ( integer value from, to ) ; |
|||
begin |
|||
integer count; |
|||
count := 0; |
|||
for testValue := from until to do begin |
|||
writeon( i_w := 4, s_w := 0, " ", testValue, ordinalSuffix( testValue ) ); |
|||
count := count + 1; |
|||
if count rem 8 = 0 then write() |
|||
end for_testValue ; |
|||
if count rem 8 not = 0 then write(); |
|||
write() |
|||
end testSuffix ; |
|||
begin % task % |
|||
testSuffix( 0, 25 ); |
|||
testSuffix( 250, 265 ); |
|||
testSuffix( 1000, 1025 ) |
|||
end |
|||
end. |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
0th 1st 2nd 3rd 4th 5th 6th 7th |
|||
8th 9th 10th 11th 12th 13th 14th 15th |
|||
16th 17th 18th 19th 20th 21st 22nd 23rd |
|||
24th 25th |
|||
250th 251st 252nd 253rd 254th 255th 256th 257th |
|||
258th 259th 260th 261st 262nd 263rd 264th 265th |
|||
1000th 1001st 1002nd 1003rd 1004th 1005th 1006th 1007th |
|||
1008th 1009th 1010th 1011th 1012th 1013th 1014th 1015th |
|||
1016th 1017th 1018th 1019th 1020th 1021st 1022nd 1023rd |
|||
1024th 1025th |
|||
</pre> |
|||
=={{header|APL}}== |
|||
{{works with|Dyalog APL}} |
|||
<syntaxhighlight lang="apl"> nth←{ |
|||
sfx←4 2⍴'stndrdth' |
|||
tens←(10<100|⍵)∧20>100|⍵ |
|||
(⍕⍵),sfx[(4×tens)⌈(⍳3)⍳10|⍵;] |
|||
}</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
2 13⍴ nth¨ 0,⍳25 |
|||
0th 1st 2nd 3rd 4th 5th 6th 7th 8th 9th 10th 11th 12th |
|||
13th 14th 15th 16th 17th 18th 19th 20th 21st 22nd 23rd 24th 25th |
|||
2 7⍴ nth¨ 250+0,⍳15 |
|||
250th 251st 252nd 253rd 254th 255th 256th |
|||
257th 258th 259th 260th 261st 262nd 263rd |
|||
2 13⍴ nth¨ 1000+0,⍳25 |
|||
1000th 1001st 1002nd 1003rd 1004th 1005th 1006th 1007th 1008th 1009th 1010th 1011th 1012th |
|||
1013th 1014th 1015th 1016th 1017th 1018th 1019th 1020th 1021st 1022nd 1023rd 1024th 1025th |
|||
</pre> |
|||
=={{header|AppleScript}}== |
|||
<syntaxhighlight lang="applescript">-- ORDINAL STRINGS ----------------------------------------------------------- |
|||
-- ordinalString :: Int -> String |
|||
on ordinalString(n) |
|||
(n as string) & ordinalSuffix(n) |
|||
end ordinalString |
|||
-- ordinalSuffix :: Int -> String |
|||
on ordinalSuffix(n) |
|||
set modHundred to n mod 100 |
|||
if (11 ≤ modHundred) and (13 ≥ modHundred) then |
|||
"th" |
|||
else |
|||
item ((n mod 10) + 1) of ¬ |
|||
{"th", "st", "nd", "rd", "th", "th", "th", "th", "th", "th"} |
|||
end if |
|||
end ordinalSuffix |
|||
-- TEST ---------------------------------------------------------------------- |
|||
on run |
|||
-- showOrdinals :: [Int] -> [String] |
|||
script showOrdinals |
|||
on |λ|(lstInt) |
|||
map(ordinalString, lstInt) |
|||
end |λ| |
|||
end script |
|||
map(showOrdinals, ¬ |
|||
map(uncurry(enumFromTo), ¬ |
|||
[[0, 25], [250, 265], [1000, 1025]])) |
|||
end run |
|||
-- GENERIC FUNCTIONS --------------------------------------------------------- |
|||
-- enumFromTo :: Int -> Int -> [Int] |
|||
on enumFromTo(m, n) |
|||
if m > n then |
|||
set d to -1 |
|||
else |
|||
set d to 1 |
|||
end if |
|||
set lst to {} |
|||
repeat with i from m to n by d |
|||
set end of lst to i |
|||
end repeat |
|||
return lst |
|||
end enumFromTo |
|||
-- map :: (a -> b) -> [a] -> [b] |
|||
on map(f, xs) |
|||
tell mReturn(f) |
|||
set lng to length of xs |
|||
set lst to {} |
|||
repeat with i from 1 to lng |
|||
set end of lst to |λ|(item i of xs, i, xs) |
|||
end repeat |
|||
return lst |
|||
end tell |
|||
end map |
|||
-- Lift 2nd class handler function into 1st class script wrapper |
|||
-- mReturn :: Handler -> Script |
|||
on mReturn(f) |
|||
if class of f is script then |
|||
f |
|||
else |
|||
script |
|||
property |λ| : f |
|||
end script |
|||
end if |
|||
end mReturn |
|||
-- uncurry :: Handler (a -> b -> c) -> Script |λ| ((a, b) -> c) |
|||
on uncurry(f) |
|||
script |
|||
on |λ|(xy) |
|||
set {x, y} to xy |
|||
mReturn(f)'s |λ|(x, y) |
|||
end |λ| |
|||
end script |
|||
end uncurry</syntaxhighlight> |
|||
{{Out}} |
{{Out}} |
||
<pre>{{"0th", "1st", "2nd", "3rd", "4th", "5th", "6th", "7th", "8th", "9th", |
|||
<pre>0'TH 1'ST 2'ND 3'RD 4'TH 5'TH 6'TH 7'TH 8'TH 9'TH 10'TH 11'TH 12'TH 13'TH 14'TH 15'TH 16'TH 17'TH 18'TH 19'TH 20'TH 21'ST 22'ND 23'RD 24'TH 25'TH 250'TH 251'ST 252'ND 253'RD 254'TH 255'TH 256'TH 257'TH 258'TH 259'TH 260'TH 261'ST 262'ND 263'RD 264'TH 265'TH 1000'TH 1001'ST 1002'ND 1003'RD 1004'TH 1005'TH 1006'TH 1007'TH 1008'TH 1009'TH 1010'TH 1011'TH 1012'TH 1013'TH 1014'TH 1015'TH 1016'TH 1017'TH 1018'TH 1019'TH 1020'TH 1021'ST 1022'ND 1023'RD 1024'TH 1025'TH</pre> |
|||
"10th", "11th", "12th", "13th", "14th", "15th", "16th", "17th", "18th", |
|||
"19th", "20th", "21st", "22nd", "23rd", "24th", "25th"}, |
|||
{"250th", "251st", "252nd", "253rd", "254th", "255th", "256th", "257th", |
|||
"258th", "259th", "260th", "261st", "262nd", "263rd", "264th", "265th"}, |
|||
{"1000th", "1001st", "1002nd", "1003rd", "1004th", "1005th", "1006th", |
|||
"1007th", "1008th", "1009th", "1010th", "1011th", "1012th", "1013th", |
|||
"1014th", "1015th", "1016th", "1017th", "1018th", "1019th", "1020th", |
|||
"1021st", "1022nd", "1023rd", "1024th", "1025th"}}</pre> |
|||
=={{header|Arturo}}== |
|||
<syntaxhighlight lang="rebol">suffixes: ["th" "st" "nd" "rd" "th" "th" "th" "th" "th" "th"] |
|||
nth: function [n][ |
|||
if? or? 100 >= n%100 |
|||
20 < n%100 |
|||
-> (to :string n) ++ "'" ++ suffixes\[n%10] |
|||
else -> (to :string n) ++ "'th" |
|||
] |
|||
loop range.step:250 0 1000 'j [ |
|||
loop j..j+24 'i -> |
|||
prints (nth i)++" " |
|||
print "" |
|||
]</syntaxhighlight> |
|||
{{out}} |
|||
<pre>0'th 1'st 2'nd 3'rd 4'th 5'th 6'th 7'th 8'th 9'th 10'th 11'st 12'nd 13'rd 14'th 15'th 16'th 17'th 18'th 19'th 20'th 21'st 22'nd 23'rd 24'th |
|||
250'th 251'st 252'nd 253'rd 254'th 255'th 256'th 257'th 258'th 259'th 260'th 261'st 262'nd 263'rd 264'th 265'th 266'th 267'th 268'th 269'th 270'th 271'st 272'nd 273'rd 274'th |
|||
500'th 501'st 502'nd 503'rd 504'th 505'th 506'th 507'th 508'th 509'th 510'th 511'st 512'nd 513'rd 514'th 515'th 516'th 517'th 518'th 519'th 520'th 521'st 522'nd 523'rd 524'th |
|||
750'th 751'st 752'nd 753'rd 754'th 755'th 756'th 757'th 758'th 759'th 760'th 761'st 762'nd 763'rd 764'th 765'th 766'th 767'th 768'th 769'th 770'th 771'st 772'nd 773'rd 774'th |
|||
1000'th 1001'st 1002'nd 1003'rd 1004'th 1005'th 1006'th 1007'th 1008'th 1009'th 1010'th 1011'st 1012'nd 1013'rd 1014'th 1015'th 1016'th 1017'th 1018'th 1019'th 1020'th 1021'st 1022'nd 1023'rd 1024'th</pre> |
|||
=={{header|AutoHotkey}}== |
=={{header|AutoHotkey}}== |
||
{{works with|AutoHotkey 1.1}} |
{{works with|AutoHotkey 1.1}} |
||
< |
<syntaxhighlight lang="autohotkey">for k, v in [[0, 25], [250, 265], [1000, 1025]] { |
||
while v[1] <= v[2] { |
while v[1] <= v[2] { |
||
Out .= Ordinal(v[1]) " " |
Out .= Ordinal(v[1]) " " |
||
Line 150: | Line 655: | ||
s1 := Mod(n, 10) |
s1 := Mod(n, 10) |
||
return n (s1 = 1 ? "st" : s1 = 2 ? "nd" : s1 = 3 ? "rd" : "th") |
return n (s1 = 1 ? "st" : s1 = 2 ? "nd" : s1 = 3 ? "rd" : "th") |
||
}</ |
}</syntaxhighlight> |
||
{{Out}} |
{{Out}} |
||
<pre>0th 1st 2nd 3rd 4th 5th 6th 7th 8th 9th 10th 11th 12th 13th 14th 15th 16th 17th 18th 19th 20th 21st 22nd 23rd 24th 25th |
<pre>0th 1st 2nd 3rd 4th 5th 6th 7th 8th 9th 10th 11th 12th 13th 14th 15th 16th 17th 18th 19th 20th 21st 22nd 23rd 24th 25th |
||
Line 157: | Line 662: | ||
=={{header|AWK}}== |
=={{header|AWK}}== |
||
<syntaxhighlight lang="awk"> |
|||
<lang AWK> |
|||
# syntax: GAWK -f NTH.AWK |
# syntax: GAWK -f NTH.AWK |
||
BEGIN { |
BEGIN { |
||
Line 190: | Line 695: | ||
return(nthday) |
return(nthday) |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 200: | Line 705: | ||
=={{header|Babel}}== |
=={{header|Babel}}== |
||
< |
<syntaxhighlight lang="babel">((irregular ("st" "nd" "rd")) |
||
(main |
(main |
||
Line 227: | Line 732: | ||
. . } |
. . } |
||
{ -> %d "'th" . } |
{ -> %d "'th" . } |
||
ifte }))</ |
ifte }))</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 240: | Line 745: | ||
1008'th 1007'th 1006'th 1005'th 1004'th 1003'rd 1002'nd 1001'st 1000'th</pre> |
1008'th 1007'th 1006'th 1005'th 1004'th 1003'rd 1002'nd 1001'st 1000'th</pre> |
||
=={{header| |
=={{header|BASIC}}== |
||
==={{header|ANSI BASIC}}=== |
|||
<lang dos>@echo off |
|||
{{trans|Ada}} |
|||
::Main thing... |
|||
{{works with|Decimal BASIC}} |
|||
call :Nth 0 25 |
|||
<syntaxhighlight lang="basic"> |
|||
call :Nth 250 265 |
|||
100 REM Nth |
|||
call :Nth 1000 1025 |
|||
110 DECLARE EXTERNAL FUNCTION Suffix$ |
|||
pause |
|||
120 DECLARE EXTERNAL SUB PrintImages |
|||
exit /b |
|||
130 CALL PrintImages(0, 25) |
|||
140 CALL PrintImages(250, 265) |
|||
150 CALL PrintImages(1000, 1025) |
|||
160 END |
|||
170 REM |
|||
180 EXTERNAL SUB PrintImages(LoLim, HiLim) |
|||
190 FOR I = LoLim TO HiLim |
|||
200 PRINT STR$(I); Suffix$(I); " "; |
|||
210 NEXT I |
|||
220 PRINT |
|||
230 END SUB |
|||
240 REM |
|||
250 EXTERNAL FUNCTION Suffix$(N) |
|||
260 LET NMod10 = MOD(N, 10) |
|||
270 LET NMod100 = MOD(N, 100) |
|||
280 IF NMod10 = 1 AND NMod100 <> 11 THEN |
|||
290 LET Suffix$ = "st" |
|||
300 ELSEIF NMod10 = 2 AND NMod100 <> 12 THEN |
|||
310 LET Suffix$ = "nd" |
|||
320 ELSEIF NMod10 = 3 AND NMod100 <> 13 THEN |
|||
330 LET Suffix$ = "rd" |
|||
340 ELSE |
|||
350 LET Suffix$ = "th" |
|||
360 END IF |
|||
370 END FUNCTION |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
0th 1st 2nd 3rd 4th 5th 6th 7th 8th 9th 10th 11th 12th 13th 14th 15th 16th 17th 18th 19th 20th 21st 22nd 23rd 24th 25th |
|||
250th 251st 252nd 253rd 254th 255th 256th 257th 258th 259th 260th 261st 262nd 263rd 264th 265th |
|||
1000th 1001st 1002nd 1003rd 1004th 1005th 1006th 1007th 1008th 1009th 1010th 1011th 1012th 1013th 1014th 1015th 1016th 1017th 1018th 1019th 1020th 1021st 1022nd 1023rd 1024th 1025th |
|||
</pre> |
|||
==={{header|Applesoft BASIC}}=== |
|||
<syntaxhighlight lang="applesoftbasic">0 OP = 1 |
|||
10 FOR N = 0 TO 25 : GOSUB 100 : NEXT |
|||
20 FOR N = 250 TO 265 : GOSUB 100 : NEXT |
|||
30 FOR N = 1000 TO 1025 : GOSUB 100 : NEXT |
|||
40 END |
|||
100 GOSUB 200"NTH |
|||
::The subroutine |
|||
110 PRINT NTH$ " "; |
|||
:Nth <lbound> <ubound> |
|||
120 RETURN |
|||
setlocal enabledelayedexpansion |
|||
for /l %%n in (%~1,1,%~2) do ( |
|||
200 M1 = N - INT(N / 10) * 10 |
|||
set curr_num=%%n |
|||
210 M2 = N - INT(N / 100) * 100 |
|||
set "out=%%nth" |
|||
220 NTH$ = "TH" |
|||
if !curr_num:~-1!==1 (set "out=%%nst") |
|||
230 IF M1 = 1 AND M2 <> 11 THEN NTH$ = "ST" |
|||
if !curr_num:~-1!==2 (set "out=%%nnd") |
|||
240 IF M1 = 2 AND M2 <> 12 THEN NTH$ = "ND" |
|||
if !curr_num:~-1!==3 (set "out=%%nrd") |
|||
250 IF M1 = 3 AND M2 <> 13 THEN NTH$ = "RD" |
|||
set "range_output=!range_output! !out!" |
|||
260 IF NOT OP THEN NTH$ = "'" + NTH$ |
|||
) |
|||
270 NTH$ = STR$(N) + NTH$ |
|||
echo."!range_output:~1!" |
|||
280 RETURN</syntaxhighlight> |
|||
goto :EOF</lang> |
|||
{{Out}} |
{{Out}} |
||
<pre>0'TH 1'ST 2'ND 3'RD 4'TH 5'TH 6'TH 7'TH 8'TH 9'TH 10'TH 11'TH 12'TH 13'TH 14'TH 15'TH 16'TH 17'TH 18'TH 19'TH 20'TH 21'ST 22'ND 23'RD 24'TH 25'TH 250'TH 251'ST 252'ND 253'RD 254'TH 255'TH 256'TH 257'TH 258'TH 259'TH 260'TH 261'ST 262'ND 263'RD 264'TH 265'TH 1000'TH 1001'ST 1002'ND 1003'RD 1004'TH 1005'TH 1006'TH 1007'TH 1008'TH 1009'TH 1010'TH 1011'TH 1012'TH 1013'TH 1014'TH 1015'TH 1016'TH 1017'TH 1018'TH 1019'TH 1020'TH 1021'ST 1022'ND 1023'RD 1024'TH 1025'TH</pre> |
|||
<pre>"0th 1st 2nd 3rd 4th 5th 6th 7th 8th 9th 10th 11st 12nd 13rd 14th 15th 16th 17th 18th 19th 20th 21st 22nd 23rd 24th 25th" |
|||
"250th 251st 252nd 253rd 254th 255th 256th 257th 258th 259th 260th 261st 262nd 263rd 264th 265th" |
|||
==={{header|BaCon}}=== |
|||
"1000th 1001st 1002nd 1003rd 1004th 1005th 1006th 1007th 1008th 1009th 1010th 1011st 1012nd 1013rd 1014th 1015th 1016th 1017th 1018th 1019th 1020th 1021st 1022nd 1023rd 1024th 1025th" |
|||
<syntaxhighlight lang="freebasic">' Nth (sans apostrophes) |
|||
Press any key to continue . . .</pre> |
|||
FUNCTION nth$(NUMBER n) TYPE STRING |
|||
LOCAL suffix |
|||
IF n < 0 THEN RETURN STR$(n) |
|||
IF MOD(n, 100) >= 11 AND MOD(n, 100) <= 13 THEN |
|||
suffix = "th" |
|||
ELSE |
|||
suffix = MID$("thstndrdthththththth", MOD(n, 10) * 2 + 1, 2) |
|||
ENDIF |
|||
RETURN CONCAT$(STR$(n), suffix) |
|||
END FUNCTION |
|||
' Test a few ranges |
|||
FOR i = 1 TO 4 |
|||
READ first, last |
|||
per = 1 |
|||
FOR n = first TO last |
|||
PRINT nth$(n) FORMAT "%s " |
|||
' limit to 10 entries per line |
|||
IF per = 10 OR n = last THEN |
|||
per = 1 |
|||
PRINT |
|||
ELSE |
|||
INCR per |
|||
ENDIF |
|||
NEXT |
|||
NEXT |
|||
DATA 0, 25, 250, 265, 1000, 1025, -20, -11</syntaxhighlight> |
|||
{{out}} |
|||
<pre>prompt$ ./nth |
|||
0th 1st 2nd 3rd 4th 5th 6th 7th 8th 9th |
|||
10th 11th 12th 13th 14th 15th 16th 17th 18th 19th |
|||
20th 21st 22nd 23rd 24th 25th |
|||
250th 251st 252nd 253rd 254th 255th 256th 257th 258th 259th |
|||
260th 261st 262nd 263rd 264th 265th |
|||
1000th 1001st 1002nd 1003rd 1004th 1005th 1006th 1007th 1008th 1009th |
|||
1010th 1011th 1012th 1013th 1014th 1015th 1016th 1017th 1018th 1019th |
|||
1020th 1021st 1022nd 1023rd 1024th 1025th |
|||
-20 -19 -18 -17 -16 -15 -14 -13 -12 -11</pre> |
|||
==={{header|BASIC256}}=== |
|||
<syntaxhighlight lang="basic256"> |
|||
function ordinal(n) |
|||
ns$ = string(n) |
|||
begin case |
|||
case right(ns$, 1) = "1" |
|||
if right(ns$, 2) = "11" then return ns$ + "th" |
|||
return ns$ + "st" |
|||
case right(ns$, 1) = "2" |
|||
if right(ns$, 2) = "12" then return ns$ + "th" |
|||
return ns$ + "nd" |
|||
case right(ns$, 1) = "3" |
|||
if right(ns$, 2) = "13" then return ns$ + "th" |
|||
return ns$ + "rd" |
|||
else |
|||
return ns$ + "th" |
|||
end case |
|||
end function |
|||
subroutine imprimeOrdinal(a, b) |
|||
for i = a to b |
|||
print ordinal(i); " "; |
|||
next i |
|||
print |
|||
end subroutine |
|||
call imprimeOrdinal (0, 25) |
|||
call imprimeOrdinal (250, 265) |
|||
call imprimeOrdinal (1000, 1025) |
|||
end |
|||
</syntaxhighlight> |
|||
=={{header|BBC BASIC}}== |
==={{header|BBC BASIC}}=== |
||
{{works with|BBC BASIC for Windows}} |
{{works with|BBC BASIC for Windows}} |
||
< |
<syntaxhighlight lang="bbcbasic"> PROCNth( 0, 25) |
||
PROCNth( 250, 265) |
PROCNth( 250, 265) |
||
PROCNth(1000,1025) |
PROCNth(1000,1025) |
||
Line 285: | Line 899: | ||
NEXT |
NEXT |
||
PRINT |
PRINT |
||
ENDPROC</ |
ENDPROC</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 293: | Line 907: | ||
</pre> |
</pre> |
||
==={{header|Chipmunk Basic}}=== |
|||
{{works with|Chipmunk Basic|3.6.4}} |
|||
{{trans|BASIC256}} |
|||
<syntaxhighlight lang="qbasic">100 cls |
|||
110 imprimeordinal(0,25) |
|||
120 print : print |
|||
130 imprimeordinal(250,265) |
|||
140 print : print |
|||
150 imprimeordinal(1000,1025) |
|||
160 print |
|||
170 end |
|||
180 sub ordinal$(n) |
|||
190 ns$ = str$(n) |
|||
200 ordinal$ = "" |
|||
210 select case right$(ns$,1) |
|||
220 case "1" |
|||
230 if right$(ns$,2) = "11" then ordinal$ = ns$+"th" : goto 340 |
|||
240 ordinal$ = ns$+"st" : goto 340 |
|||
250 case "2" |
|||
260 if right$(ns$,2) = "12" then ordinal$ = ns$+"th" : goto 340 |
|||
270 ordinal$ = ns$+"nd" : goto 340 |
|||
280 case "3" |
|||
290 if right$(ns$,2) = "13" then ordinal$ = ns$+"th" : goto 340 |
|||
300 ordinal$ = ns$+"rd" : goto 340 |
|||
310 case else |
|||
320 ordinal$ = ns$+"th" : goto 340 |
|||
330 end case |
|||
340 return |
|||
350 sub imprimeordinal(a,b) |
|||
360 for i = a to b |
|||
370 print ordinal$(i);" "; |
|||
380 next i |
|||
390 return</syntaxhighlight> |
|||
==={{header|Commodore BASIC}}=== |
|||
{{trans|Applesoft BASIC}} |
|||
{{works with|Commodore BASIC|2.0}} |
|||
<syntaxhighlight lang="basic"> |
|||
0 REM ROSETTACODE.ORG |
|||
1 REM N'TH |
|||
2 REM WRITE A FUNCTION/METHOD/SUBROUTINE/... THAT WHEN GIVEN AN INTEGER GREATER |
|||
3 REM THAN OR EQUAL TO ZERO RETURNS A STRING OF THE NUMBER FOLLOWED BY |
|||
4 REM AN APOSTROPHE THEN THE ORDINAL SUFFIX. |
|||
5 REM BASED ON APPLESOFT BASIC VERSION @ ROSETTACODE.ORG |
|||
6 REM |
|||
7 REM ************************* |
|||
8 PRINT CHR$(14): REM CHANGE TO LOWER/UPPER CASE CHAR SET |
|||
9 OP = 1 |
|||
10 FOR N = 0 TO 25 : GOSUB 100 : NEXT : PRINT |
|||
20 FOR N = 250 TO 265 : GOSUB 100 : NEXT : PRINT |
|||
30 FOR N = 1000 TO 1025 : GOSUB 100 : NEXT : PRINT |
|||
40 END |
|||
50 REM ************************* |
|||
100 GOSUB 200 |
|||
110 PRINT NTH$ " "; |
|||
120 RETURN |
|||
130 REM ************************ |
|||
200 M1 = N - INT(N / 10) * 10 |
|||
210 M2 = N - INT(N / 100) * 100 |
|||
220 NTH$ = "TH" |
|||
230 IF M1 = 1 AND M2 <> 11 THEN NTH$ = "ST" |
|||
240 IF M1 = 2 AND M2 <> 12 THEN NTH$ = "ND" |
|||
250 IF M1 = 3 AND M2 <> 13 THEN NTH$ = "RD" |
|||
260 IF NOT OP THEN NTH$ = "'" + NTH$ |
|||
270 NTH$ = STR$(N) + NTH$ |
|||
280 RETURN |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
Commodore 64 (40 column text) |
|||
<pre> |
|||
0'th 1'st 2'nd 3'rd 4'th 5'th 6't |
|||
h 7'th 8'th 9'th 10'th 11'th 12'th |
|||
13'th 14'th 15'th 16'th 17'th 18' |
|||
th 19'th 20'th 21'st 22'nd 23'rd 2 |
|||
4'th 25'th |
|||
250'th 251'st 252'nd 253'rd 254'th |
|||
255'th 256'th 257'th 258'th 259'th |
|||
260'th 261'st 262'nd 263'rd 264'th |
|||
265'th |
|||
1000'th 1001'st 1002'nd 1003'rd 100 |
|||
4'th 1005'th 1006'th 1007'th 1008'th |
|||
1009'th 1010'th 1011'th 1012'th 10 |
|||
13'th 1014'th 1015'th 1016'th 1017't |
|||
h 1018'th 1019'th 1020'th 1021'st 1 |
|||
022'nd 1023'rd 1024'th 1025'th |
|||
</pre> |
|||
==={{header|FreeBASIC}}=== |
|||
<syntaxhighlight lang="freebasic">' FB 1.05.0 Win64 |
|||
' Apostrophes NOT used as incorrect English |
|||
Function ordinal(n As UInteger) As String |
|||
Dim ns As String = Str(n) |
|||
Select Case Right(ns, 1) |
|||
Case "0", "4" To "9" |
|||
Return ns + "th" |
|||
Case "1" |
|||
If Right(ns, 2) = "11" Then Return ns + "th" |
|||
Return ns + "st" |
|||
Case "2" |
|||
If Right(ns, 2) = "12" Then Return ns + "th" |
|||
Return ns + "nd" |
|||
Case "3" |
|||
If Right(ns, 2) = "13" Then Return ns + "th" |
|||
Return ns + "rd" |
|||
End Select |
|||
End Function |
|||
Dim i As Integer |
|||
For i = 0 To 25 |
|||
Print ordinal(i); " "; |
|||
Next |
|||
Print : Print |
|||
For i = 250 To 265 |
|||
Print ordinal(i); " "; |
|||
Next |
|||
Print : Print |
|||
For i = 1000 To 1025 |
|||
Print ordinal(i); " "; |
|||
Next |
|||
Print : Print |
|||
Print "Press any key to quit" |
|||
Sleep</syntaxhighlight> |
|||
{{out}} |
|||
<pre>0th 1st 2nd 3rd 4th 5th 6th 7th 8th 9th 10th 11th 12th 13th 14th 15th 16th 17th 18th 19th 20th 21st 22nd 23rd 24th 25th |
|||
250th 251st 252nd 253rd 254th 255th 256th 257th 258th 259th 260th 261st 262nd 263rd 264th 265th |
|||
1000th 1001st 1002nd 1003rd 1004th 1005th 1006th 1007th 1008th 1009th 1010th 1011th 1012th 1013th 1014th 1015th 1016th |
|||
1017th 1018th 1019th 1020th 1021st 1022nd 1023rd 1024th 1025th</pre> |
|||
==={{header|Gambas}}=== |
|||
'''[https://gambas-playground.proko.eu/?gist=6d60749ae886a37f128e75cffc6c7118 Click this link to run this code]''' |
|||
<syntaxhighlight lang="gambas">Public Sub Main() |
|||
Dim siNums As Short[] = [0, 25, 250, 265, 1000, 1025] |
|||
Dim siCount, siNumbers As Short |
|||
Dim sOrdinal As String |
|||
For siNumbers = 0 To 4 Step 2 |
|||
For siCount = siNums[siNumbers] To siNums[siNumbers + 1] |
|||
sOrdinal = "th" |
|||
If Right(Str(siCount), 1) = "1" And Right(Str(siCount), 2) <> "11" Then sOrdinal = "st" |
|||
If Right(Str(siCount), 1) = "2" And Right(Str(siCount), 2) <> "12" Then sOrdinal = "nd" |
|||
If Right(Str(siCount), 1) = "3" And Right(Str(siCount), 2) <> "13" Then sOrdinal = "rd" |
|||
Print siCount & sOrdinal;; |
|||
Next |
|||
Print gb.NewLine |
|||
Next |
|||
End </syntaxhighlight> |
|||
Output: |
|||
<pre> |
|||
0th 1st 2nd 3rd 4th 5th 6th 7th 8th 9th 10th 11th 12th 13th 14th 15th 16th 17th 18th 19th 20th 21st 22nd 23rd 24th 25th |
|||
250th 251st 252nd 253rd 254th 255th 256th 257th 258th 259th 260th 261st 262nd 263rd 264th 265th |
|||
1000th 1001st 1002nd 1003rd 1004th 1005th 1006th 1007th 1008th 1009th 1010th 1011th 1012th 1013th 1014th 1015th 1016th 1017th 1018th 1019th 1020th 1021st 1022nd 1023rd 1024th 1025th |
|||
</pre> |
|||
==={{header|GW-BASIC}}=== |
|||
{{trans|Ada}} |
|||
{{works with|PC-BASIC|any}} |
|||
<syntaxhighlight lang="qbasic"> |
|||
10 ' N'th |
|||
20 LET LOLIM% = 0 |
|||
30 LET HILIM% = 25 |
|||
40 GOSUB 1000 |
|||
50 LET LOLIM% = 250 |
|||
60 LET HILIM% = 265 |
|||
70 GOSUB 1000 |
|||
80 LET LOLIM% = 1000 |
|||
90 LET HILIM% = 1025 |
|||
100 GOSUB 1000 |
|||
110 END |
|||
995 ' Print images |
|||
1000 FOR I% = LOLIM% TO HILIM% |
|||
1010 LET NR% = I% |
|||
1020 GOSUB 1500 |
|||
1030 LET SI$ = STR$(I%) |
|||
1040 PRINT RIGHT$(SI$, LEN(SI$) - 1); SUF$; " "; |
|||
1050 NEXT I% |
|||
1060 PRINT |
|||
1070 RETURN |
|||
1495 ' Get suffix |
|||
1500 IF (NR% MOD 10 = 1) AND (NR% MOD 100 <> 11) THEN LET SUF$ = "st": GOTO 2000 |
|||
1600 IF (NR% MOD 10 = 2) AND (NR% MOD 100 <> 12) THEN LET SUF$ = "nd": GOTO 2000 |
|||
1700 IF (NR% MOD 10 = 3) AND (NR% MOD 100 <> 13) THEN LET SUF$ = "rd": GOTO 2000 |
|||
1800 LET SUF$ = "th" |
|||
2000 RETURN |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
0th 1st 2nd 3rd 4th 5th 6th 7th 8th 9th 10th 11th 12th 13th 14th 15th 16th 17th 18th 19th 20th 21st 22nd 23rd 24th 25th |
|||
250th 251st 252nd 253rd 254th 255th 256th 257th 258th 259th 260th 261st 262nd 263rd 264th 265th |
|||
1000th 1001st 1002nd 1003rd 1004th 1005th 1006th 1007th 1008th 1009th 1010th 1011th 1012th 1013th 1014th 1015th 1016th 1017th 1018th 1019th 1020th 1021st 1022nd 1023rd 1024th 1025th |
|||
</pre> |
|||
==={{header|Liberty BASIC}}=== |
|||
{{trans|Ada}} |
|||
{{works with|Just BASIC|any}} |
|||
{{works with|Run BASIC}} |
|||
<syntaxhighlight lang="lb"> |
|||
call printImages 0, 25 |
|||
call printImages 250, 265 |
|||
call printImages 1000, 1025 |
|||
end |
|||
sub printImages loLim, hiLim |
|||
loLim = int(loLim) |
|||
hiLIm = int(hiLim) |
|||
for i = loLim to hiLim |
|||
print str$(i) + suffix$(i) + " "; |
|||
next i |
|||
print |
|||
end sub |
|||
function suffix$(n) |
|||
n = int(n) |
|||
nMod10 = n mod 10 |
|||
nMod100 = n mod 100 |
|||
if (nMod10 = 1) and (nMod100 <> 11) then |
|||
suffix$ = "st" |
|||
else |
|||
if (nMod10 = 2) and (nMod100 <> 12) then |
|||
suffix$ = "nd" |
|||
else |
|||
if (NMod10 = 3) and (NMod100 <> 13) then |
|||
suffix$ = "rd" |
|||
else |
|||
suffix$ = "th" |
|||
end if |
|||
end if |
|||
end if |
|||
end function |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
0th 1st 2nd 3th 4th 5th 6th 7th 8th 9th 10th 11th 12th 13th 14th 15th 16th 17th 18th 19th 20th 21st 22nd 23th 24th 25th |
|||
250th 251st 252nd 253th 254th 255th 256th 257th 258th 259th 260th 261st 262nd 263th 264th 265th |
|||
1000th 1001st 1002nd 1003th 1004th 1005th 1006th 1007th 1008th 1009th 1010th 1011th 1012th 1013th 1014th 1015th 1016th 1017th 1018th 1019th 1020th 1021st 1022nd 1023th 1024th 1025th |
|||
</pre> |
|||
==={{header|Microsoft Small Basic}}=== |
|||
{{trans|Ada}} |
|||
<syntaxhighlight lang="microsoftsmallbasic"> |
|||
loLim = 0 |
|||
hiLim = 25 |
|||
PrintImages() |
|||
loLim = 250 |
|||
hiLim = 265 |
|||
PrintImages() |
|||
loLim = 1000 |
|||
hiLim = 1025 |
|||
PrintImages() |
|||
Sub PrintImages |
|||
For i = loLim To hiLim |
|||
nr = i |
|||
GetSuffix() |
|||
TextWindow.Write(i + suffix + " ") |
|||
EndFor |
|||
TextWindow.WriteLine("") |
|||
EndSub |
|||
Sub GetSuffix |
|||
rem10 = Math.Remainder(nr, 10) |
|||
rem100 = Math.Remainder(nr, 100) |
|||
If rem10 = 1 And rem100 <> 11 Then |
|||
suffix = "st" |
|||
ElseIf rem10 = 2 And rem100 <> 12 Then |
|||
suffix = "nd" |
|||
ElseIf rem10 = 3 And rem100 <> 13 Then |
|||
suffix = "rd" |
|||
Else |
|||
suffix = "th" |
|||
EndIf |
|||
EndSub |
|||
</syntaxhighlight> |
|||
==={{header|Nascom BASIC}}=== |
|||
{{trans|Ada}} |
|||
{{works with|Nascom ROM BASIC|4.7}} |
|||
<syntaxhighlight lang="basic"> |
|||
10 REM N'th |
|||
20 LOLIM=0 |
|||
30 HILIM=25 |
|||
40 GOSUB 1000 |
|||
50 LOLIM=250 |
|||
60 HILIM=265 |
|||
70 GOSUB 1000 |
|||
80 LOLIM=1000 |
|||
90 HILIM=1025 |
|||
100 GOSUB 1000 |
|||
110 END |
|||
995 REM ** Print images |
|||
1000 FOR I=LOLIM TO HILIM |
|||
1010 NR=I |
|||
1020 GOSUB 1500 |
|||
1030 SI$=STR$(I) |
|||
1040 PRINT RIGHT$(SI$,LEN(SI$)-1);SUF$;" "; |
|||
1050 NEXT I |
|||
1060 PRINT |
|||
1070 RETURN |
|||
1495 REM ** Get suffix |
|||
1500 LA=NR-INT(NR/10)*10:NLA=NR-INT(NR/100)*100 |
|||
1510 IF LA=1 AND NLA<>11 THEN SUF$="st":RETURN |
|||
1520 IF LA=2 AND NLA<>12 THEN SUF$="nd":RETURN |
|||
1530 IF LA=3 AND NLA<>13 THEN SUF$="rd":RETURN |
|||
1540 SUF$="th" |
|||
1550 RETURN |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
0th 1st 2nd 3rd 4th 5th 6th 7th 8th 9th 10th 11th 12th 13th 14th 15th 16th 17th 18th 19th 20th 21st 22nd 23rd 24th 25th |
|||
250th 251st 252nd 253rd 254th 255th 256th 257th 258th 259th 260th 261st 262nd 263rd 264th 265th |
|||
1000th 1001st 1002nd 1003rd 1004th 1005th 1006th 1007th 1008th 1009th 1010th 1011th 1012th 1013th 1014th 1015th 1016th 1017th 1018th 1019th 1020th 1021st 1022nd 1023rd 1024th 1025th |
|||
</pre> |
|||
==={{header|PureBasic}}=== |
|||
<syntaxhighlight lang="purebasic">Procedure.s Suffix(n.i) |
|||
Select n%10 |
|||
Case 1 : If n%100<>11 : ProcedureReturn "st" : EndIf |
|||
Case 2 : If n%100<>12 : ProcedureReturn "nd" : EndIf |
|||
Case 3 : If n%100<>13 : ProcedureReturn "rd" : EndIf |
|||
EndSelect |
|||
ProcedureReturn "th" |
|||
EndProcedure |
|||
Procedure put(a.i,b.i) |
|||
For i=a To b : Print(Str(i)+Suffix(i)+" ") : Next |
|||
PrintN("") |
|||
EndProcedure |
|||
If OpenConsole()=0 : End 1 : EndIf |
|||
put(0,25) |
|||
put(250,265) |
|||
put(1000,1025) |
|||
Input()</syntaxhighlight> |
|||
{{out}} |
|||
<pre>0th 1st 2nd 3rd 4th 5th 6th 7th 8th 9th 10th 11th 12th 13th 14th 15th 16th 17th 18th 19th 20th 21st 22nd 23rd 24th 25th |
|||
250th 251st 252nd 253rd 254th 255th 256th 257th 258th 259th 260th 261st 262nd 263rd 264th 265th |
|||
1000th 1001st 1002nd 1003rd 1004th 1005th 1006th 1007th 1008th 1009th 1010th 1011th 1012th 1013th 1014th 1015th 1016th 1017th 1018th 1019th 1020th 1021st 1022nd 1023rd 1024th 1025th </pre> |
|||
==={{header|QBasic}}=== |
|||
<syntaxhighlight lang="basic"> |
|||
DECLARE FUNCTION sufijo$ (n%) |
|||
DECLARE SUB imprimeOrdinal (loLim%, hiLim%) |
|||
imprimeOrdinal 0, 25 |
|||
imprimeOrdinal 250, 265 |
|||
imprimeOrdinal 1000, 1025 |
|||
END |
|||
DEFINT A-Z |
|||
SUB imprimeOrdinal (loLim, hiLim) |
|||
loLim = INT(loLim) |
|||
hiLim = INT(hiLim) |
|||
FOR i = loLim TO hiLim |
|||
PRINT STR$(i) + sufijo$(i) + " "; |
|||
NEXT i |
|||
PRINT |
|||
END SUB |
|||
FUNCTION sufijo$ (n) |
|||
n = INT(n) |
|||
NMod10 = n MOD 10 |
|||
NMod100 = n MOD 100 |
|||
IF (NMod10 = 1) AND (NMod100 <> 11) THEN |
|||
sufijo$ = "st" |
|||
ELSE |
|||
IF (NMod10 = 2) AND (NMod100 <> 12) THEN |
|||
sufijo$ = "nd" |
|||
ELSE |
|||
IF (NMod10 = 3) AND (NMod100 <> 13) THEN |
|||
sufijo$ = "rd" |
|||
ELSE |
|||
sufijo$ = "th" |
|||
END IF |
|||
END IF |
|||
END IF |
|||
END FUNCTION |
|||
</syntaxhighlight> |
|||
==={{header|Run BASIC}}=== |
|||
The [[#Liberty_BASIC|Liberty BASIC]] solution works without any changes. |
|||
==={{header|Sinclair ZX81 BASIC}}=== |
|||
Works flawlessly with 2k or more of RAM. With 1k, the subroutine itself works but you can't quite print all the tests: the program crashes with an 'out of memory' error code after 1017th. (A slightly less useful and readable version gets as far as 1023rd; 1025th is probably attainable, but might involve obfuscating the program more than is appropriate for this site.) |
|||
<syntaxhighlight lang="basic"> 10 FOR N=0 TO 25 |
|||
20 GOSUB 160 |
|||
30 PRINT N$;" "; |
|||
40 NEXT N |
|||
50 PRINT |
|||
60 FOR N=250 TO 265 |
|||
70 GOSUB 160 |
|||
80 PRINT N$;" "; |
|||
90 NEXT N |
|||
100 PRINT |
|||
110 FOR N=1000 TO 1025 |
|||
120 GOSUB 160 |
|||
130 PRINT N$;" "; |
|||
140 NEXT N |
|||
150 STOP |
|||
160 LET N$=STR$ N |
|||
170 LET S$="TH" |
|||
180 IF LEN N$=1 THEN GOTO 200 |
|||
190 IF N$(LEN N$-1)="1" THEN GOTO 230 |
|||
200 IF N$(LEN N$)="1" THEN LET S$="ST" |
|||
210 IF N$(LEN N$)="2" THEN LET S$="ND" |
|||
220 IF N$(LEN N$)="3" THEN LET S$="RD" |
|||
230 LET N$=N$+S$ |
|||
240 RETURN</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
0TH 1ST 2ND 3RD 4TH 5TH 6TH 7TH 8TH 9TH 10TH 11TH 12TH 13TH 14TH 15TH 16TH 17TH 18TH 19TH 20TH 21ST 22ND 23RD 24TH 25TH |
|||
250TH 251ST 252ND 253RD 254TH 255TH 256TH 257TH 258TH 259TH 260TH 261ST 262ND 263RD 264TH 265TH |
|||
1000TH 1001ST 1002ND 1003RD 1004TH 1005TH 1006TH 1007TH 1008TH 1009TH 1010TH 1011TH 1012TH 1013TH 1014TH 1015TH 1016TH 1017TH 1018TH 1019TH 1020TH 1021ST 1022ND 1023RD 1024TH 1025TH</pre> |
|||
==={{header|True BASIC}}=== |
|||
<syntaxhighlight lang="basic"> |
|||
SUB sufijo (n) |
|||
LET n = INT(n) |
|||
LET NMod10 = MOD(n, 10) |
|||
LET NMod100 = MOD(n, 100) |
|||
IF (NMod10 = 1) AND (NMod100 <> 11) THEN |
|||
LET sufi$ = "st" |
|||
ELSE |
|||
IF (NMod10 = 2) AND (NMod100 <> 12) THEN |
|||
LET sufi$ = "nd" |
|||
ELSE |
|||
IF (NMod10 = 3) AND (NMod100 <> 13) THEN |
|||
LET sufi$ = "rd" |
|||
ELSE |
|||
LET sufi$ = "th" |
|||
END IF |
|||
END IF |
|||
END IF |
|||
PRINT sufi$; |
|||
END SUB |
|||
SUB imprimeOrdinal (loLim, hiLim) |
|||
LET loLim = INT(loLim) |
|||
LET hiLim = INT(hiLim) |
|||
FOR i = loLim TO hiLim |
|||
PRINT i; |
|||
CALL sufijo (i) |
|||
PRINT " "; |
|||
NEXT i |
|||
PRINT |
|||
END SUB |
|||
CALL imprimeOrdinal (0, 25) |
|||
CALL imprimeOrdinal (250, 265) |
|||
CALL imprimeOrdinal (1000, 1025) |
|||
END |
|||
</syntaxhighlight> |
|||
==={{header|uBasic/4tH}}=== |
|||
<syntaxhighlight lang="text">For x = 0 to 25 ' Test range 0..25 |
|||
Push x : GoSub _PrintOrdinal |
|||
Next x : Print |
|||
For x = 250 to 265 ' Test range 250..265 |
|||
Push x : GoSub _PrintOrdinal |
|||
Next x : Print |
|||
For x = 1000 to 1025 ' Test range 1000..1025 |
|||
Push x : GoSub _PrintOrdinal |
|||
Next x : Print |
|||
End ' End test program |
|||
' ( n --) |
|||
_PrintOrdinal ' Ordinal subroutine |
|||
If Tos() > -1 Then ' If within range then |
|||
Print Using "____#";Tos();"'"; ' Print the number |
|||
' Take care of 11, 12 and 13 |
|||
If (Tos()%100 > 10) * (Tos()%100 < 14) Then |
|||
Gosub (Pop() * 0) + 100 ' Clear stack and print "th" |
|||
Return ' We're done here |
|||
EndIf |
|||
Push Pop() % 10 ' Calculate n mod 10 |
|||
GoSub 100 + 10 * ((Tos()>0) + (Tos()>1) + (Tos()>2) - (3 * (Pop()>3))) |
|||
Else ' And decide which ordinal to use |
|||
Print Pop();" is less than zero" ' Otherwise, it is an error |
|||
EndIf |
|||
Return |
|||
' Select and print proper ordinal |
|||
100 Print "th"; : Return |
|||
110 Print "st"; : Return |
|||
120 Print "nd"; : Return |
|||
130 Print "rd"; : Return</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
0'th 1'st 2'nd 3'rd 4'th 5'th 6'th 7'th 8'th 9'th 10'th 11'th 12'th 13'th 14'th 15'th 16'th 17'th 18'th 19'th 20'th 21'st 22'nd 23'rd 24'th 25'th |
|||
250'th 251'st 252'nd 253'rd 254'th 255'th 256'th 257'th 258'th 259'th 260'th 261'st 262'nd 263'rd 264'th 265'th |
|||
1000'th 1001'st 1002'nd 1003'rd 1004'th 1005'th 1006'th 1007'th 1008'th 1009'th 1010'th 1011'th 1012'th 1013'th 1014'th 1015'th 1016'th 1017'th 1018'th 1019'th 1020'th 1021'st 1022'nd 1023'rd 1024'th 1025'th |
|||
</pre> |
|||
==={{header|VBA}}=== |
|||
{{trans|Phix}}<syntaxhighlight lang="vb">Private Function ordinals() As Variant |
|||
ordinals = [{"th","st","nd","rd"}] |
|||
End Function |
|||
Private Function Nth(n As Variant, Optional apostrophe As Boolean = False) As String |
|||
Dim mod10 As Integer: mod10 = n Mod 10 + 1 |
|||
If mod10 > 4 Or n Mod 100 = mod10 + 9 Then mod10 = 1 |
|||
Nth = CStr(n) & String$(Val(-apostrophe), "'") & ordinals()(mod10) |
|||
End Function |
|||
Public Sub main() |
|||
Ranges = [{0,25;250,265;1000,1025}] |
|||
For i = 1 To UBound(Ranges) |
|||
For j = Ranges(i, 1) To Ranges(i, 2) |
|||
If j Mod 10 = 0 Then Debug.Print |
|||
Debug.Print Format(Nth(j, i = 2), "@@@@@@@"); |
|||
Next j |
|||
Debug.Print |
|||
Next i |
|||
End Sub</syntaxhighlight>{{out}} |
|||
<pre> 0th 1st 2nd 3rd 4th 5th 6th 7th 8th 9th |
|||
10th 11th 12th 13th 14th 15th 16th 17th 18th 19th |
|||
20th 21st 22nd 23rd 24th 25th |
|||
250'th 251'st 252'nd 253'rd 254'th 255'th 256'th 257'th 258'th 259'th |
|||
260'th 261'st 262'nd 263'rd 264'th 265'th |
|||
1000th 1001st 1002nd 1003rd 1004th 1005th 1006th 1007th 1008th 1009th |
|||
1010th 1011th 1012th 1013th 1014th 1015th 1016th 1017th 1018th 1019th |
|||
1020th 1021st 1022nd 1023rd 1024th 1025th</pre> |
|||
==={{header|XBasic}}=== |
|||
{{trans|Ada}} |
|||
{{works with|Windows XBasic}} |
|||
<syntaxhighlight lang="xbasic"> |
|||
PROGRAM "nth" |
|||
VERSION "0.0002" |
|||
DECLARE FUNCTION Entry() |
|||
INTERNAL FUNCTION Suffix$(n&&) |
|||
INTERNAL FUNCTION PrintImages (loLim&&, hiLim&&) |
|||
FUNCTION Entry() |
|||
PrintImages( 0, 25) |
|||
PrintImages( 250, 265) |
|||
PrintImages(1000, 1025) |
|||
END FUNCTION |
|||
FUNCTION Suffix$(n&&) |
|||
nMod10@@ = n&& MOD 10 |
|||
nMod100@@ = n&& MOD 100 |
|||
SELECT CASE TRUE |
|||
CASE (nMod10@@ = 1) AND (nMod100@@ <> 11): |
|||
RETURN ("st") |
|||
CASE (nMod10@@ = 2) AND (nMod100@@ <> 12): |
|||
RETURN ("nd") |
|||
CASE (nMod10@@ = 3) AND (nMod100@@ <> 13): |
|||
RETURN ("rd") |
|||
CASE ELSE: |
|||
RETURN ("th") |
|||
END SELECT |
|||
END FUNCTION |
|||
FUNCTION PrintImages(loLim&&, hiLim&&) |
|||
FOR i&& = loLim&& TO hiLim&& |
|||
PRINT TRIM$(STRING$(i&&)); Suffix$(i&&); " "; |
|||
NEXT |
|||
PRINT |
|||
END FUNCTION |
|||
END PROGRAM |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
0th 1st 2nd 3rd 4th 5th 6th 7th 8th 9th 10th 11th 12th 13th 14th 15th 16th 17th 18th 19th 20th 21st 22nd 23rd 24th 25th |
|||
250th 251st 252nd 253rd 254th 255th 256th 257th 258th 259th 260th 261st 262nd 263rd 264th 265th |
|||
1000th 1001st 1002nd 1003rd 1004th 1005th 1006th 1007th 1008th 1009th 1010th 1011th 1012th 1013th 1014th 1015th 1016th 1017th 1018th 1019th 1020th 1021st 1022nd 1023rd 1024th 1025th |
|||
</pre> |
|||
==={{header|Yabasic}}=== |
|||
{{trans|Liberty BASIC}} |
|||
<syntaxhighlight lang="xbasic"> |
|||
sub ordinal$ (n) |
|||
NMod10 = mod(n, 10) |
|||
NMod100 = mod(n, 100) |
|||
if (NMod10 = 1) and (NMod100 <> 11) then |
|||
return "st" |
|||
else |
|||
if (NMod10 = 2) and (NMod100 <> 12) then |
|||
return "nd" |
|||
else |
|||
if (NMod10 = 3) and (NMod100 <> 13) then |
|||
return "rd" |
|||
else |
|||
return "th" |
|||
end if |
|||
end if |
|||
end if |
|||
end sub |
|||
sub imprimeOrdinal(a, b) |
|||
for i = a to b |
|||
print i, ordinal$(i), " "; |
|||
next i |
|||
print |
|||
end sub |
|||
imprimeOrdinal (0, 25) |
|||
imprimeOrdinal (250, 265) |
|||
imprimeOrdinal (1000, 1025) |
|||
end |
|||
</syntaxhighlight> |
|||
==={{header|ZX Spectrum Basic}}=== |
|||
<syntaxhighlight lang="basic"> 10 FOR n=0 TO 25 |
|||
20 GO SUB 140 |
|||
30 PRINT n$;" "; |
|||
40 NEXT n |
|||
50 FOR n=250 TO 265 |
|||
60 GO SUB 140 |
|||
70 PRINT n$;" "; |
|||
80 NEXT n |
|||
90 FOR n=1000 TO 1025 |
|||
100 GO SUB 140 |
|||
110 PRINT n$;" "; |
|||
120 NEXT n |
|||
130 STOP |
|||
140 LET s$="th" |
|||
150 LET n$=STR$ n |
|||
160 IF LEN n$=1 THEN GO TO 180 |
|||
170 IF n$(LEN n$-1)="1" THEN GO TO 210 |
|||
180 IF n$(LEN n$)="1" THEN LET s$="st" |
|||
190 IF n$(LEN n$)="2" THEN LET s$="nd" |
|||
200 IF n$(LEN n$)="3" THEN LET s$="rd" |
|||
210 LET n$=n$+s$ |
|||
220 RETURN</syntaxhighlight> |
|||
{{out}} |
|||
<pre>0th 1st 2nd 3rd 4th 5th 6th 7th 8th 9th 10th 11th 12th 13th 14th 15th 16th 17th 18th 19th 20th 21st 22nd 23rd 24th 25th 250th 251st 252nd 253rd 254th 255th 256th 257th 258th 259th 260th 261st 262nd 263rd 264th 265th 1000th 1001st 1002nd 1003rd 1004th 1005th 1006th 1007th 1008th 1009th 1010th 1011th 1012th 1013th 1014th 1015th 1016th 1017th 1018th 1019th 1020th 1021st 1022nd 1023rd 1024th 1025th</pre> |
|||
=={{header|Batch File}}== |
|||
<syntaxhighlight lang="dos">@echo off |
|||
::Main thing... |
|||
call :Nth 0 25 |
|||
call :Nth 250 265 |
|||
call :Nth 1000 1025 |
|||
pause |
|||
exit /b |
|||
::The subroutine |
|||
:Nth <lbound> <ubound> |
|||
setlocal enabledelayedexpansion |
|||
for /l %%n in (%~1,1,%~2) do ( |
|||
set curr_num=%%n |
|||
if !curr_num:~-2!==11 (set "out=%%nth" |
|||
) else if !curr_num:~-2!==12 (set "out=%%nth" |
|||
) else if !curr_num:~-2!==13 (set "out=%%nth" |
|||
) else if !curr_num:~-1!==1 (set "out=%%nst" |
|||
) else if !curr_num:~-1!==2 (set "out=%%nnd" |
|||
) else if !curr_num:~-1!==3 (set "out=%%nrd" |
|||
) else (set "out=%%nth") |
|||
set "range_output=!range_output! !out!" |
|||
) |
|||
echo."!range_output:~1!" |
|||
goto :EOF</syntaxhighlight> |
|||
{{Out}} |
|||
<pre> |
|||
"0th 1st 2nd 3rd 4th 5th 6th 7th 8th 9th 10th 11th 12th 13th 14th 15th 16th 17th 18th 19th 20th 21st 22nd 23rd 24th 25th" |
|||
"250th 251st 252nd 253rd 254th 255th 256th 257th 258th 259th 260th 261st 262nd 263rd 264th 265th" |
|||
"1000th 1001st 1002nd 1003rd 1004th 1005th 1006th 1007th 1008th 1009th 1010th 1011th 1012th 1013th 1014th 1015th 1016th 1017th 1018th 1019th 1020th 1021st 1022nd 1023rd 1024th 1025th" |
|||
Press any key to continue . . . |
|||
</pre> |
|||
=={{header|BCPL}}== |
|||
<syntaxhighlight lang="bcpl">get "libhdr" |
|||
// Generate ASCII string of number n with ordinal suffix |
|||
// The string is stored at v. |
|||
let nth(n, v) = valof |
|||
$( let sfx = "thstndrd" |
|||
let c, s = 0, ? |
|||
// Generate digits |
|||
$( c := c + 1 |
|||
v%c := n rem 10 + '0' |
|||
n := n / 10 |
|||
$) repeatuntil n = 0 |
|||
// Put digits in correct order |
|||
for i = 1 to c/2 do |
|||
$( let d = v%i |
|||
v%i := v%(c+1-i) |
|||
v%(c+1-i) := d |
|||
$) |
|||
// The length of the string is the amount of digits + 2 |
|||
v%0 := c+2; |
|||
// Figure out the proper suffix from the last two digits |
|||
test v%(c-1)='1' | v%c>'3' |
|||
then s := 0 |
|||
else s := 2*(v%c - '0') |
|||
v%(c+1) := sfx%(s+1) |
|||
v%(c+2) := sfx%(s+2) |
|||
resultis v |
|||
$) |
|||
let start() be |
|||
$( let buf = vec 10 |
|||
for i = 0 to 25 do writef("%S*N", nth(i, buf)) |
|||
for i = 250 to 265 do writef("%S*N", nth(i, buf)) |
|||
for i = 1000 to 1025 do writef("%S*N", nth(i, buf)) |
|||
$)</syntaxhighlight> |
|||
{{out}} |
|||
<pre style="height:50ex">0th |
|||
1st |
|||
2nd |
|||
3rd |
|||
4th |
|||
5th |
|||
6th |
|||
7th |
|||
8th |
|||
9th |
|||
10th |
|||
11th |
|||
12th |
|||
13th |
|||
14th |
|||
15th |
|||
16th |
|||
17th |
|||
18th |
|||
19th |
|||
20th |
|||
21st |
|||
22nd |
|||
23rd |
|||
24th |
|||
25th |
|||
250th |
|||
251st |
|||
252nd |
|||
253rd |
|||
254th |
|||
255th |
|||
256th |
|||
257th |
|||
258th |
|||
259th |
|||
260th |
|||
261st |
|||
262nd |
|||
263rd |
|||
264th |
|||
265th |
|||
1000th |
|||
1001st |
|||
1002nd |
|||
1003rd |
|||
1004th |
|||
1005th |
|||
1006th |
|||
1007th |
|||
1008th |
|||
1009th |
|||
1010th |
|||
1011th |
|||
1012th |
|||
1013th |
|||
1014th |
|||
1015th |
|||
1016th |
|||
1017th |
|||
1018th |
|||
1019th |
|||
1020th |
|||
1021st |
|||
1022nd |
|||
1023rd |
|||
1024th |
|||
1025th</pre> |
|||
=={{header|Befunge}}== |
=={{header|Befunge}}== |
||
The bottom section of code contains the "subroutine" that calculates the ordinal of a given number; the top section generates the list of values to test. |
The bottom section of code contains the "subroutine" that calculates the ordinal of a given number; the top section generates the list of values to test. |
||
< |
<syntaxhighlight lang="befunge">0>55*:>1-\:0\`!v |
||
#v$#$<^:\+*8"}"_ |
#v$#$<^:\+*8"}"_ |
||
>35*:>1-\:0\`!v |
>35*:>1-\:0\`!v |
||
Line 307: | Line 1,709: | ||
htdd >$>:#,_$:vg |
htdd >$>:#,_$:vg |
||
v"d"\*!`3:%+55<9 |
v"d"\*!`3:%+55<9 |
||
>%55+/1-!!*:8g,^</ |
>%55+/1-!!*:8g,^</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>0th 1st 2nd 3rd 4th 5th 6th 7th 8th 9th 10th 11th 12th 13th 14th 15th 16th 17th 18th 19th 20th 21st 22nd 23rd 24th 25th 250th 251st 252nd 253rd 254th 255th 256th 257th 258th 259th 260th 261st 262nd 263rd 264th 265th 1000th 1001st 1002nd 1003rd 1004th 1005th 1006th 1007th 1008th 1009th 1010th 1011th 1012th 1013th 1014th 1015th 1016th 1017th 1018th 1019th 1020th 1021st 1022nd 1023rd 1024th 1025th</pre> |
<pre>0th 1st 2nd 3rd 4th 5th 6th 7th 8th 9th 10th 11th 12th 13th 14th 15th 16th 17th 18th 19th 20th 21st 22nd 23rd 24th 25th 250th 251st 252nd 253rd 254th 255th 256th 257th 258th 259th 260th 261st 262nd 263rd 264th 265th 1000th 1001st 1002nd 1003rd 1004th 1005th 1006th 1007th 1008th 1009th 1010th 1011th 1012th 1013th 1014th 1015th 1016th 1017th 1018th 1019th 1020th 1021st 1022nd 1023rd 1024th 1025th</pre> |
||
=={{header|BQN}}== |
|||
<syntaxhighlight lang="bqn">Nth ← { |
|||
sfx ← ⟨"th","st","nd","rd"⟩ |
|||
(•Fmt 𝕩)∾sfx⊑˜ (1≠10|⌊𝕩÷10)×(3≥10|𝕩)×10|𝕩 |
|||
} |
|||
∘‿4⥊ Nth¨ (↕26) ∾ (250+↕16) ∾ (1000+↕26)</syntaxhighlight> |
|||
{{out}} |
|||
<pre>┌─ |
|||
╵ "0th" "1st" "2nd" "3rd" |
|||
"4th" "5th" "6th" "7th" |
|||
"8th" "9th" "10th" "11th" |
|||
"12th" "13th" "14th" "15th" |
|||
"16th" "17th" "18th" "19th" |
|||
"20th" "21st" "22nd" "23rd" |
|||
"24th" "25th" "250th" "251st" |
|||
"252nd" "253rd" "254th" "255th" |
|||
"256th" "257th" "258th" "259th" |
|||
"260th" "261st" "262nd" "263rd" |
|||
"264th" "265th" "1000th" "1001st" |
|||
"1002nd" "1003rd" "1004th" "1005th" |
|||
"1006th" "1007th" "1008th" "1009th" |
|||
"1010th" "1011th" "1012th" "1013th" |
|||
"1014th" "1015th" "1016th" "1017th" |
|||
"1018th" "1019th" "1020th" "1021st" |
|||
"1022nd" "1023rd" "1024th" "1025th" |
|||
┘</pre> |
|||
=={{header|C}}== |
=={{header|C}}== |
||
< |
<syntaxhighlight lang="c">#include <stdio.h> |
||
char* addSuffix(int num, char* buf, size_t len) |
char* addSuffix(int num, char* buf, size_t len) |
||
Line 364: | Line 1,793: | ||
return 0; |
return 0; |
||
}</ |
}</syntaxhighlight> |
||
Another method with dynamic memory allocation |
|||
<syntaxhighlight lang="c">#include <stdlib.h> |
|||
#include <stdio.h> |
|||
static int digits(const int x) { |
|||
if (x / 10 == 0) return 1; |
|||
return 1 + digits(x / 10); |
|||
} |
|||
static char * get_ordinal(const int i) { |
|||
const int string_size = digits(i) + 3; |
|||
char * o_number = malloc(string_size); |
|||
char * ordinal; |
|||
if(i % 100 >= 11 && i % 100 <= 13) ordinal = "th"; |
|||
else ordinal = i/10==1?"th":i%10==1?"st":i%10==2?"nd":i%10==3?"rd":"th"; |
|||
sprintf_s(o_number, string_size, "%d%s", i, ordinal); |
|||
return o_number; |
|||
} |
|||
static void print_range(const int begin, const int end) { |
|||
printf("Set [%d,%d]:\n", begin, end); |
|||
for (int i = begin; i <= end; i++) { |
|||
char * o_number = get_ordinal(i); |
|||
printf("%s ", o_number); |
|||
free(o_number); |
|||
} |
|||
printf("\n"); |
|||
} |
|||
int main(void) { |
|||
print_range(0, 25); |
|||
print_range(250, 265); |
|||
print_range(1000, 1025); |
|||
}</syntaxhighlight> |
|||
{{out}} |
{{out}} |
||
<pre>Set [0,25] : |
<pre>Set [0,25] : |
||
Line 371: | Line 1,834: | ||
250th 251st 252nd 253rd 254th 255th 256th 257th 258th 259th 260th 261st 262nd 263rd 264th 265th |
250th 251st 252nd 253rd 254th 255th 256th 257th 258th 259th 260th 261st 262nd 263rd 264th 265th |
||
Set [1000,1025] : |
Set [1000,1025] : |
||
1000th 1001st 1002nd 1003rd 1004th 1005th 1006th 1007th 1008th 1009th 1010th 1011th 1012th 1013th 1014th 1015th 1016th 1017th 1018th 1019th 1020th 1021st 1022nd 1023rd 1024th 1025th</pre> |
|||
=={{header|C sharp|C#}}== |
|||
{{trans|Ruby}} |
|||
<syntaxhighlight lang="csharp">using System; |
|||
using System.Linq; |
|||
class Program |
|||
{ |
|||
private static string Ordinalize(int i) |
|||
{ |
|||
i = Math.Abs(i); |
|||
if (new[] {11, 12, 13}.Contains(i%100)) |
|||
return i + "th"; |
|||
switch (i%10) |
|||
{ |
|||
case 1: |
|||
return i + "st"; |
|||
case 2: |
|||
return i + "nd"; |
|||
case 3: |
|||
return i + "rd"; |
|||
default: |
|||
return i + "th"; |
|||
} |
|||
} |
|||
static void Main() |
|||
{ |
|||
Console.WriteLine(string.Join(" ", Enumerable.Range(0, 26).Select(Ordinalize))); |
|||
Console.WriteLine(string.Join(" ", Enumerable.Range(250, 16).Select(Ordinalize))); |
|||
Console.WriteLine(string.Join(" ", Enumerable.Range(1000, 26).Select(Ordinalize))); |
|||
} |
|||
}</syntaxhighlight> |
|||
Dotnet 5 version without LINQ |
|||
<syntaxhighlight lang="csharp">using System; |
|||
static string Ordinalize(int i) |
|||
{ |
|||
return i + ( |
|||
i % 100 is >= 11 and <= 13 ? "th" : |
|||
i % 10 == 1 ? "st" : |
|||
i % 10 == 2 ? "nd" : |
|||
i % 10 == 3 ? "rd" : |
|||
"th"); |
|||
} |
|||
static void PrintRange(int begin, int end) |
|||
{ |
|||
for(var i = begin; i <= end; i++) |
|||
Console.Write(Ordinalize(i) + (i == end ? "" : " ")); |
|||
Console.WriteLine(); |
|||
} |
|||
PrintRange(0, 25); |
|||
PrintRange(250, 265); |
|||
PrintRange(1000, 1025);</syntaxhighlight> |
|||
{{Out}} |
|||
<pre>0th 1st 2nd 3rd 4th 5th 6th 7th 8th 9th 10th 11th 12th 13th 14th 15th 16th 17th 18th 19th 20th 21st 22nd 23rd 24th 25th |
|||
250th 251st 252nd 253rd 254th 255th 256th 257th 258th 259th 260th 261st 262nd 263rd 264th 265th |
|||
1000th 1001st 1002nd 1003rd 1004th 1005th 1006th 1007th 1008th 1009th 1010th 1011th 1012th 1013th 1014th 1015th 1016th 1017th 1018th 1019th 1020th 1021st 1022nd 1023rd 1024th 1025th</pre> |
1000th 1001st 1002nd 1003rd 1004th 1005th 1006th 1007th 1008th 1009th 1010th 1011th 1012th 1013th 1014th 1015th 1016th 1017th 1018th 1019th 1020th 1021st 1022nd 1023rd 1024th 1025th</pre> |
||
=={{header|C++}}== |
=={{header|C++}}== |
||
< |
<syntaxhighlight lang="cpp">#include <string> |
||
#include <iostream> |
#include <iostream> |
||
Line 414: | Line 1,940: | ||
return 0; |
return 0; |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>Set [0,25] : |
<pre>Set [0,25] : |
||
Line 423: | Line 1,949: | ||
1000th 1001st 1002nd 1003rd 1004th 1005th 1006th 1007th 1008th 1009th 1010th 1011th 1012th 1013th 1014th 1015th 1016th 1017th 1018th 1019th 1020th 1021st 1022nd 1023rd 1024th 1025th</pre> |
1000th 1001st 1002nd 1003rd 1004th 1005th 1006th 1007th 1008th 1009th 1010th 1011th 1012th 1013th 1014th 1015th 1016th 1017th 1018th 1019th 1020th 1021st 1022nd 1023rd 1024th 1025th</pre> |
||
=={{header| |
=={{header|Clojure}}== |
||
<syntaxhighlight lang="clojure"> |
|||
{{trans|Ruby}} |
|||
<lang csharp> |
|||
class Program |
|||
{ |
|||
private static string Ordinalize(int i) |
|||
{ |
|||
i = Math.Abs(i); |
|||
if (new[] {11, 12, 13}.Contains(i%100)) |
|||
return i + "th"; |
|||
switch (i%10) |
|||
{ |
|||
case 1: |
|||
return i + "st"; |
|||
case 2: |
|||
return i + "nd"; |
|||
case 3: |
|||
return i + "rd"; |
|||
default: |
|||
return i + "th"; |
|||
} |
|||
} |
|||
static void Main() |
|||
{ |
|||
Console.WriteLine(string.Join(" ", Enumerable.Range(0, 26).Select(Ordinalize))); |
|||
Console.WriteLine(string.Join(" ", Enumerable.Range(250, 16).Select(Ordinalize))); |
|||
Console.WriteLine(string.Join(" ", Enumerable.Range(1000, 26).Select(Ordinalize))); |
|||
} |
|||
} |
|||
</lang> |
|||
{{Out}} |
|||
<pre>0th 1st 2nd 3rd 4th 5th 6th 7th 8th 9th 10th 11th 12th 13th 14th 15th 16th 17th 18th 19th 20th 21st 22nd 23rd 24th 25th |
|||
250th 251st 252nd 253rd 254th 255th 256th 257th 258th 259th 260th 261st 262nd 263rd 264th 265th |
|||
1000th 1001st 1002nd 1003rd 1004th 1005th 1006th 1007th 1008th 1009th 1010th 1011th 1012th 1013th 1014th 1015th 1016th 1017th 1018th 1019th 1020th 1021st 1022nd 1023rd 1024th 1025th</pre> |
|||
=={{Header|Clojure}}== |
|||
<lang clojure> |
|||
(defn n-th [n] |
(defn n-th [n] |
||
(str n |
(str n |
||
Line 478: | Line 1,965: | ||
(apply str (interpose " " (map n-th (range 250 266)))) |
(apply str (interpose " " (map n-th (range 250 266)))) |
||
(apply str (interpose " " (map n-th (range 1000 1026)))) |
(apply str (interpose " " (map n-th (range 1000 1026)))) |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 489: | Line 1,976: | ||
Alternatively, if you want to print the full ordinal English, it becomes trivial with pprint: |
Alternatively, if you want to print the full ordinal English, it becomes trivial with pprint: |
||
< |
<syntaxhighlight lang="clojure"> |
||
(apply str (interpose " " (map #(clojure.pprint/cl-format nil "~:R" %) (range 0 26)))) |
(apply str (interpose " " (map #(clojure.pprint/cl-format nil "~:R" %) (range 0 26)))) |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
"zeroth first second third fourth fifth sixth seventh eighth ninth tenth eleventh twelfth thirteenth fourteenth fifteenth sixteenth seventeenth eighteenth nineteenth twentieth twenty-first twenty-second twenty-third twenty-fourth twenty-fifth" |
"zeroth first second third fourth fifth sixth seventh eighth ninth tenth eleventh twelfth thirteenth fourteenth fifteenth sixteenth seventeenth eighteenth nineteenth twentieth twenty-first twenty-second twenty-third twenty-fourth twenty-fifth" |
||
</pre> |
</pre> |
||
=={{header|CLU}}== |
|||
<syntaxhighlight lang="clu">nth = proc (n: int) returns (string) |
|||
num: string := int$unparse(n) |
|||
sfx: array[string] := array[string]$[0: "th", "st", "nd", "rd"] |
|||
if n / 10 // 10 = 1 cor n // 10 > 3 then |
|||
return(num || sfx[0]) |
|||
else |
|||
return(num || sfx[n // 10]) |
|||
end |
|||
end nth |
|||
do_range = proc (from, to: int) |
|||
po: stream := stream$primary_output() |
|||
col: int := 0 |
|||
for i: int in int$from_to(from,to) do |
|||
stream$putleft(po, nth(i), 7) |
|||
col := col + 1 |
|||
if col = 10 then |
|||
stream$putc(po, '\n') |
|||
col := 0 |
|||
end |
|||
end |
|||
stream$putl(po, "\n") |
|||
end do_range |
|||
start_up = proc () |
|||
do_range(0,25) |
|||
do_range(250,265) |
|||
do_range(1000,1025) |
|||
end start_up</syntaxhighlight> |
|||
{{out}} |
|||
<pre>0th 1st 2nd 3rd 4th 5th 6th 7th 8th 9th |
|||
10th 11th 12th 13th 14th 15th 16th 17th 18th 19th |
|||
20th 21st 22nd 23rd 24th 25th |
|||
250th 251st 252nd 253rd 254th 255th 256th 257th 258th 259th |
|||
260th 261st 262nd 263rd 264th 265th |
|||
1000th 1001st 1002nd 1003rd 1004th 1005th 1006th 1007th 1008th 1009th |
|||
1010th 1011th 1012th 1013th 1014th 1015th 1016th 1017th 1018th 1019th |
|||
1020th 1021st 1022nd 1023rd 1024th 1025th</pre> |
|||
=={{header|COBOL}}== |
|||
COBOL stores numbers in decimal form, so there is no need to use a modulo function: the last digit or the last two digits can be extracted directly. |
|||
<syntaxhighlight lang="cobol">IDENTIFICATION DIVISION. |
|||
PROGRAM-ID. NTH-PROGRAM. |
|||
DATA DIVISION. |
|||
WORKING-STORAGE SECTION. |
|||
01 WS-NUMBER. |
|||
05 N PIC 9(8). |
|||
05 LAST-TWO-DIGITS PIC 99. |
|||
05 LAST-DIGIT PIC 9. |
|||
05 N-TO-OUTPUT PIC Z(7)9. |
|||
05 SUFFIX PIC AA. |
|||
PROCEDURE DIVISION. |
|||
TEST-PARAGRAPH. |
|||
PERFORM NTH-PARAGRAPH VARYING N FROM 0 BY 1 UNTIL N IS GREATER THAN 25. |
|||
PERFORM NTH-PARAGRAPH VARYING N FROM 250 BY 1 UNTIL N IS GREATER THAN 265. |
|||
PERFORM NTH-PARAGRAPH VARYING N FROM 1000 BY 1 UNTIL N IS GREATER THAN 1025. |
|||
STOP RUN. |
|||
NTH-PARAGRAPH. |
|||
MOVE 'TH' TO SUFFIX. |
|||
MOVE N (7:2) TO LAST-TWO-DIGITS. |
|||
IF LAST-TWO-DIGITS IS LESS THAN 4, |
|||
OR LAST-TWO-DIGITS IS GREATER THAN 20, |
|||
THEN PERFORM DECISION-PARAGRAPH. |
|||
MOVE N TO N-TO-OUTPUT. |
|||
DISPLAY N-TO-OUTPUT WITH NO ADVANCING. |
|||
DISPLAY SUFFIX WITH NO ADVANCING. |
|||
DISPLAY SPACE WITH NO ADVANCING. |
|||
DECISION-PARAGRAPH. |
|||
MOVE N (8:1) TO LAST-DIGIT. |
|||
IF LAST-DIGIT IS EQUAL TO 1 THEN MOVE 'ST' TO SUFFIX. |
|||
IF LAST-DIGIT IS EQUAL TO 2 THEN MOVE 'ND' TO SUFFIX. |
|||
IF LAST-DIGIT IS EQUAL TO 3 THEN MOVE 'RD' TO SUFFIX.</syntaxhighlight> |
|||
Output: |
|||
<pre> 0TH 1ST 2ND 3RD 4TH 5TH 6TH 7TH 8TH 9TH 10TH 11TH 12TH 13TH 14TH 15TH 16TH 17TH 18TH 19TH 20TH 21ST 22ND 23RD 24TH 25TH 250TH 251ST 252ND 253RD 254TH 255TH 256TH 257TH 258TH 259TH 260TH 261ST 262ND 263RD 264TH 265TH 1000TH 1001ST 1002ND 1003RD 1004TH 1005TH 1006TH 1007TH 1008TH 1009TH 1010TH 1011TH 1012TH 1013TH 1014TH 1015TH 1016TH 1017TH 1018TH 1019TH 1020TH 1021ST 1022ND 1023RD 1024TH 1025TH</pre> |
|||
=={{header|Common Lisp}}== |
=={{header|Common Lisp}}== |
||
< |
<syntaxhighlight lang="lisp">(defun add-suffix (number) |
||
(let* ((suffixes #10("th" "st" "nd" "rd" "th")) |
(let* ((suffixes #10("th" "st" "nd" "rd" "th")) |
||
(last2 (mod number 100)) |
(last2 (mod number 100)) |
||
Line 506: | Line 2,073: | ||
(svref suffixes last-digit)))) |
(svref suffixes last-digit)))) |
||
(format nil "~a~a" number suffix))) |
(format nil "~a~a" number suffix))) |
||
</syntaxhighlight> |
|||
A more concise, albeit less readable version: |
|||
(defun add-suffix (n) |
|||
<syntaxhighlight lang="lisp">(defun add-suffix (n) |
|||
"A more concise, albeit less readable version" |
|||
(format nil "~d'~:[~[th~;st~;nd~;rd~:;th~]~;th~]" n (< (mod (- n 10) 100) 10) (mod n 10))) |
(format nil "~d'~:[~[th~;st~;nd~;rd~:;th~]~;th~]" n (< (mod (- n 10) 100) 10) (mod n 10))) |
||
</syntaxhighlight> |
|||
Display the results: |
|||
(loop for (low high) in '((0 25) (250 265) (1000 1025)) |
|||
<syntaxhighlight lang="lisp">(loop for (low high) in '((0 25) (250 265) (1000 1025)) |
|||
do (progn |
do (progn |
||
(format t "~a to ~a: " low high) |
(format t "~a to ~a: " low high) |
||
(loop for n from low to high |
(loop for n from low to high |
||
do (format t "~a " (add-suffix n)) |
do (format t "~a " (add-suffix n)) |
||
finally (terpri))))</ |
finally (terpri))))</syntaxhighlight> |
||
{{Out}} |
{{Out}} |
||
Line 525: | Line 2,095: | ||
1000 to 1025: 1000th 1001st 1002nd 1003rd 1004th 1005th 1006th 1007th 1008th 1009th 1010th 1011th 1012th 1013th 1014th 1015th 1016th 1017th 1018th 1019th 1020th 1021st 1022nd 1023rd 1024th 1025th</pre> |
1000 to 1025: 1000th 1001st 1002nd 1003rd 1004th 1005th 1006th 1007th 1008th 1009th 1010th 1011th 1012th 1013th 1014th 1015th 1016th 1017th 1018th 1019th 1020th 1021st 1022nd 1023rd 1024th 1025th</pre> |
||
=={{ |
=={{header|Cowgol}}== |
||
<syntaxhighlight lang="cowgol">include "cowgol.coh"; |
|||
include "strings.coh"; |
|||
# Given an int32, return decimal string and add an ordinal suffix |
|||
sub ordsfx(n: int32, buf: [uint8]): (out: [uint8]) is |
|||
var sfx: [uint8][] := {"th", "st", "nd", "rd"}; |
|||
out := buf; |
|||
buf := IToA(n, 10, buf); # IToA is included in standard library |
|||
# Since we now already have the digits, we can make the decision |
|||
# without doing any more work. |
|||
if (n > 10 and [@prev @prev buf] == '1') |
|||
or ([@prev buf] > '3') then |
|||
CopyString(sfx[0], buf); |
|||
else |
|||
CopyString(sfx[[@prev buf]-'0'], buf); |
|||
end if; |
|||
end sub; |
|||
# Print suffixed numerals from start to end inclusive |
|||
sub test(start: int32, end_: int32) is |
|||
var buf: uint8[16]; # buffer |
|||
var n: uint8 := 10; |
|||
while start <= end_ loop |
|||
print(ordsfx(start, &buf[0])); |
|||
print_char(' '); |
|||
start := start + 1; |
|||
n := n - 1; |
|||
if n == 0 then |
|||
print_nl(); |
|||
n := 10; |
|||
end if; |
|||
end loop; |
|||
print_nl(); |
|||
end sub; |
|||
test(0, 25); |
|||
test(250,265); |
|||
test(1000,1025);</syntaxhighlight> |
|||
{{out}} |
|||
<pre>0th 1st 2nd 3rd 4th 5th 6th 7th 8th 9th |
|||
10th 11th 12th 13th 14th 15th 16th 17th 18th 19th |
|||
20th 21st 22nd 23rd 24th 25th |
|||
250th 251st 252nd 253rd 254th 255th 256th 257th 258th 259th |
|||
260th 261st 262nd 263rd 264th 265th |
|||
1000th 1001st 1002nd 1003rd 1004th 1005th 1006th 1007th 1008th 1009th |
|||
1010th 1011th 1012th 1013th 1014th 1015th 1016th 1017th 1018th 1019th |
|||
1020th 1021st 1022nd 1023rd 1024th 1025th</pre> |
|||
=={{header|Crystal}}== |
|||
{{trans|Ruby}} |
|||
<syntaxhighlight lang="ruby">struct Int |
|||
def ordinalize |
|||
num = self.abs |
|||
ordinal = if (11..13).includes?(num % 100) |
|||
"th" |
|||
else |
|||
case num % 10 |
|||
when 1; "st" |
|||
when 2; "nd" |
|||
when 3; "rd" |
|||
else "th" |
|||
end |
|||
end |
|||
"#{self}#{ordinal}" |
|||
end |
|||
end |
|||
[(0..25),(250..265),(1000..1025)].each{|r| puts r.map{ |n| n.ordinalize }.join(", "); puts} |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
0th, 1st, 2nd, 3rd, 4th, 5th, 6th, 7th, 8th, 9th, 10th, 11th, 12th, 13th, 14th, 15th, 16th, 17th, 18th, 19th, 20th, 21st, 22nd, 23rd, 24th, 25th |
|||
250th, 251st, 252nd, 253rd, 254th, 255th, 256th, 257th, 258th, 259th, 260th, 261st, 262nd, 263rd, 264th, 265th |
|||
1000th, 1001st, 1002nd, 1003rd, 1004th, 1005th, 1006th, 1007th, 1008th, 1009th, 1010th, 1011th, 1012th, 1013th, 1014th, 1015th, 1016th, 1017th, 1018th, 1019th, 1020th, 1021st, 1022nd, 1023rd, 1024th, 1025th |
|||
</pre> |
|||
=={{header|D}}== |
|||
{{trans|Python}} |
{{trans|Python}} |
||
< |
<syntaxhighlight lang="d">import std.stdio, std.string, std.range, std.algorithm; |
||
string nth(in uint n) pure { |
string nth(in uint n) pure { |
||
Line 538: | Line 2,191: | ||
foreach (r; [iota(26), iota(250, 266), iota(1000, 1026)]) |
foreach (r; [iota(26), iota(250, 266), iota(1000, 1026)]) |
||
writefln("%-(%s %)", r.map!nth); |
writefln("%-(%s %)", r.map!nth); |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>0'th 1'st 2'nd 3'rd 4'th 5'th 6'th 7'th 8'th 9'th 10'th 11'th 12'th 13'th 14'th 15'th 16'th 17'th 18'th 19'th 20'th 21'st 22'nd 23'rd 24'th 25'th |
<pre>0'th 1'st 2'nd 3'rd 4'th 5'th 6'th 7'th 8'th 9'th 10'th 11'th 12'th 13'th 14'th 15'th 16'th 17'th 18'th 19'th 20'th 21'st 22'nd 23'rd 24'th 25'th |
||
250'th 251'st 252'nd 253'rd 254'th 255'th 256'th 257'th 258'th 259'th 260'th 261'st 262'nd 263'rd 264'th 265'th |
250'th 251'st 252'nd 253'rd 254'th 255'th 256'th 257'th 258'th 259'th 260'th 261'st 262'nd 263'rd 264'th 265'th |
||
1000'th 1001'st 1002'nd 1003'rd 1004'th 1005'th 1006'th 1007'th 1008'th 1009'th 1010'th 1011'th 1012'th 1013'th 1014'th 1015'th 1016'th 1017'th 1018'th 1019'th 1020'th 1021'st 1022'nd 1023'rd 1024'th 1025'th</pre> |
1000'th 1001'st 1002'nd 1003'rd 1004'th 1005'th 1006'th 1007'th 1008'th 1009'th 1010'th 1011'th 1012'th 1013'th 1014'th 1015'th 1016'th 1017'th 1018'th 1019'th 1020'th 1021'st 1022'nd 1023'rd 1024'th 1025'th</pre> |
||
=={{header|Delphi}}== |
|||
See [https://www.rosettacode.org/wiki/N%27th#Pascal Pascal]. |
|||
=={{header|Draco}}== |
|||
<syntaxhighlight lang="draco">proc nonrec nth(word n; *char buf) *char: |
|||
channel output text ch; |
|||
open(ch, buf); |
|||
write(ch; n, |
|||
if (n/10)%10=1 then "th" |
|||
elif n%10=1 then "st" |
|||
elif n%10=2 then "nd" |
|||
elif n%10=3 then "rd" |
|||
else "th" |
|||
fi |
|||
); |
|||
close(ch); |
|||
buf |
|||
corp; |
|||
proc nonrec print_range(word start, stop) void: |
|||
[8] char buf; |
|||
word col, n; |
|||
col := 0; |
|||
for n from start upto stop do |
|||
write(nth(n, &buf[0])); |
|||
col := col + 1; |
|||
if col%10=0 then writeln() else write('\t') fi |
|||
od; |
|||
writeln() |
|||
corp |
|||
proc nonrec main() void: |
|||
print_range(0, 25); |
|||
print_range(250, 265); |
|||
print_range(1000, 1025) |
|||
corp</syntaxhighlight> |
|||
{{out}} |
|||
<pre>0th 1st 2nd 3rd 4th 5th 6th 7th 8th 9th |
|||
10th 11th 12th 13th 14th 15th 16th 17th 18th 19th |
|||
20th 21st 22nd 23rd 24th 25th |
|||
250th 251st 252nd 253rd 254th 255th 256th 257th 258th 259th |
|||
260th 261st 262nd 263rd 264th 265th |
|||
1000th 1001st 1002nd 1003rd 1004th 1005th 1006th 1007th 1008th 1009th |
|||
1010th 1011th 1012th 1013th 1014th 1015th 1016th 1017th 1018th 1019th |
|||
1020th 1021st 1022nd 1023rd 1024th 1025th</pre> |
|||
=={{header|EasyLang}}== |
|||
<syntaxhighlight lang="easylang"> |
|||
func$ nth num . |
|||
last2 = num mod 100 |
|||
last = num mod 10 |
|||
if last2 >= 11 and last2 <= 13 |
|||
return num & "th" |
|||
elif last = 1 |
|||
return num & "st" |
|||
elif last = 2 |
|||
return num & "nd" |
|||
elif last = 3 |
|||
return num & "rd" |
|||
else |
|||
return num & "th" |
|||
. |
|||
. |
|||
print "0 to 25:" |
|||
for i = 0 to 25 |
|||
write nth i & " " |
|||
. |
|||
print "" |
|||
print "250 to 265:" |
|||
for i = 250 to 265 |
|||
write nth i & " " |
|||
. |
|||
print "" |
|||
print "1000 to 1025:" |
|||
for i = 1000 to 1025 |
|||
write nth i & " " |
|||
. |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
0 to 25: |
|||
0th 1st 2nd 3rd 4th 5th 6th 7th 8th 9th 10th 11th 12th 13th 14th 15th 16th 17th 18th 19th 20th 21st 22nd 23rd 24th 25th |
|||
250 to 265: |
|||
250th 251st 252nd 253rd 254th 255th 256th 257th 258th 259th 260th 261st 262nd 263rd 264th 265th |
|||
1000 to 1025: |
|||
1000th 1001st 1002nd 1003rd 1004th 1005th 1006th 1007th 1008th 1009th 1010th 1011th 1012th 1013th 1014th 1015th 1016th 1017th 1018th 1019th 1020th 1021st 1022nd 1023rd 1024th 1025th |
|||
</pre> |
|||
=={{header|ed}}== |
|||
<syntaxhighlight lang="sed"> |
|||
H |
|||
# Special case: 10-19 |
|||
g/1[0-9]$/s//&'th/ |
|||
g/1$/s//&'st/ |
|||
g/2$/s//&'nd/ |
|||
g/3$/s//&'rd/ |
|||
g/[0-9]$/s//&'th/ |
|||
# Join by spaces, purely for aesthetics. |
|||
g/.*/s//& / |
|||
,j |
|||
,p |
|||
Q |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
<pre>$ ed -s nth.input < nth.ed |
|||
Newline appended |
|||
0'th 1'st 2'nd 3'rd 4'th 5'th 6'th 7'th 8'th 9'th 10'th 11'th 12'th 13'th 14'th 15'th 16'th 17'th 18'th 19'th 20'th 21'st 22'nd 23'rd 24'th 25'th 250'th 251'st 252'nd 253'rd 254'th 255'th 256'th 257'th 258'th 259'th 260'th 261'st 262'nd 263'rd 264'th 265'th 1000'th 1001'st 1002'nd 1003'rd 1004'th 1005'th 1006'th 1007'th 1008'th 1009'th 1010'th 1011'th 1012'th 1013'th 1014'th 1015'th 1016'th 1017'th 1018'th 1019'th 1020'th 1021'st 1022'nd 1023'rd 1024'th 1025'th </pre> |
|||
=={{header|Elena}}== |
|||
{{trans|C#}} |
|||
ELENA 6.x : |
|||
<syntaxhighlight lang="elena">import extensions; |
|||
import system'math; |
|||
import system'routines; |
|||
extension op |
|||
{ |
|||
ordinalize() |
|||
{ |
|||
int i := self.Absolute; |
|||
if (new int[]{11,12,13}.ifExists(i.mod(100))) |
|||
{ |
|||
^ i.toPrintable() + "th" |
|||
}; |
|||
(i.mod(10)) => |
|||
1 { ^ i.toPrintable() + "st" } |
|||
2 { ^ i.toPrintable() + "nd" } |
|||
3 { ^ i.toPrintable() + "rd" }; |
|||
^ i.toPrintable() + "th" |
|||
} |
|||
} |
|||
public program() |
|||
{ |
|||
console.printLine(new Range(0,26).selectBy(mssgconst ordinalize<op>[1])); |
|||
console.printLine(new Range(250,26).selectBy(mssgconst ordinalize<op>[1])); |
|||
console.printLine(new Range(1000,26).selectBy(mssgconst ordinalize<op>[1])) |
|||
}</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
0th,1st,2nd,3rd,4th,5th,6th,7th,8th,9th,10th,11th,12th,13th,14th,15th,16th,17th,18th,19th,20th,21st,22nd,23rd,24th,25th |
|||
250th,251st,252nd,253rd,254th,255th,256th,257th,258th,259th,260th,261st,262nd,263rd,264th,265th,266th,267th,268th,269th,270th,271st,272nd,273rd,274th,275th |
|||
1000th,1001st,1002nd,1003rd,1004th,1005th,1006th,1007th,1008th,1009th,1010th,1011th,1012th,1013th,1014th,1015th,1016th,1017th,1018th,1019th,1020th,1021st,1022nd,1023rd,1024th,1025th |
|||
</pre> |
|||
=={{header|Elixir}}== |
=={{header|Elixir}}== |
||
< |
<syntaxhighlight lang="elixir">defmodule RC do |
||
def ordinalize(n) do |
def ordinalize(n) do |
||
num = abs(n) |
num = abs(n) |
||
Line 564: | Line 2,367: | ||
Enum.each([0..25, 250..265, 1000..1025], fn range -> |
Enum.each([0..25, 250..265, 1000..1025], fn range -> |
||
Enum.map(range, fn n -> RC.ordinalize(n) end) |> Enum.join(" ") |> IO.puts |
Enum.map(range, fn n -> RC.ordinalize(n) end) |> Enum.join(" ") |> IO.puts |
||
end)</ |
end)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 574: | Line 2,377: | ||
=={{header|ERRE}}== |
=={{header|ERRE}}== |
||
<syntaxhighlight lang="erre"> |
|||
<lang ERRE> |
|||
PROGRAM NTH_SOLVE |
PROGRAM NTH_SOLVE |
||
Line 598: | Line 2,401: | ||
NTH(1000,1025) |
NTH(1000,1025) |
||
END PROGRAM |
END PROGRAM |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 609: | Line 2,412: | ||
1020th 1021st 1022nd 1023rd 1024th 1025th |
1020th 1021st 1022nd 1023rd 1024th 1025th |
||
</pre> |
</pre> |
||
=={{header|F_Sharp|F#}}== |
|||
<syntaxhighlight lang="fsharp">open System |
|||
let ordinalsuffix n = |
|||
let suffixstrings = [|"th"; "st"; "nd"; "rd"|] |
|||
let (d, r) = Math.DivRem(n, 10) |
|||
n.ToString() + suffixstrings.[ if r < 4 && (d &&& 1) = 0 then r else 0 ] |
|||
[<EntryPoint>] |
|||
let main argv = |
|||
let show = (Seq.iter (ordinalsuffix >> (printf " %s"))) >> (Console.WriteLine) |
|||
[0..25] |> show |
|||
[250..265] |> show |
|||
[1000..1025] |> show |
|||
0 |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
<pre> 0th 1st 2nd 3rd 4th 5th 6th 7th 8th 9th 10th 11th 12th 13th 14th 15th 16th 17th 18th 19th 20th 21st 22nd 23rd 24th 25th |
|||
250th 251th 252th 253th 254th 255th 256th 257th 258th 259th 260th 261st 262nd 263rd 264th 265th |
|||
1000th 1001st 1002nd 1003rd 1004th 1005th 1006th 1007th 1008th 1009th 1010th 1011th 1012th 1013th 1014th 1015th 1016th 1017th 1018th 1019th 1020th 1021st 1022nd 1023rd 1024th 1025th</pre> |
|||
=={{header|Factor}}== |
|||
<syntaxhighlight lang="factor">USING: io kernel math math.order math.parser math.ranges qw |
|||
sequences ; |
|||
IN: rosetta-code.nth |
|||
: n'th ( n -- str ) |
|||
dup 10 /mod swap 1 = [ drop 0 ] when |
|||
[ number>string ] |
|||
[ 4 min qw{ th st nd rd th } nth ] bi* append ; |
|||
: n'th-demo ( -- ) |
|||
0 25 250 265 1000 1025 [ [a,b] ] 2tri@ |
|||
[ [ n'th write bl ] each nl ] tri@ ; |
|||
MAIN: n'th-demo</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
0th 1st 2nd 3rd 4th 5th 6th 7th 8th 9th 10th 11th 12th 13th 14th 15th 16th 17th 18th 19th 20th 21st 22nd 23rd 24th 25th |
|||
250th 251st 252nd 253rd 254th 255th 256th 257th 258th 259th 260th 261st 262nd 263rd 264th 265th |
|||
1000th 1001st 1002nd 1003rd 1004th 1005th 1006th 1007th 1008th 1009th 1010th 1011th 1012th 1013th 1014th 1015th 1016th 1017th 1018th 1019th 1020th 1021st 1022nd 1023rd 1024th 1025th |
|||
</pre> |
|||
=={{header|Fennel}}== |
|||
{{trans|Lua}} |
|||
<syntaxhighlight lang="fennel"> |
|||
(fn get-suffix [n] |
|||
(let [last-two (% n 100) |
|||
last-one (% n 10)] |
|||
(if (and (> last-two 3) (< last-two 21)) :th |
|||
(= last-one 1) :st |
|||
(= last-one 2) :nd |
|||
(= last-one 3) :rd |
|||
:th))) |
|||
(fn nth [n] |
|||
(.. n "'" (get-suffix n))) |
|||
(for [i 0 25] |
|||
(print (nth i) (nth (+ i 250)) (nth (+ i 1000)))) |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
Same as [[#Lua|Lua]]. |
|||
=={{header|Forth}}== |
=={{header|Forth}}== |
||
< |
<syntaxhighlight lang="forth">: 'nth ( -- c-addr ) s" th st nd rd th th th th th th " drop ; |
||
: .nth ( n -- ) |
: .nth ( n -- ) |
||
dup 10 20 within if 0 .r ." th " exit then |
dup 100 mod 10 20 within if 0 .r ." th " exit then |
||
dup 0 .r 10 mod 3 * 'nth + 3 type ; |
dup 0 .r 10 mod 3 * 'nth + 3 type ; |
||
Line 620: | Line 2,490: | ||
26 0 test 266 250 test 1026 1000 test ; |
26 0 test 266 250 test 1026 1000 test ; |
||
tests</ |
tests</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 637: | Line 2,507: | ||
1000th 1001st 1002nd 1003rd 1004th |
1000th 1001st 1002nd 1003rd 1004th |
||
1005th 1006th 1007th 1008th 1009th |
1005th 1006th 1007th 1008th 1009th |
||
1010th |
1010th 1011th 1012th 1013th 1014th |
||
1015th 1016th 1017th 1018th 1019th |
1015th 1016th 1017th 1018th 1019th |
||
1020th 1021st 1022nd 1023rd 1024th |
1020th 1021st 1022nd 1023rd 1024th |
||
1022th ok</pre> |
|||
=={{header|Fortran}}== |
=={{header|Fortran}}== |
||
{{output?}} |
|||
THE INSTRUCTIONS! |
|||
Write a function/method/subroutine/... that when given an integer greater than or equal to zero returns a string of the number followed by an apostrophe then the ordinal suffix. |
|||
Example returns would include 1'st 2'nd 3'rd 11'th 111'th 1001'st 1012'th |
|||
Please find the compilation instructions and examples in comments at the start of the source. |
Please find the compilation instructions and examples in comments at the start of the source. |
||
< |
<syntaxhighlight lang="fortran">!-*- mode: compilation; default-directory: "/tmp/" -*- |
||
!Compilation started at Fri Jun 6 15:40:18 |
!Compilation started at Fri Jun 6 15:40:18 |
||
! |
! |
||
Line 721: | Line 2,597: | ||
if ((10 .le. tens) .and. (tens .lt. 20)) then |
if ((10 .le. tens) .and. (tens .lt. 20)) then |
||
ordinate = a // "'" // ateen(ones_index:ones_index+1) |
ordinate = a // "'" // ateen(ones_index:ones_index+1) |
||
! ^^^^^^ remove these characters to remove the important ' |
|||
else |
else |
||
ordinate = a // "'" // a09(ones_index:ones_index+1) |
ordinate = a // "'" // a09(ones_index:ones_index+1) |
||
! ^^^^^^ remove these characters to remove the important ' |
|||
end if |
end if |
||
else |
else |
||
Line 733: | Line 2,611: | ||
end function ordinate |
end function ordinate |
||
end program nth</ |
end program nth</syntaxhighlight> |
||
=={{header|Go}}== |
=={{header|Go}}== |
||
{{trans| |
{{trans|Raku}} |
||
< |
<syntaxhighlight lang="go">package main |
||
import "fmt" |
import "fmt" |
||
Line 773: | Line 2,651: | ||
} |
} |
||
fmt.Println() |
fmt.Println() |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 782: | Line 2,660: | ||
=={{header|Haskell}}== |
=={{header|Haskell}}== |
||
< |
<syntaxhighlight lang="haskell">import Data.Array |
||
ordSuffs :: Array Integer String |
ordSuffs :: Array Integer String |
||
Line 800: | Line 2,678: | ||
printOrdSuffs [ 0.. 25] |
printOrdSuffs [ 0.. 25] |
||
printOrdSuffs [ 250.. 265] |
printOrdSuffs [ 250.. 265] |
||
printOrdSuffs [1000..1025]</ |
printOrdSuffs [1000..1025]</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 811: | Line 2,689: | ||
The following works in both languages. |
The following works in both languages. |
||
< |
<syntaxhighlight lang="unicon">procedure main(A) |
||
every writes(" ",nth(0 to 25) | "\n") |
every writes(" ",nth(0 to 25) | "\n") |
||
every writes(" ",nth(250 to 265) | "\n") |
every writes(" ",nth(250 to 265) | "\n") |
||
Line 821: | Line 2,699: | ||
(n%10 = 2, n%100 ~= 12, "nd") | |
(n%10 = 2, n%100 ~= 12, "nd") | |
||
(n%10 = 3, n%100 ~= 13, "rd") | "th") |
(n%10 = 3, n%100 ~= 13, "rd") | "th") |
||
end</ |
end</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 830: | Line 2,708: | ||
1000th 1001st 1002nd 1003rd 1004th 1005th 1006th 1007th 1008th 1009th 1010th 1011th 1012th 1013h 1014th 1015th 1016th 1017th 1018th 1019th 1020th 1021st 1022nd 1023rd 1024th 1025th |
1000th 1001st 1002nd 1003rd 1004th 1005th 1006th 1007th 1008th 1009th 1010th 1011th 1012th 1013h 1014th 1015th 1016th 1017th 1018th 1019th 1020th 1021st 1022nd 1023rd 1024th 1025th |
||
-> |
-> |
||
</pre> |
|||
=={{Header|Insitux}}== |
|||
<syntaxhighlight lang="insitux"> |
|||
(function ordinal n |
|||
(if ([11 12 13] (rem n 100)) |
|||
(str n "th") |
|||
(str n (or ((rem n 10) ["th" "st" "nd" "rd"]) "th")))) |
|||
(function line x y |
|||
(-> (range x y) |
|||
(map (comp ordinal (pad-left " " 6))) |
|||
(join " "))) |
|||
(.. str (line 0 13) "\n" (line 1000 1013) |
|||
"\n\n" (line 13 26) "\n" (line 1013 1026) |
|||
"\n\n" (line 250 266)) |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
0th 1st 2nd 3rd 4th 5th 6th 7th 8th 9th 10th 11th 12th |
|||
1000th 1001st 1002nd 1003rd 1004th 1005th 1006th 1007th 1008th 1009th 1010th 1011th 1012th |
|||
13th 14th 15th 16th 17th 18th 19th 20th 21st 22nd 23rd 24th 25th |
|||
1013th 1014th 1015th 1016th 1017th 1018th 1019th 1020th 1021st 1022nd 1023rd 1024th 1025th |
|||
250th 251st 252nd 253rd 254th 255th 256th 257th 258th 259th 260th 261st 262nd 263rd 264th 265th |
|||
</pre> |
</pre> |
||
Line 836: | Line 2,744: | ||
Implementation: |
Implementation: |
||
< |
<syntaxhighlight lang="j">suf=: (;:'th st nd rd th') {::~ 4 <. 10 10 (* 1&~:)~/@#: ] |
||
nth=: [: ;:inv (": , suf)each</ |
nth=: [: ;:inv (": , suf)each</syntaxhighlight> |
||
Task: |
Task: |
||
< |
<syntaxhighlight lang="j"> thru=: <./ + i.@(+ *)@-~ |
||
nth 0 thru 25 |
nth 0 thru 25 |
||
0th 1st 2nd 3rd 4th 5th 6th 7th 8th 9th 10th 11th 12th 13th 14th 15th 16th 17th 18th 19th 20th 21st 22nd 23rd 24th 25th |
0th 1st 2nd 3rd 4th 5th 6th 7th 8th 9th 10th 11th 12th 13th 14th 15th 16th 17th 18th 19th 20th 21st 22nd 23rd 24th 25th |
||
Line 847: | Line 2,755: | ||
250th 251st 252nd 253rd 254th 255th 256th 257th 258th 259th 260th 261st 262nd 263rd 264th 265th |
250th 251st 252nd 253rd 254th 255th 256th 257th 258th 259th 260th 261st 262nd 263rd 264th 265th |
||
nth 1000 thru 1025 |
nth 1000 thru 1025 |
||
1000th 1001st 1002nd 1003rd 1004th 1005th 1006th 1007th 1008th 1009th 1010th 1011th 1012th 1013th 1014th 1015th 1016th 1017th 1018th 1019th 1020th 1021st 1022nd 1023rd 1024th 1025th</ |
1000th 1001st 1002nd 1003rd 1004th 1005th 1006th 1007th 1008th 1009th 1010th 1011th 1012th 1013th 1014th 1015th 1016th 1017th 1018th 1019th 1020th 1021st 1022nd 1023rd 1024th 1025th</syntaxhighlight> |
||
=={{header|Java}}== |
=={{header|Java}}== |
||
< |
<syntaxhighlight lang="java">public class Nth { |
||
public static String ordinalAbbrev(int n){ |
public static String ordinalAbbrev(int n){ |
||
String ans = "th"; //most of the time it should be "th" |
String ans = "th"; //most of the time it should be "th" |
||
Line 875: | Line 2,783: | ||
} |
} |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{works with|Java|8+}} |
{{works with|Java|8+}} |
||
< |
<syntaxhighlight lang="java">package nth; |
||
import java.util.stream.IntStream; |
import java.util.stream.IntStream; |
||
Line 924: | Line 2,832: | ||
; |
; |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>0th 1st 2nd 3rd 4th 5th 6th 7th 8th 9th 10th 11th 12th 13th 14th 15th 16th 17th 18th 19th 20th 21st 22nd 23rd 24th 25th |
<pre>0th 1st 2nd 3rd 4th 5th 6th 7th 8th 9th 10th 11th 12th 13th 14th 15th 16th 17th 18th 19th 20th 21st 22nd 23rd 24th 25th |
||
250th 251st 252nd 253rd 254th 255th 256th 257th 258th 259th 260th 261st 262nd 263rd 264th 265th |
250th 251st 252nd 253rd 254th 255th 256th 257th 258th 259th 260th 261st 262nd 263rd 264th 265th |
||
1000th 1001st 1002nd 1003rd 1004th 1005th 1006th 1007th 1008th 1009th 1010th 1011th 1012th 1013th 1014th 1015th 1016th 1017th 1018th 1019th 1020th 1021st 1022nd 1023rd 1024th 1025th </pre> |
1000th 1001st 1002nd 1003rd 1004th 1005th 1006th 1007th 1008th 1009th 1010th 1011th 1012th 1013th 1014th 1015th 1016th 1017th 1018th 1019th 1020th 1021st 1022nd 1023rd 1024th 1025th </pre> |
||
=={{header|JavaScript}}== |
=={{header|JavaScript}}== |
||
===ES5 |
===ES5=== |
||
< |
<syntaxhighlight lang="javascript">console.log(function () { |
||
var lstSuffix = 'th st nd rd th th th th th th'.split(' '), |
var lstSuffix = 'th st nd rd th th th th th th'.split(' '), |
||
Line 958: | Line 2,865: | ||
}).join('\n\n'); |
}).join('\n\n'); |
||
}());</ |
}());</syntaxhighlight> |
||
Output: |
|||
{{Out}} |
|||
<lang JavaScript>0th 1st 2nd 3rd 4th 5th 6th 7th 8th 9th 10th 11th 12th 13th 14th 15th 16th 17th 18th 19th 20th 21st 22nd 23rd 24th 25th |
|||
<syntaxhighlight lang="javascript">0th 1st 2nd 3rd 4th 5th 6th 7th 8th 9th 10th 11th 12th 13th 14th 15th 16th 17th 18th 19th 20th 21st 22nd 23rd 24th 25th |
|||
250th 251st 252nd 253rd 254th 255th 256th 257th 258th 259th 260th 261st 262nd 263rd 264th 265th |
250th 251st 252nd 253rd 254th 255th 256th 257th 258th 259th 260th 261st 262nd 263rd 264th 265th |
||
1000th 1001st 1002nd 1003rd 1004th 1005th 1006th 1007th 1008th 1009th 1010th 1011th 1012th 1013th 1014th 1015th 1016th 1017th 1018th 1019th 1020th 1021st 1022nd 1023rd 1024th 1025th</ |
1000th 1001st 1002nd 1003rd 1004th 1005th 1006th 1007th 1008th 1009th 1010th 1011th 1012th 1013th 1014th 1015th 1016th 1017th 1018th 1019th 1020th 1021st 1022nd 1023rd 1024th 1025th</syntaxhighlight> |
||
===ES6=== |
|||
<syntaxhighlight lang="javascript">(function (lstTestRanges) { |
|||
'use strict' |
|||
let lstSuffix = 'th st nd rd th th th th th th'.split(' '), |
|||
// ordinalString :: Int -> String |
|||
ordinalString = n => |
|||
n.toString() + ( |
|||
11 <= n % 100 && 13 >= n % 100 ? |
|||
"th" : lstSuffix[n % 10] |
|||
), |
|||
// range :: Int -> Int -> [Int] |
|||
range = (m, n) => |
|||
Array.from({ |
|||
length: (n - m) + 1 |
|||
}, (_, i) => m + i); |
|||
return lstTestRanges |
|||
.map(tpl => range |
|||
.apply(null, tpl) |
|||
.map(ordinalString) |
|||
); |
|||
})([[0, 25], [250, 265], [1000, 1025]]);</syntaxhighlight> |
|||
{{Out}} |
|||
<pre>[["0th", "1st", "2nd", "3rd", "4th", "5th", "6th", "7th", "8th", |
|||
"9th", "10th", "11th", "12th", "13th", "14th", "15th", "16th", |
|||
"17th", "18th", "19th", "20th", "21st", "22nd", "23rd", "24th", "25th"], |
|||
["250th", "251st", "252nd", "253rd", "254th", "255th", "256th", "257th", |
|||
"258th", "259th", "260th", "261st", "262nd", "263rd", "264th", "265th"], |
|||
["1000th", "1001st", "1002nd", "1003rd", "1004th", "1005th", "1006th", |
|||
"1007th", "1008th", "1009th", "1010th", "1011th", "1012th", "1013th", |
|||
"1014th", "1015th", "1016th", "1017th", "1018th", "1019th", "1020th", |
|||
"1021st", "1022nd", "1023rd", "1024th", "1025th"]]</pre> |
|||
=={{header|Joy}}== |
|||
<syntaxhighlight lang="joy">DEFINE |
|||
suffix == abs 89 + 100 rem 9 - 10 rem [{1 2 3} in] [] [pop 0] ifte ["th" "st" "nd" "rd"] of; |
|||
nth == ['d 1 1 format] [suffix] cleave concat. |
|||
[0 250 1000] [25 [dup nth putchars ' putch succ] times nth putchars '\n putch] step.</syntaxhighlight> |
|||
{{out}} |
|||
<pre>0th 1st 2nd 3rd 4th 5th 6th 7th 8th 9th 10th 11th 12th 13th 14th 15th 16th 17th 18th 19th 20th 21st 22nd 23rd 24th 25th |
|||
250th 251st 252nd 253rd 254th 255th 256th 257th 258th 259th 260th 261st 262nd 263rd 264th 265th 266th 267th 268th 269th 270th 271st 272nd 273rd 274th 275th |
|||
1000th 1001st 1002nd 1003rd 1004th 1005th 1006th 1007th 1008th 1009th 1010th 1011th 1012th 1013th 1014th 1015th 1016th 1017th 1018th 1019th 1020th 1021st 1022nd 1023rd 1024th 1025th</pre> |
|||
=={{header|jq}}== |
=={{header|jq}}== |
||
<syntaxhighlight lang="jq"> |
|||
<lang jq> |
|||
# ordinalize an integer input, positive or negative |
# ordinalize an integer input, positive or negative |
||
def ordinalize: |
def ordinalize: |
||
Line 987: | Line 2,948: | ||
([range(-5; -1)], [range(0;26)], [range(250;266)], [range(1000;1026)]) |
([range(-5; -1)], [range(0;26)], [range(250;266)], [range(1000;1026)]) |
||
| map(ordinalize) |
| map(ordinalize) |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 997: | Line 2,958: | ||
=={{header|Julia}}== |
=={{header|Julia}}== |
||
<syntaxhighlight lang="julia">using Printf</syntaxhighlight> |
|||
'''Function''' |
|||
'''Function''': |
|||
<lang Julia> |
|||
<syntaxhighlight lang="julia">function ordinal(n::Integer) |
|||
function sprintfordinal{T<:Integer}(n::T) |
|||
n < 0 && throw(DomainError()) |
|||
const sfixes = ["st", "nd", "rd"] |
|||
suffixes = ("st", "nd", "rd") |
|||
0 <= n || throw(ArgumentError("number to be formatted must be ≥ 0, got $n")) |
|||
u = n%10 |
u = n % 10 |
||
t = |
t = n ÷ 10 % 10 |
||
if |
if u > 3 || u == 0 || t == 1 |
||
suf = "th" |
|||
else |
else |
||
suf = suffixes[u] |
|||
end |
end |
||
return string(n, suf) |
|||
end</syntaxhighlight> |
|||
end |
|||
</lang> |
|||
'''Main''' |
'''Main''': |
||
<syntaxhighlight lang="julia">println("Tests of ordinal formatting of integers.") |
|||
<lang Julia> |
|||
println("Tests of ordinal formatting of integers.") |
|||
for (i, n) in enumerate(0:25) |
for (i, n) in enumerate(0:25) |
||
(i - 1) % 10 == 0 && println() |
|||
@printf("%7s", ordinal(n)) |
|||
end |
|||
print(@sprintf("%7s", sprintfordinal(n))) |
|||
end |
end |
||
println() |
|||
println() |
|||
for (i, n) in enumerate(250:265) |
for (i, n) in enumerate(250:265) |
||
(i - 1) % 10 == 0 && println() |
|||
@printf("%7s", ordinal(n)) |
|||
end |
|||
print(@sprintf("%7s", sprintfordinal(n))) |
|||
end |
end |
||
println() |
|||
println() |
|||
for (i, n) in enumerate(1000:1025) |
for (i, n) in enumerate(1000:1025) |
||
(i - 1) % 10 == 0 && println() |
|||
@printf("%7s", ordinal(n)) |
|||
end</syntaxhighlight> |
|||
end |
|||
'''Main2''': |
|||
print(@sprintf("%7s", sprintfordinal(n))) |
|||
<syntaxhighlight lang="julia">Nth(x::Integer) = if x % 100 ∈ [11, 12, 13] "th" else ["th", "st", "nd", "rd", "th"][min(x % 10 + 1, 5)] end |
|||
NthA(x::Integer) = "$(x)'$(Nth(x)) " |
|||
[0:25..., 250:265..., 1000:1025...] .|> NthA .|> print;</syntaxhighlight> |
|||
{{out}} |
|||
<pre>Tests of ordinal formatting of integers. |
|||
0th 1st 2nd 3rd 4th 5th 6th 7th 8th 9th |
|||
10th 11th 12th 13th 14th 15th 16th 17th 18th 19th |
|||
20th 21st 22nd 23rd 24th 25th |
|||
250th 251st 252nd 253rd 254th 255th 256th 257th 258th 259th |
|||
260th 261st 262nd 263rd 264th 265th |
|||
1000th 1001st 1002nd 1003rd 1004th 1005th 1006th 1007th 1008th 1009th |
|||
1010th 1011th 1012th 1013th 1014th 1015th 1016th 1017th 1018th 1019th |
|||
1020th 1021st 1022nd 1023rd 1024th 1025th</pre> |
|||
=={{header|Kotlin}}== |
|||
<syntaxhighlight lang="scala">fun Int.ordinalAbbrev() = |
|||
if (this % 100 / 10 == 1) "th" |
|||
else when (this % 10) { 1 -> "st" 2 -> "nd" 3 -> "rd" else -> "th" } |
|||
fun IntRange.ordinalAbbrev() = map { "$it" + it.ordinalAbbrev() }.joinToString(" ") |
|||
fun main(args: Array<String>) { |
|||
listOf((0..25), (250..265), (1000..1025)).forEach { println(it.ordinalAbbrev()) } |
|||
}</syntaxhighlight> |
|||
=={{header|Lambdatalk}}== |
|||
Translation from the javascript entry |
|||
<syntaxhighlight lang="scheme"> |
|||
{def fnOrdinalForm |
|||
{lambda {:n} |
|||
{if {and {<= 11 {% :n 100}} {>= 13 {% :n 100}}} |
|||
then :nth |
|||
else :n{A.get {% :n 10} th st nd rd th th th th th th}}}} |
|||
-> fnOrdinalForm |
|||
{S.map fnOrdinalForm {S.serie 0 25}} |
|||
-> 0th 1st 2nd 3rd 4th 5th 6th 7th 8th 9th 10th 11th 12th 13th 14th 15th |
|||
16th 17th 18th 19th 20th 21st 22nd 23rd 24th 25th |
|||
{S.map fnOrdinalForm {S.serie 250 265}} |
|||
-> 250th 251st 252nd 253rd 254th 255th 256th 257th 258th 259th 260th |
|||
261st 262nd 263rd 264th 265th |
|||
{S.map fnOrdinalForm {S.serie 1000 1025}} |
|||
-> 1000th 1001st 1002nd 1003rd 1004th 1005th 1006th 1007th 1008th 1009th 1010th |
|||
1011th 1012th 1013th 1014th 1015th 1016th 1017th 1018th 1019th 1020th |
|||
1021st 1022nd 1023rd 1024th 1025th |
|||
</syntaxhighlight> |
|||
=={{header|Lua}}== |
|||
The apostrophe just looks weird if you ask me. No one did, obviously. |
|||
<syntaxhighlight lang="lua">function getSuffix (n) |
|||
local lastTwo, lastOne = n % 100, n % 10 |
|||
if lastTwo > 3 and lastTwo < 21 then return "th" end |
|||
if lastOne == 1 then return "st" end |
|||
if lastOne == 2 then return "nd" end |
|||
if lastOne == 3 then return "rd" end |
|||
return "th" |
|||
end |
end |
||
println() |
|||
function Nth (n) return n .. "'" .. getSuffix(n) end |
|||
</lang> |
|||
for i = 0, 25 do print(Nth(i), Nth(i + 250), Nth(i + 1000)) end</syntaxhighlight> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
0'th 250'th 1000'th |
|||
Tests of ordinal formatting of integers. |
|||
1'st 251'st 1001'st |
|||
2'nd 252'nd 1002'nd |
|||
3'rd 253'rd 1003'rd |
|||
4'th 254'th 1004'th |
|||
5'th 255'th 1005'th |
|||
6'th 256'th 1006'th |
|||
7'th 257'th 1007'th |
|||
8'th 258'th 1008'th |
|||
9'th 259'th 1009'th |
|||
10'th 260'th 1010'th |
|||
11'th 261'st 1011'th |
|||
12'th 262'nd 1012'th |
|||
13'th 263'rd 1013'th |
|||
14'th 264'th 1014'th |
|||
15'th 265'th 1015'th |
|||
16'th 266'th 1016'th |
|||
17'th 267'th 1017'th |
|||
18'th 268'th 1018'th |
|||
19'th 269'th 1019'th |
|||
20'th 270'th 1020'th |
|||
21'st 271'st 1021'st |
|||
22'nd 272'nd 1022'nd |
|||
23'rd 273'rd 1023'rd |
|||
24'th 274'th 1024'th |
|||
25'th 275'th 1025'th |
|||
</pre> |
|||
=={{header|Maple}}== |
|||
0th 1st 2nd 3rd 4th 5th 6th 7th 8th 9th |
|||
<syntaxhighlight lang="maple">toOrdinal := proc(n:: nonnegint) |
|||
10th 11th 12th 13th 14th 15th 16th 17th 18th 19th |
|||
if 1 <= n and n <= 10 then |
|||
20th 21st 22nd 23rd 24th 25th |
|||
if n >= 4 then |
|||
printf("%ath", n); |
|||
elif n = 3 then |
|||
printf("%ard", n); |
|||
elif n = 2 then |
|||
printf("%and", n); |
|||
else |
|||
printf("%ast", n); |
|||
end if: |
|||
else |
|||
printf(convert(n, 'ordinal')); |
|||
end if: |
|||
return NULL; |
|||
end proc: |
|||
a := [[0, 25], [250, 265], [1000, 1025]]: |
|||
for i in a do |
|||
for j from i[1] to i[2] do |
|||
toOrdinal(j); |
|||
printf(" "); |
|||
end do; |
|||
printf("\n\n"); |
|||
end do;</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
0th 1st 2nd 3rd 4th 5th 6th 7th 8th 9th 10th 11th 12th 13th 14th 15th 16th 17th 18th 19th 20th 21st 22nd 23rd 24th 25th |
|||
250th 251st 252nd 253rd 254th 255th 256th 257th 258th 259th 260th 261st 262nd 263rd 264th 265th |
|||
260th 261st 262nd 263rd 264th 265th |
|||
1000th 1001st 1002nd 1003rd 1004th 1005th 1006th 1007th 1008th 1009th 1010th 1011th 1012th 1013th 1014th 1015th 1016th 1017th 1018th 1019th 1020th 1021st 1022nd 1023rd 1024th 1025th |
|||
1010th 1011th 1012th 1013th 1014th 1015th 1016th 1017th 1018th 1019th |
|||
1020th 1021st 1022nd 1023rd 1024th 1025th |
|||
</pre> |
</pre> |
||
=={{header|Mathematica}}== |
=={{header|Mathematica}}/{{header|Wolfram Language}}== |
||
I borrowed the logic from the Python code. |
I borrowed the logic from the Python code. |
||
< |
<syntaxhighlight lang="mathematica">suffixlist = {"th", "st", "nd", "rd", "th", "th", "th", "th", "th","th"}; |
||
addsuffix[n_] := Module[{suffix}, |
addsuffix[n_] := Module[{suffix}, |
||
suffix = Which[ |
suffix = Which[ |
||
Line 1,070: | Line 3,138: | ||
addsuffix[#] & /@ Range[0, 25] (* test 1 *) |
addsuffix[#] & /@ Range[0, 25] (* test 1 *) |
||
addsuffix[#] & /@ Range[250, 265] (* test 2 *) |
addsuffix[#] & /@ Range[250, 265] (* test 2 *) |
||
addsuffix[#] & /@ Range[1000, 1025] (* test 3 *) |
addsuffix[#] & /@ Range[1000, 1025] (* test 3 *)</syntaxhighlight> |
||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre>{"0th", "1st", "2nd", "3rd", "4th", "5th", "6th", "7th", "8th", "9th", "10th", "11th", "12th", "13th", "14th", "15th", "16th", "17th", "18th", "19th", "20th", "21st", "22nd", "23rd", "24th", "25th"} |
<pre>{"0th", "1st", "2nd", "3rd", "4th", "5th", "6th", "7th", "8th", "9th", "10th", "11th", "12th", "13th", "14th", "15th", "16th", "17th", "18th", "19th", "20th", "21st", "22nd", "23rd", "24th", "25th"} |
||
Line 1,079: | Line 3,146: | ||
{"1000th", "1001st", "1002nd", "1003rd", "1004th", "1005th", "1006th", "1007th", "1008th", "1009th", "1010th", "1011th", "1012th", "1013th", "1014th", "1015th", "1016th", "1017th", "1018th", "1019th", "1020th", "1021st", "1022nd", "1023rd", "1024th", "1025th"}</pre> |
{"1000th", "1001st", "1002nd", "1003rd", "1004th", "1005th", "1006th", "1007th", "1008th", "1009th", "1010th", "1011th", "1012th", "1013th", "1014th", "1015th", "1016th", "1017th", "1018th", "1019th", "1020th", "1021st", "1022nd", "1023rd", "1024th", "1025th"}</pre> |
||
=={{ |
=={{header|MATLAB}}== |
||
< |
<syntaxhighlight lang="matlab">function s = nth(n) |
||
tens = mod(n, 100); |
tens = mod(n, 100); |
||
if tens > 9 && tens < 20 |
if tens > 9 && tens < 20 |
||
Line 1,097: | Line 3,164: | ||
end |
end |
||
s = sprintf('%d%s', n, suf); |
s = sprintf('%d%s', n, suf); |
||
end</ |
end</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>0th 1st 2nd 3rd 4th 5th 6th 7th 8th 9th 10th 11th 12th 13th 14th 15th 16th 17th 18th 19th 20th 21st 22nd 23rd 24th 25th |
<pre>0th 1st 2nd 3rd 4th 5th 6th 7th 8th 9th 10th 11th 12th 13th 14th 15th 16th 17th 18th 19th 20th 21st 22nd 23rd 24th 25th |
||
Line 1,104: | Line 3,171: | ||
</pre> |
</pre> |
||
=={{ |
=={{header|MiniScript}}== |
||
To get the output all on one line, we append it to a list as we go, and then print the list all at once at the end. |
|||
<syntaxhighlight lang="miniscript"> |
|||
ordinal = function(n) |
|||
if n % 100 > 3 and n %100 < 20 then return n + "th" |
|||
if n % 10 == 1 then return n + "st" |
|||
if n % 10 == 2 then return n + "nd" |
|||
if n % 10 == 3 then return n + "rd" |
|||
return n + "th" |
|||
end function |
|||
test = function(from, to) |
|||
out = [] |
|||
for i in range(from, to) |
|||
out.push ordinal(i) |
|||
end for |
|||
print out.join |
|||
end function |
|||
test 0, 25 |
|||
test 250, 265 |
|||
test 1000, 1025 |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
0th 1st 2nd 3rd 4th 5th 6th 7th 8th 9th 10th 11th 12th 13th 14th 15th 16th 17th 18th 19th 20th 21st 22nd 23rd 24th 25th |
|||
250th 251st 252nd 253rd 254th 255th 256th 257th 258th 259th 260th 261st 262nd 263rd 264th 265th |
|||
1000th 1001st 1002nd 1003rd 1004th 1005th 1006th 1007th 1008th 1009th 1010th 1011th 1012th 1013th 1014th 1015th 1016th 1017th 1018th 1019th 1020th 1021st 1022nd 1023rd 1024th 1025th |
|||
</pre> |
|||
=={{header|Modula-2}}== |
|||
{{trans|Ada}} |
|||
{{works with|ADW Modula-2|any (Compile with the linker option ''Console Application'').}} |
|||
<syntaxhighlight lang="modula2"> |
|||
MODULE Nth; |
|||
FROM STextIO IMPORT |
|||
WriteString, WriteLn; |
|||
FROM WholeStr IMPORT |
|||
IntToStr; |
|||
PROCEDURE Suffix(N: CARDINAL; VAR OUT Destination: ARRAY OF CHAR); |
|||
VAR |
|||
NMod10, NMod100: CARDINAL; |
|||
BEGIN |
|||
NMod10 := N MOD 10; |
|||
NMod100 := N MOD 100; |
|||
IF (NMod10 = 1) AND (NMod100 <> 11) THEN |
|||
Destination := "st"; |
|||
ELSIF (NMod10 = 2) AND (NMod100 <> 12) THEN |
|||
Destination := "nd"; |
|||
ELSIF (NMod10 = 3) AND (NMod100 <> 13) THEN |
|||
Destination := "rd"; |
|||
ELSE |
|||
Destination := "th"; |
|||
END; |
|||
END Suffix; |
|||
PROCEDURE PrintImages(LoLim, HiLim: CARDINAL); |
|||
VAR |
|||
I: CARDINAL; |
|||
IString: ARRAY [0 .. 15] OF CHAR; |
|||
ISuff: ARRAY [0 .. 1] OF CHAR; |
|||
BEGIN |
|||
FOR I := LoLim TO HiLim DO |
|||
IntToStr(I, IString); |
|||
Suffix(I, ISuff); |
|||
WriteString(IString); |
|||
WriteString(ISuff); |
|||
WriteString(" "); |
|||
END; |
|||
WriteLn; |
|||
END PrintImages; |
|||
BEGIN |
|||
PrintImages( 0, 25); |
|||
PrintImages( 250, 265); |
|||
PrintImages(1000, 1025); |
|||
END Nth. |
|||
</syntaxhighlight> |
|||
=={{header|Nanoquery}}== |
|||
{{trans|Java}} |
|||
<syntaxhighlight lang="nanoquery">def ordinalAbbrev(n) |
|||
if int(n % 100 / 10) = 1 |
|||
return "th" |
|||
end |
|||
if n % 10 = 1 |
|||
return "st" |
|||
else if n % 10 = 2 |
|||
return "nd" |
|||
else if n % 10 = 3 |
|||
return "rd" |
|||
else |
|||
return "th" |
|||
end |
|||
end |
|||
for i in range(0, 25) |
|||
print (i + "'" + ordinalAbbrev(i) + " ") |
|||
end |
|||
println |
|||
for i in range(250, 265) |
|||
print (i + "'" + ordinalAbbrev(i) + " ") |
|||
end |
|||
println |
|||
for i in range(1000, 1025) |
|||
print (i + "'" + ordinalAbbrev(i) + " ") |
|||
end |
|||
println</syntaxhighlight> |
|||
{{out}} |
|||
<pre>0'th 1'st 2'nd 3'rd 4'th 5'th 6'th 7'th 8'th 9'th 10'th 11'th 12'th 13'th 14'th 15'th 16'th 17'th 18'th 19'th 20'th 21'st 22'nd 23'rd 24'th 25'th |
|||
250'th 251'st 252'nd 253'rd 254'th 255'th 256'th 257'th 258'th 259'th 260'th 261'st 262'nd 263'rd 264'th 265'th |
|||
1000'th 1001'st 1002'nd 1003'rd 1004'th 1005'th 1006'th 1007'th 1008'th 1009'th 1010'th 1011'th 1012'th 1013'th 1014'th 1015'th 1016'th 1017'th 1018'th 1019'th 1020'th 1021'st 1022'nd 1023'rd 1024'th 1025'th</pre> |
|||
=={{header|Nim}}== |
|||
{{trans|Python}} |
{{trans|Python}} |
||
< |
<syntaxhighlight lang="nim">const Suffix = ["th", "st", "nd", "rd", "th", "th", "th", "th", "th", "th"] |
||
proc nth(n): string = |
proc nth(n: Natural): string = |
||
$n & "'" & (if n mod 100 |
$n & "'" & (if n mod 100 in 11..20: "th" else: Suffix[n mod 10]) |
||
for j in countup(0, 1000, 250): |
for j in countup(0, 1000, 250): |
||
for i in j..j+24: |
for i in j..j+24: |
||
stdout.write nth(i), " " |
stdout.write nth(i), " " |
||
echo ""</ |
echo ""</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>0'th 1'st 2'nd 3'rd 4'th 5'th 6'th 7'th 8'th 9'th 10'th 11'th 12'th 13'th 14'th 15'th 16'th 17'th 18'th 19'th 20'th 21'st 22'nd 23'rd 24'th |
<pre>0'th 1'st 2'nd 3'rd 4'th 5'th 6'th 7'th 8'th 9'th 10'th 11'th 12'th 13'th 14'th 15'th 16'th 17'th 18'th 19'th 20'th 21'st 22'nd 23'rd 24'th |
||
Line 1,121: | Line 3,308: | ||
750'th 751'st 752'nd 753'rd 754'th 755'th 756'th 757'th 758'th 759'th 760'th 761'st 762'nd 763'rd 764'th 765'th 766'th 767'th 768'th 769'th 770'th 771'st 772'nd 773'rd 774'th |
750'th 751'st 752'nd 753'rd 754'th 755'th 756'th 757'th 758'th 759'th 760'th 761'st 762'nd 763'rd 764'th 765'th 766'th 767'th 768'th 769'th 770'th 771'st 772'nd 773'rd 774'th |
||
1000'th 1001'st 1002'nd 1003'rd 1004'th 1005'th 1006'th 1007'th 1008'th 1009'th 1010'th 1011'th 1012'th 1013'th 1014'th 1015'th 1016'th 1017'th 1018'th 1019'th 1020'th 1021'st 1022'nd 1023'rd 1024'th</pre> |
1000'th 1001'st 1002'nd 1003'rd 1004'th 1005'th 1006'th 1007'th 1008'th 1009'th 1010'th 1011'th 1012'th 1013'th 1014'th 1015'th 1016'th 1017'th 1018'th 1019'th 1020'th 1021'st 1022'nd 1023'rd 1024'th</pre> |
||
=={{header|Objeck}}== |
|||
{{trans|Java}} |
|||
<syntaxhighlight lang="objeck">class Nth { |
|||
function : OrdinalAbbrev(n : Int ) ~ String { |
|||
ans := "th"; # most of the time it should be "th" |
|||
if(n % 100 / 10 = 1) { |
|||
return ans; # teens are all "th" |
|||
}; |
|||
select(n % 10){ |
|||
label 1: { ans := "st"; } |
|||
label 2: { ans := "nd"; } |
|||
label 3: { ans := "rd"; } |
|||
}; |
|||
return ans; |
|||
} |
|||
function : Main(args : String[]) ~ Nil { |
|||
for(i := 0; i <= 25; i+=1;) { |
|||
abbr := OrdinalAbbrev(i); |
|||
"{$i}{$abbr} "->Print(); |
|||
}; |
|||
""->PrintLine(); |
|||
for(i := 250; i <= 265; i+=1;) { |
|||
abbr := OrdinalAbbrev(i); |
|||
"{$i}{$abbr} "->Print(); |
|||
}; |
|||
""->PrintLine(); |
|||
for(i := 1000; i <= 1025; i+=1;) { |
|||
abbr := OrdinalAbbrev(i); |
|||
"{$i}{$abbr} "->Print(); |
|||
}; |
|||
} |
|||
}</syntaxhighlight> |
|||
{{output}} |
|||
<pre> |
|||
0th 1st 2nd 3rd 4th 5th 6th 7th 8th 9th 10th 11th 12th 13th 14th 15th 16th 17th 18th 19th 20th 21st 22nd 23rd 24th 25th |
|||
250th 251st 252nd 253rd 254th 255th 256th 257th 258th 259th 260th 261st 262nd 263rd 264th 265th |
|||
1000th 1001st 1002nd 1003rd 1004th 1005th 1006th 1007th 1008th 1009th 1010th 1011th 1012th 1013th 1014th 1015th 1016th 1017th 1018th 1019th 1020th 1021st 1022nd 1023rd 1024th 1025th |
|||
</pre> |
|||
=={{header|OCaml}}== |
|||
<syntaxhighlight lang="ocaml"> |
|||
let show_nth n = |
|||
if (n mod 10 = 1) && (n mod 100 <> 11) then "st" |
|||
else if (n mod 10 = 2) && (n mod 100 <> 12) then "nd" |
|||
else if (n mod 10 = 3) && (n mod 100 <> 13) then "rd" |
|||
else "th" |
|||
let () = |
|||
let show_ordinals (min, max) = |
|||
for i=min to max do |
|||
Printf.printf "%d%s " i (show_nth i) |
|||
done; |
|||
print_newline() in |
|||
List.iter show_ordinals [ (0,25); (250,265); (1000,1025) ] |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
0th 1st 2nd 3rd 4th 5th 6th 7th 8th 9th 10th 11th 12th 13th 14th 15th 16th 17th 18th 19th 20th 21st 22nd 23rd 24th 25th |
|||
250th 251st 252nd 253rd 254th 255th 256th 257th 258th 259th 260th 261st 262nd 263rd 264th 265th |
|||
1000th 1001st 1002nd 1003rd 1004th 1005th 1006th 1007th 1008th 1009th 1010th 1011th 1012th 1013th 1014th 1015th 1016th 1017th 1018th 1019th 1020th 1021st 1022nd 1023rd 1024th 1025th |
|||
</pre> |
|||
=={{header|Oforth}}== |
=={{header|Oforth}}== |
||
< |
<syntaxhighlight lang="oforth">: nth(n) |
||
{ |
|||
| r | |
| r | |
||
n "th" over 10 mod ->r |
n "th" over 10 mod ->r |
||
Line 1,131: | Line 3,389: | ||
r 2 == ifTrue: [ n 100 mod 12 == ifFalse: [ drop "nd" ] ] |
r 2 == ifTrue: [ n 100 mod 12 == ifFalse: [ drop "nd" ] ] |
||
r 3 == ifTrue: [ n 100 mod 13 == ifFalse: [ drop "rd" ] ] |
r 3 == ifTrue: [ n 100 mod 13 == ifFalse: [ drop "rd" ] ] |
||
+ ;</syntaxhighlight> |
|||
+ |
|||
}</lang> |
|||
{{out}} |
{{out}} |
||
Line 1,150: | Line 3,407: | ||
</pre> |
</pre> |
||
=={{ |
=={{header|PARI/GP}}== |
||
(Spurious apostrophes intentionally omitted, following |
(Spurious apostrophes intentionally omitted, following Raku.) |
||
< |
<syntaxhighlight lang="parigp">ordinal(n)=my(k=n%10,m=n%100); Str(n,if(m<21&&m>3,"th",k==1,"st",k==2,"nd",k==3,"rd","th")); |
||
apply(ordinal, [0..25]) |
apply(ordinal, [0..25]) |
||
apply(ordinal, [250..265]) |
apply(ordinal, [250..265]) |
||
apply(ordinal, [1000..1025]) |
apply(ordinal, [1000..1025]) |
||
apply(ordinal, [111, 1012])</ |
apply(ordinal, [111, 1012])</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>%1 = ["0th", "1st", "2nd", "3rd", "4th", "5th", "6th", "7th", "8th", "9th", "10th", "11th", "12th", "13th", "14th", "15th", "16th", "17th", "18th", "19th", "20th", "21st", "22nd", "23rd", "24th", "25th"] |
<pre>%1 = ["0th", "1st", "2nd", "3rd", "4th", "5th", "6th", "7th", "8th", "9th", "10th", "11th", "12th", "13th", "14th", "15th", "16th", "17th", "18th", "19th", "20th", "21st", "22nd", "23rd", "24th", "25th"] |
||
Line 1,164: | Line 3,421: | ||
%4 = ["111th", "1012th"]</pre> |
%4 = ["111th", "1012th"]</pre> |
||
=={{ |
=={{header|Pascal}}== |
||
nearly copy of [[N'th#Ada|Ada]] |
|||
{{Trans|Perl 6}} |
|||
<syntaxhighlight lang="pascal">Program n_th; |
|||
function Suffix(N: NativeInt):AnsiString; |
|||
var |
|||
res: AnsiString; |
|||
begin |
|||
res:= 'th'; |
|||
case N mod 10 of |
|||
1:IF N mod 100 <> 11 then |
|||
res:= 'st'; |
|||
2:IF N mod 100 <> 12 then |
|||
res:= 'nd'; |
|||
3:IF N mod 100 <> 13 then |
|||
res:= 'rd'; |
|||
else |
|||
end; |
|||
Suffix := res; |
|||
end; |
|||
procedure Print_Images(loLim, HiLim: NativeInt); |
|||
var |
|||
i : NativeUint; |
|||
begin |
|||
for I := LoLim to HiLim do |
|||
write(i,Suffix(i),' '); |
|||
writeln; |
|||
end; |
|||
begin |
|||
Print_Images( 0, 25); |
|||
Print_Images( 250, 265); |
|||
Print_Images(1000, 1025); |
|||
end.</syntaxhighlight> |
|||
{{Out}} shortened |
|||
<pre> |
|||
0th 1st 2nd 3rd 4th ... 11th 12th 13th ..20th 21st 22nd 23rd 24th 25th |
|||
250th 251st 252nd 253rd 254th ..261st 262nd 263rd 264th 265th |
|||
..1001st 1002nd 1003rd 1004th..1011th..1013th..1021st 1022nd 1023rd 1024th |
|||
</pre> |
|||
=={{header|PascalABC.NET}}== |
|||
<syntaxhighlight lang="delphi"> |
|||
var suffix := |'th', 'st', 'nd', 'rd'| + |'th'| * 6; |
|||
function Nth(n: integer) |
|||
:= $'{n}''' + if n mod 100 not in 11..19 then suffix[n mod 10] else 'th'; |
|||
begin |
|||
((0..24) + (500..524) + (700..724) + (1000..1024)) |
|||
.Select(Nth).Println; |
|||
end. |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
0'th 1'st 2'nd 3'rd 4'th 5'th 6'th 7'th 8'th 9'th 10'th 11'th 12'th 13'th 14'th 15'th 16'th 17'th 18'th 19'th 20'th 21'st 22'nd 23'rd 24'th 500'th 501'st 502'nd 503'rd 504'th 505'th 506'th 507'th 508'th 509'th 510'th 511'th 512'th 513'th 514'th 515'th 516'th 517'th 518'th 519'th 520'th 521'st 522'nd 523'rd 524'th 700'th 701'st 702'nd 703'rd 704'th 705'th 706'th 707'th 708'th 709'th 710'th 711'th 712'th 713'th 714'th 715'th 716'th 717'th 718'th 719'th 720'th 721'st 722'nd 723'rd 724'th 1000'th 1001'st 1002'nd 1003'rd 1004'th 1005'th 1006'th 1007'th 1008'th 1009'th 1010'th 1011'th 1012'th 1013'th 1014'th 1015'th 1016'th 1017'th 1018'th 1019'th 1020'th 1021'st 1022'nd 1023'rd 1024'th |
|||
</pre> |
|||
=={{header|Perl}}== |
|||
{{Trans|Raku}} |
|||
Requires Perl 5.10 or newer for the Defined OR operator (//). |
Requires Perl 5.10 or newer for the Defined OR operator (//). |
||
<syntaxhighlight lang="perl">use 5.10.0; |
|||
<lang perl>my %irregulars = ( 1 => 'st', |
|||
my %irregulars = ( 1 => 'st', |
|||
2 => 'nd', |
2 => 'nd', |
||
3 => 'rd', |
3 => 'rd', |
||
Line 1,181: | Line 3,498: | ||
sub range { join ' ', map { nth($_) } @{$_[0]} } |
sub range { join ' ', map { nth($_) } @{$_[0]} } |
||
print range($_), "\n" for ([0..25], [250..265], [1000..1025]); |
print range($_), "\n" for ([0..25], [250..265], [1000..1025]);</syntaxhighlight> |
||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre>0th 1st 2nd 3rd 4th 5th 6th 7th 8th 9th 10th 11th 12th 13th 14th 15th 16th 17th 18th 19th 20th 21st 22nd 23rd 24th 25th |
|||
Same as Perl 6 |
|||
250th 251st 252nd 253rd 254th 255th 256th 257th 258th 259th 260th 261st 262nd 263rd 264th 265th |
|||
1000th 1001st 1002nd 1003rd 1004th 1005th 1006th 1007th 1008th 1009th 1010th 1011th 1012th 1013th 1014th 1015th 1016th 1017th 1018th 1019th 1020th 1021st 1022nd 1023rd 1024th 1025th</pre> |
|||
Alternatively, can use a library. |
|||
{{libheader|Lingua::EN::Numbers::Ordinate}} |
{{libheader|Lingua::EN::Numbers::Ordinate}} |
||
< |
<syntaxhighlight lang="perl">use Lingua::EN::Numbers::Ordinate 'ordinate'; |
||
foreach my $i (0..25, 250..265, 1000..1025) { |
foreach my $i (0..25, 250..265, 1000..1025) { |
||
print ordinate($i),"\n"; |
print ordinate($i),"\n"; |
||
}</ |
}</syntaxhighlight> |
||
=={{Header|Perl 6}}== |
|||
(Spurious apostrophes intentionally omitted.) |
|||
<lang perl6>my %irregulars = <1 st 2 nd 3 rd>, (11..13 X=> 'th'); |
|||
sub nth ($n) { $n ~ ( %irregulars{$n % 100} // %irregulars{$n % 10} // 'th' ) } |
|||
=={{header|Phix}}== |
|||
say .list».&nth for [^26], [250..265], [1000..1025];</lang> |
|||
<!--<syntaxhighlight lang="phix">(phixonline)--> |
|||
<span style="color: #008080;">constant</span> <span style="color: #000000;">ordinals</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #008000;">"th"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"st"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"nd"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"rd"</span><span style="color: #0000FF;">}</span> |
|||
<span style="color: #008080;">function</span> <span style="color: #000000;">Nth</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">bool</span> <span style="color: #000000;">apostrophe</span><span style="color: #0000FF;">=</span><span style="color: #004600;">false</span><span style="color: #0000FF;">)</span> |
|||
<span style="color: #004080;">integer</span> <span style="color: #000000;">mod10</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">mod</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">,</span><span style="color: #000000;">10</span><span style="color: #0000FF;">)+</span><span style="color: #000000;">1</span> |
|||
<span style="color: #008080;">if</span> <span style="color: #000000;">mod10</span><span style="color: #0000FF;">></span><span style="color: #000000;">4</span> <span style="color: #008080;">or</span> <span style="color: #7060A8;">mod</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">,</span><span style="color: #000000;">100</span><span style="color: #0000FF;">)=</span><span style="color: #000000;">mod10</span><span style="color: #0000FF;">+</span><span style="color: #000000;">9</span> <span style="color: #008080;">then</span> <span style="color: #000000;">mod10</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">1</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span> |
|||
<span style="color: #008080;">return</span> <span style="color: #7060A8;">sprintf</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"%d%s"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">n</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #008000;"><nowiki>'\''</nowiki></span><span style="color: #0000FF;">,</span><span style="color: #000000;">apostrophe</span><span style="color: #0000FF;">)&</span><span style="color: #000000;">ordinals</span><span style="color: #0000FF;">[</span><span style="color: #000000;">mod10</span><span style="color: #0000FF;">]})</span> |
|||
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span> |
|||
<span style="color: #008080;">constant</span> <span style="color: #000000;">ranges</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{{</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">25</span><span style="color: #0000FF;">},{</span><span style="color: #000000;">250</span><span style="color: #0000FF;">,</span><span style="color: #000000;">265</span><span style="color: #0000FF;">},{</span><span style="color: #000000;">1000</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1025</span><span style="color: #0000FF;">}}</span> |
|||
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">ranges</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span> |
|||
<span style="color: #008080;">for</span> <span style="color: #000000;">j</span><span style="color: #0000FF;">=</span><span style="color: #000000;">ranges</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">][</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]</span> <span style="color: #008080;">to</span> <span style="color: #000000;">ranges</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">][</span><span style="color: #000000;">2</span><span style="color: #0000FF;">]</span> <span style="color: #008080;">do</span> |
|||
<span style="color: #008080;">if</span> <span style="color: #7060A8;">mod</span><span style="color: #0000FF;">(</span><span style="color: #000000;">j</span><span style="color: #0000FF;">,</span><span style="color: #000000;">10</span><span style="color: #0000FF;">)=</span><span style="color: #000000;">0</span> <span style="color: #008080;">then</span> <span style="color: #7060A8;">puts</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"\n"</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span> |
|||
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">" %6s"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">Nth</span><span style="color: #0000FF;">(</span><span style="color: #000000;">j</span><span style="color: #0000FF;">,</span><span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">2</span><span style="color: #0000FF;">)})</span> |
|||
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span> |
|||
<span style="color: #7060A8;">puts</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"\n"</span><span style="color: #0000FF;">)</span> |
|||
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span> |
|||
<!--</syntaxhighlight>--> |
|||
{{out}} |
{{out}} |
||
<pre> |
|||
<pre>0th 1st 2nd 3rd 4th 5th 6th 7th 8th 9th 10th 11th 12th 13th 14th 15th 16th 17th 18th 19th 20th 21st 22nd 23rd 24th 25th |
|||
0th 1st 2nd 3rd 4th 5th 6th 7th 8th 9th |
|||
250th 251st 252nd 253rd 254th 255th 256th 257th 258th 259th 260th 261st 262nd 263rd 264th 265th |
|||
10th 11th 12th 13th 14th 15th 16th 17th 18th 19th |
|||
1000th 1001st 1002nd 1003rd 1004th 1005th 1006th 1007th 1008th 1009th 1010th 1011th 1012th 1013th 1014th 1015th 1016th 1017th 1018th 1019th 1020th 1021st 1022nd 1023rd 1024th 1025th</pre> |
|||
20th 21st 22nd 23rd 24th 25th |
|||
If you want to get Unicodally fancy, use this version instead: |
|||
<lang perl6>my %irregulars = <1 ˢᵗ 2 ⁿᵈ 3 ʳᵈ>, (11..13 X=> 'ᵗʰ'); |
|||
250'th 251'st 252'nd 253'rd 254'th 255'th 256'th 257'th 258'th 259'th |
|||
sub nth ($n) { $n ~ ( %irregulars{$n % 100} // %irregulars{$n % 10} // 'ᵗʰ' ) } |
|||
260'th 261'st 262'nd 263'rd 264'th 265'th |
|||
1000th 1001st 1002nd 1003rd 1004th 1005th 1006th 1007th 1008th 1009th |
|||
say .list».&nth for [^26], [250..265], [1000..1025];</lang> |
|||
1010th 1011th 1012th 1013th 1014th 1015th 1016th 1017th 1018th 1019th |
|||
{{out}} |
|||
1020th 1021st 1022nd 1023rd 1024th 1025th |
|||
<blockquote>0ᵗʰ 1ˢᵗ 2ⁿᵈ 3ʳᵈ 4ᵗʰ 5ᵗʰ 6ᵗʰ 7ᵗʰ 8ᵗʰ 9ᵗʰ 10ᵗʰ 11ᵗʰ 12ᵗʰ 13ᵗʰ 14ᵗʰ 15ᵗʰ 16ᵗʰ 17ᵗʰ 18ᵗʰ 19ᵗʰ 20ᵗʰ 21ˢᵗ 22ⁿᵈ 23ʳᵈ 24ᵗʰ 25ᵗʰ<br> |
|||
</pre> |
|||
250ᵗʰ 251ˢᵗ 252ⁿᵈ 253ʳᵈ 254ᵗʰ 255ᵗʰ 256ᵗʰ 257ᵗʰ 258ᵗʰ 259ᵗʰ 260ᵗʰ 261ˢᵗ 262ⁿᵈ 263ʳᵈ 264ᵗʰ 265ᵗʰ<br> |
|||
Alternatively you can use the builtin ord(), and a more functional style, as follows (same output) |
|||
1000ᵗʰ 1001ˢᵗ 1002ⁿᵈ 1003ʳᵈ 1004ᵗʰ 1005ᵗʰ 1006ᵗʰ 1007ᵗʰ 1008ᵗʰ 1009ᵗʰ 1010ᵗʰ 1011ᵗʰ 1012ᵗʰ 1013ᵗʰ 1014ᵗʰ 1015ᵗʰ 1016ᵗʰ 1017ᵗʰ 1018ᵗʰ 1019ᵗʰ 1020ᵗʰ 1021ˢᵗ 1022ⁿᵈ 1023ʳᵈ 1024ᵗʰ 1025ᵗʰ</blockquote> |
|||
<!--<syntaxhighlight lang="phix">(phixonline)--> |
|||
<span style="color: #008080;">function</span> <span style="color: #000000;">do_one</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">string</span> <span style="color: #000000;">apostrophe</span><span style="color: #0000FF;">)</span> |
|||
<span style="color: #008080;">return</span> <span style="color: #7060A8;">pad_head</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">sprintf</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"%d%s%s"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">n</span><span style="color: #0000FF;">,</span><span style="color: #000000;">apostrophe</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">ord</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">)}),</span><span style="color: #000000;">7</span><span style="color: #0000FF;">)</span> |
|||
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span> |
|||
<span style="color: #008080;">procedure</span> <span style="color: #000000;">do_set</span><span style="color: #0000FF;">(</span><span style="color: #004080;">sequence</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">bool</span> <span style="color: #000000;">bApostrophe</span><span style="color: #0000FF;">=</span><span style="color: #004600;">false</span><span style="color: #0000FF;">)</span> |
|||
<span style="color: #7060A8;">puts</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">join_by</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">apply</span><span style="color: #0000FF;">(</span><span style="color: #004600;">true</span><span style="color: #0000FF;">,</span><span style="color: #000000;">do_one</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">s</span><span style="color: #0000FF;">,{</span><span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #008000;">'`'</span><span style="color: #0000FF;">,</span><span style="color: #000000;">bApostrophe</span><span style="color: #0000FF;">)}}),</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">10</span><span style="color: #0000FF;">,</span><span style="color: #008000;">""</span><span style="color: #0000FF;">)&</span><span style="color: #008000;">"\n"</span><span style="color: #0000FF;">)</span> |
|||
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span> |
|||
<span style="color: #008080;">constant</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">rs</span><span style="color: #0000FF;">,</span><span style="color: #000000;">re</span><span style="color: #0000FF;">}</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">columnize</span><span style="color: #0000FF;">({{</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">25</span><span style="color: #0000FF;">},{</span><span style="color: #000000;">250</span><span style="color: #0000FF;">,</span><span style="color: #000000;">265</span><span style="color: #0000FF;">},{</span><span style="color: #000000;">1000</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1025</span><span style="color: #0000FF;">}})</span> |
|||
<span style="color: #7060A8;">papply</span><span style="color: #0000FF;">(</span><span style="color: #004600;">true</span><span style="color: #0000FF;">,</span><span style="color: #000000;">do_set</span><span style="color: #0000FF;">,{</span><span style="color: #7060A8;">apply</span><span style="color: #0000FF;">(</span><span style="color: #004600;">true</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">tagset</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">re</span><span style="color: #0000FF;">,</span><span style="color: #000000;">rs</span><span style="color: #0000FF;">}),{</span><span style="color: #004600;">false</span><span style="color: #0000FF;">,</span><span style="color: #004600;">true</span><span style="color: #0000FF;">,</span><span style="color: #004600;">false</span><span style="color: #0000FF;">}})</span> |
|||
<!--</syntaxhighlight>--> |
|||
=={{ |
=={{header|PHP}}== |
||
< |
<syntaxhighlight lang="php">function nth($num) { |
||
$os = "th"; |
$os = "th"; |
||
if ($num % 100 <= 10 or $num % 100 > 20) { |
if ($num % 100 <= 10 or $num % 100 > 20) { |
||
Line 1,239: | Line 3,580: | ||
} |
} |
||
echo "\n"; |
echo "\n"; |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,246: | Line 3,587: | ||
1000th 1001st 1002nd 1003rd 1004th 1005th 1006th 1007th 1008th 1009th 1010th 1011th 1012th 1013th 1014th 1015th 1016th 1017th 1018th 1019th 1020th 1021st 1022nd 1023rd 1024th 1025th |
1000th 1001st 1002nd 1003rd 1004th 1005th 1006th 1007th 1008th 1009th 1010th 1011th 1012th 1013th 1014th 1015th 1016th 1017th 1018th 1019th 1020th 1021st 1022nd 1023rd 1024th 1025th |
||
</pre> |
</pre> |
||
=={{Header|PicoLisp}}== |
|||
=={{header|Picat}}== |
|||
<lang PicoLisp>(de rangeth (A B) |
|||
<code>nth/3</code> is a built-in predicate in Picat, so let's call the functions <code>nth2/1</code> and onward. |
|||
===Prolog style=== |
|||
{{trans|Prolog}} |
|||
<syntaxhighlight lang="picat">nth2(N) = N.to_string() ++ Th => |
|||
( tween(N) -> Th = "th" |
|||
; 1 = N mod 10 -> Th = "st" |
|||
; 2 = N mod 10 -> Th = "nd" |
|||
; 3 = N mod 10 -> Th = "rd" |
|||
; Th = "th" ). |
|||
tween(N) => Tween = N mod 100, between(11, 13, Tween).</syntaxhighlight> |
|||
===Function with explicit conditions=== |
|||
<syntaxhighlight lang="picat">nth3(N) = cc(N,"th"), tween(N) => true. |
|||
nth3(N) = cc(N,"st"), N mod 10 = 1 => true. |
|||
nth3(N) = cc(N,"nd"), N mod 10 = 2 => true. |
|||
nth3(N) = cc(N,"rd"), N mod 10 = 3 => true. |
|||
nth3(N) = cc(N,"th") => true. |
|||
% helper function |
|||
cc(N,Th) = N.to_string() ++ Th.</syntaxhighlight> |
|||
===List of suffixes=== |
|||
{{trans|Python}} |
|||
<syntaxhighlight lang="picat">nth4(N) = Nth => |
|||
Suffix = ["th","st","nd","rd","th","th","th","th","th","th"], |
|||
Nth = N.to_string() ++ cond((N mod 100 <= 10; N mod 100 > 20), Suffix[1 + N mod 10], "th").</syntaxhighlight> |
|||
===Test=== |
|||
<syntaxhighlight lang="picat">go => |
|||
Ranges = [ 0..25, 250..265, 1000..1025], |
|||
foreach(Range in Ranges) println([nth2(I) : I in Range]) |
|||
end, |
|||
nl.</syntaxhighlight> |
|||
{{out}} |
|||
<pre>[0th,1st,2nd,3rd,4th,5th,6th,7th,8th,9th,10th,11th,12th,13th,14th,15th,16th,17th,18th,19th,20th,21st,22nd,23rd,24th,25th] |
|||
[250th,251st,252nd,253rd,254th,255th,256th,257th,258th,259th,260th,261st,262nd,263rd,264th,265th] |
|||
[1000th,1001st,1002nd,1003rd,1004th,1005th,1006th,1007th,1008th,1009th,1010th,1011th,1012th,1013th,1014th,1015th,1016th,1017th,1018th,1019th,1020th,1021st,1022nd,1023rd,1024th,1025th]</pre> |
|||
=={{header|PicoLisp}}== |
|||
<syntaxhighlight lang="picolisp">(de rangeth (A B) |
|||
(mapcar |
(mapcar |
||
'((I) |
'((I) |
||
Line 1,264: | Line 3,645: | ||
(prinl (glue " " (rangeth 1000 1025))) |
(prinl (glue " " (rangeth 1000 1025))) |
||
(bye)</ |
(bye)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,273: | Line 3,654: | ||
=={{header|PL/I}}== |
=={{header|PL/I}}== |
||
<lang>Nth: procedure options (main); /* 1 June 2014 */ |
<syntaxhighlight lang="text">Nth: procedure options (main); /* 1 June 2014 */ |
||
declare i fixed (10); |
declare i fixed (10); |
||
Line 1,298: | Line 3,679: | ||
end enth; |
end enth; |
||
end Nth;</ |
end Nth;</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> 0th 1st 2nd 3rd 4th 5th 6th 7th 8th 9th 10th 11th 12th 13th 14th 15th 16th |
<pre> 0th 1st 2nd 3rd 4th 5th 6th 7th 8th 9th 10th 11th 12th 13th 14th 15th 16th |
||
Line 1,309: | Line 3,690: | ||
1011th 1012th 1013th 1014th 1015th 1016th 1017th 1018th 1019th 1020th 1021st |
1011th 1012th 1013th 1014th 1015th 1016th 1017th 1018th 1019th 1020th 1021st |
||
1022nd 1023rd 1024th 1025th</pre> |
1022nd 1023rd 1024th 1025th</pre> |
||
=={{header|PL/M}}== |
|||
{{works with|8080 PL/M Compiler}} ... under CP/M (or an emulator) |
|||
The 8080 PL/M compiler doesn't recognise lower-case letters, so the letters for the suffixes have to be specified by their ASCII codes. |
|||
<syntaxhighlight lang="plm"> |
|||
100H: /* SUFFIX NUMBERS WITH ST, ND, RD OR TH AS APPROPRIATE */ |
|||
/* CP/M BDOS SYSTEM CALL AND I/O ROUTINES */ |
|||
BDOS: PROCEDURE( FN, ARG ); DECLARE FN BYTE, ARG ADDRESS; GOTO 5; END; |
|||
PR$CHAR: PROCEDURE( C ); DECLARE C BYTE; CALL BDOS( 2, C ); END; |
|||
PR$STRING: PROCEDURE( S ); DECLARE S ADDRESS; CALL BDOS( 9, S ); END; |
|||
PR$NL: PROCEDURE; CALL PR$CHAR( 0DH ); CALL PR$CHAR( 0AH ); END; |
|||
PR$NUMBER: PROCEDURE( N ); /* PRINTS A NUMBER IN THE MINIMUN FIELD WIDTH */ |
|||
DECLARE N ADDRESS; |
|||
DECLARE V ADDRESS, N$STR ( 6 )BYTE, W BYTE; |
|||
V = N; |
|||
W = LAST( N$STR ); |
|||
N$STR( W ) = '$'; |
|||
N$STR( W := W - 1 ) = '0' + ( V MOD 10 ); |
|||
DO WHILE( ( V := V / 10 ) > 0 ); |
|||
N$STR( W := W - 1 ) = '0' + ( V MOD 10 ); |
|||
END; |
|||
CALL PR$STRING( .N$STR( W ) ); |
|||
END PR$NUMBER; |
|||
/* TASK */ |
|||
DECLARE LD LITERALLY '100' /* LOWER CASE LETTERS NEEDED FOR THE SUFFICES */ |
|||
, LH LITERALLY '104' |
|||
, LN LITERALLY '110' |
|||
, LR LITERALLY '114' |
|||
, LS LITERALLY '115' |
|||
, LT LITERALLY '116' |
|||
; |
|||
/* RETURNS THE TWO CHARACTER ORDINAL SUFFIX FOR N */ |
|||
SUFFIX: PROCEDURE( N )ADDRESS; |
|||
DECLARE N ADDRESS; |
|||
DECLARE ( S1, S2 ) BYTE; |
|||
S1 = LT; |
|||
S2 = LH; |
|||
IF N MOD 100 < 4 OR N MOD 100 > 20 THEN DO; |
|||
DECLARE N10 ADDRESS; |
|||
N10 = N MOD 10; |
|||
IF N10 = 1 THEN DO; |
|||
S1 = LS; |
|||
S2 = LT; |
|||
END; |
|||
ELSE IF N10 = 2 THEN DO; |
|||
S1 = LN; |
|||
S2 = LD; |
|||
END; |
|||
ELSE IF N10 = 3 THEN DO; |
|||
S1 = LR; |
|||
S2 = LD; |
|||
END; |
|||
END; |
|||
RETURN ( S1 * 256 ) + S2; |
|||
END SUFFIX; |
|||
/* PRINTS THE TWO CHARACTER SUFFIX IN S */ |
|||
PR$SUFFIX: PROCEDURE( S ); |
|||
DECLARE S ADDRESS; |
|||
CALL PR$CHAR( HIGH( S ) ); |
|||
CALL PR$CHAR( LOW( S ) ); |
|||
END PR$SUFFIX ; |
|||
DECLARE I ADDRESS; |
|||
DO I = 0 TO 25; |
|||
IF I < 10 THEN CALL PR$CHAR( ' ' ); |
|||
CALL PR$CHAR( ' ' );CALL PR$NUMBER( I );CALL PR$SUFFIX( SUFFIX( I ) ); |
|||
IF ( I + 1 ) MOD 10 = 0 THEN CALL PR$NL; |
|||
END; |
|||
CALL PR$NL; |
|||
CALL PR$NL; |
|||
DO I = 250 TO 265; |
|||
CALL PR$CHAR( ' ' );CALL PR$NUMBER( I );CALL PR$SUFFIX( SUFFIX( I ) ); |
|||
IF ( I - 249 ) MOD 10 = 0 THEN CALL PR$NL; |
|||
END; |
|||
CALL PR$NL; |
|||
CALL PR$NL; |
|||
DO I = 1000 TO 1025; |
|||
CALL PR$CHAR( ' ' );CALL PR$NUMBER( I );CALL PR$SUFFIX( SUFFIX( I ) ); |
|||
IF ( I - 999 ) MOD 10 = 0 THEN CALL PR$NL; |
|||
END; |
|||
CALL PR$NL; |
|||
EOF |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
0th 1st 2nd 3rd 4th 5th 6th 7th 8th 9th |
|||
10th 11th 12th 13th 14th 15th 16th 17th 18th 19th |
|||
20th 21st 22nd 23rd 24th 25th |
|||
250th 251st 252nd 253rd 254th 255th 256th 257th 258th 259th |
|||
260th 261st 262nd 263rd 264th 265th |
|||
1000th 1001st 1002nd 1003rd 1004th 1005th 1006th 1007th 1008th 1009th |
|||
1010th 1011th 1012th 1013th 1014th 1015th 1016th 1017th 1018th 1019th |
|||
1020th 1021st 1022nd 1023rd 1024th 1025th |
|||
</pre> |
|||
=={{header|PowerShell}}== |
=={{header|PowerShell}}== |
||
<lang |
<syntaxhighlight lang="powershell"> |
||
function nth($inp){ |
|||
$suffix = "th" |
|||
$suffix = "th" |
|||
switch($inp % 100){ |
|||
11{$suffix="th"} |
|||
12{$suffix="th"} |
|||
13{$suffix="th"} |
|||
default{ |
|||
} |
|||
switch($inp % 10){ |
|||
return "$inp$suffix " |
|||
1{$suffix="st"} |
|||
2{$suffix="nd"} |
|||
3{$suffix="rd"} |
|||
} |
|||
} |
|||
} |
|||
return "$inp$suffix " |
|||
} |
} |
||
0..25 | %{Write-host -nonewline (nth "$_")};"" |
0..25 | %{Write-host -nonewline (nth "$_")};"" |
||
250..265 | %{Write-host -nonewline (nth "$_")};"" |
250..265 | %{Write-host -nonewline (nth "$_")};"" |
||
1000..1025 | %{Write-host -nonewline (nth "$_")};"" |
1000..1025 | %{Write-host -nonewline (nth "$_")};"" |
||
</syntaxhighlight> |
|||
{{Out}} |
{{Out}} |
||
<pre> |
|||
<pre>0th 1st 2nd 3rd 4th 5th 6th 7th 8th 9th 10th 11st 12nd 13rd 14th 15th 16th 17th 18th 19th 20th 21st 22nd 23rd 24th 25th |
|||
0th 1st 2nd 3rd 4th 5th 6th 7th 8th 9th 10th 11th 12th 13th 14th 15th 16th 17th 18th 19th 20th 21st 22nd 23rd 24th 25th |
|||
250th 251st 252nd 253rd 254th 255th 256th 257th 258th 259th 260th 261st 262nd 263rd 264th 265th |
250th 251st 252nd 253rd 254th 255th 256th 257th 258th 259th 260th 261st 262nd 263rd 264th 265th |
||
1000th 1001st 1002nd 1003rd 1004th 1005th 1006th 1007th 1008th 1009th 1010th |
1000th 1001st 1002nd 1003rd 1004th 1005th 1006th 1007th 1008th 1009th 1010th 1011th 1012th 1013th 1014th 1015th 1016th 1017th 1018th 1019th 1020th 1021st 1022nd 1023rd 1024th 1025th |
||
</pre> |
|||
===An Alternate Version=== |
|||
This is, I think, is a more "PowerShelly" way: |
|||
<syntaxhighlight lang="powershell"> |
|||
function Get-Nth ([int]$Number) |
|||
{ |
|||
$suffix = "th" |
|||
switch ($Number % 100){ |
|||
11 {$suffix = "th"} |
|||
12 {$suffix = "th"} |
|||
13 {$suffix = "th"} |
|||
default { |
|||
switch ($Number % 10){ |
|||
1 {$suffix = "st"} |
|||
2 {$suffix = "nd"} |
|||
3 {$suffix = "rd"} |
|||
} |
|||
} |
|||
} |
|||
"$Number$suffix" |
|||
} |
|||
1..25 | ForEach-Object {Get-Nth $_} | Format-Wide {$_} -Column 5 -Force |
|||
251..265 | ForEach-Object {Get-Nth $_} | Format-Wide {$_} -Column 5 -Force |
|||
1001..1025 | ForEach-Object {Get-Nth $_} | Format-Wide {$_} -Column 5 -Force |
|||
</syntaxhighlight> |
|||
{{Out}} |
|||
<pre> |
|||
1st 2nd 3rd 4th 5th |
|||
6th 7th 8th 9th 10th |
|||
11th 12th 13th 14th 15th |
|||
16th 17th 18th 19th 20th |
|||
21st 22nd 23rd 24th 25th |
|||
251st 252nd 253rd 254th 255th |
|||
256th 257th 258th 259th 260th |
|||
261st 262nd 263rd 264th 265th |
|||
1001st 1002nd 1003rd 1004th 1005th |
|||
1006th 1007th 1008th 1009th 1010th |
|||
1011th 1012th 1013th 1014th 1015th |
|||
1016th 1017th 1018th 1019th 1020th |
|||
1021st 1022nd 1023rd 1024th 1025th |
|||
</pre> |
|||
=={{header|Prolog}}== |
=={{header|Prolog}}== |
||
Line 1,335: | Line 3,878: | ||
Following Icon: |
Following Icon: |
||
< |
<syntaxhighlight lang="prolog">nth(N, N_Th) :- |
||
( tween(N) -> Th = "th" |
( tween(N) -> Th = "th" |
||
; 1 is N mod 10 -> Th = "st" |
; 1 is N mod 10 -> Th = "st" |
||
Line 1,351: | Line 3,894: | ||
nl, nl, |
nl, nl, |
||
forall( between(1000,1025,N), (nth(N, N_Th), format('~w, ', N_Th)) ). |
forall( between(1000,1025,N), (nth(N, N_Th), format('~w, ', N_Th)) ). |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} of `test/0`: |
{{out}} of `test/0`: |
||
Line 1,364: | Line 3,907: | ||
</pre> |
</pre> |
||
=={{ |
=={{header|Python}}== |
||
< |
<syntaxhighlight lang="python">_suffix = ['th', 'st', 'nd', 'rd', 'th', 'th', 'th', 'th', 'th', 'th'] |
||
def nth(n): |
def nth(n): |
||
Line 1,372: | Line 3,915: | ||
if __name__ == '__main__': |
if __name__ == '__main__': |
||
for j in range(0,1001, 250): |
for j in range(0,1001, 250): |
||
print(' '.join(nth(i) for i in list(range(j, j+25))))</ |
print(' '.join(nth(i) for i in list(range(j, j+25))))</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>0'th 1'st 2'nd 3'rd 4'th 5'th 6'th 7'th 8'th 9'th 10'th 11'th 12'th 13'th 14'th 15'th 16'th 17'th 18'th 19'th 20'th 21'st 22'nd 23'rd 24'th |
<pre>0'th 1'st 2'nd 3'rd 4'th 5'th 6'th 7'th 8'th 9'th 10'th 11'th 12'th 13'th 14'th 15'th 16'th 17'th 18'th 19'th 20'th 21'st 22'nd 23'rd 24'th |
||
Line 1,380: | Line 3,923: | ||
1000'th 1001'st 1002'nd 1003'rd 1004'th 1005'th 1006'th 1007'th 1008'th 1009'th 1010'th 1011'th 1012'th 1013'th 1014'th 1015'th 1016'th 1017'th 1018'th 1019'th 1020'th 1021'st 1022'nd 1023'rd 1024'th</pre> |
1000'th 1001'st 1002'nd 1003'rd 1004'th 1005'th 1006'th 1007'th 1008'th 1009'th 1010'th 1011'th 1012'th 1013'th 1014'th 1015'th 1016'th 1017'th 1018'th 1019'th 1020'th 1021'st 1022'nd 1023'rd 1024'th</pre> |
||
'''Alternate version''' |
'''Alternate version''' |
||
< |
<syntaxhighlight lang="python">#!/usr/bin/env python3 |
||
def ord(n): |
def ord(n): |
||
Line 1,394: | Line 3,937: | ||
print(*(ord(n) for n in range(26))) |
print(*(ord(n) for n in range(26))) |
||
print(*(ord(n) for n in range(250,266))) |
print(*(ord(n) for n in range(250,266))) |
||
print(*(ord(n) for n in range(1000,1026)))</ |
print(*(ord(n) for n in range(1000,1026)))</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,406: | Line 3,949: | ||
</pre> |
</pre> |
||
=={{ |
=={{header|Quackery}}== |
||
<syntaxhighlight lang="quackery"> [ table ] is suffix ( n --> $ ) |
|||
$ "th st nd rd th th th th th th" |
|||
nest$ witheach [ ' suffix put ] |
|||
[ dup number$ |
|||
swap dup 100 mod |
|||
10 21 within iff |
|||
[ drop $ "th" join ] |
|||
else |
|||
[ 10 mod |
|||
suffix join ] ] is ordinal$ ( n --> $ ) |
|||
[ over - 1+ |
|||
[] swap times |
|||
[ over i^ + ordinal$ |
|||
nested join ] |
|||
nip 50 wrap$ ] is test ( n n --> ) |
|||
0 25 test |
|||
cr |
|||
250 265 test |
|||
cr |
|||
1000 1025 test</syntaxhighlight> |
|||
{{out}} |
|||
<pre>0th 1st 2nd 3rd 4th 5th 6th 7th 8th 9th 10th 11th |
|||
12th 13th 14th 15th 16th 17th 18th 19th 20th 21st |
|||
22nd 23rd 24th 25th |
|||
250th 251st 252nd 253rd 254th 255th 256th 257th |
|||
258th 259th 260th 261st 262nd 263rd 264th 265th |
|||
1000th 1001st 1002nd 1003rd 1004th 1005th 1006th |
|||
1007th 1008th 1009th 1010th 1011th 1012th 1013th |
|||
1014th 1015th 1016th 1017th 1018th 1019th 1020th |
|||
1021st 1022nd 1023rd 1024th 1025th</pre> |
|||
=={{header|R}}== |
|||
{{trans|Python}} |
{{trans|Python}} |
||
Note that R vectors are 1-indexed. |
Note that R vectors are 1-indexed. |
||
< |
<syntaxhighlight lang="rsplus">nth <- function(n) |
||
{ |
{ |
||
if (length(n) > 1) return(sapply(n, nth)) |
if (length(n) > 1) return(sapply(n, nth)) |
||
Line 1,426: | Line 4,010: | ||
range <- list(0:25, 250:275, 500:525, 750:775, 1000:1025) |
range <- list(0:25, 250:275, 500:525, 750:775, 1000:1025) |
||
sapply(range, nth)</ |
sapply(range, nth)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> [,1] [,2] [,3] [,4] [,5] |
<pre> [,1] [,2] [,3] [,4] [,5] |
||
Line 1,459: | Line 4,043: | ||
=={{header|Racket}}== |
=={{header|Racket}}== |
||
< |
<syntaxhighlight lang="racket">#lang racket |
||
(define (teen? n) (<= 11 (modulo n 100) 19)) |
(define (teen? n) (<= 11 (modulo n 100) 19)) |
||
(define (Nth n) |
(define (Nth n) |
||
Line 1,468: | Line 4,052: | ||
(for ((range (list (in-range 26) (in-range 250 266) (in-range 1000 1026)))) |
(for ((range (list (in-range 26) (in-range 250 266) (in-range 1000 1026)))) |
||
(displayln (string-join (for/list ((nth (sequence-map Nth range))) nth) " ")))</ |
(displayln (string-join (for/list ((nth (sequence-map Nth range))) nth) " ")))</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>0'th 1'st 2'nd 3'rd 4'th 5'th 6'th 7'th 8'th 9'th 10'th 11'th 12'th 13'th 14'th 15'th 16'th 17'th 18'th 19'th 20'th 21'st 22'nd 23'rd 24'th 25'th |
<pre>0'th 1'st 2'nd 3'rd 4'th 5'th 6'th 7'th 8'th 9'th 10'th 11'th 12'th 13'th 14'th 15'th 16'th 17'th 18'th 19'th 20'th 21'st 22'nd 23'rd 24'th 25'th |
||
250'th 251'st 252'nd 253'rd 254'th 255'th 256'th 257'th 258'th 259'th 260'th 261'st 262'nd 263'rd 264'th 265'th |
250'th 251'st 252'nd 253'rd 254'th 255'th 256'th 257'th 258'th 259'th 260'th 261'st 262'nd 263'rd 264'th 265'th |
||
1000'th 1001'st 1002'nd 1003'rd 1004'th 1005'th 1006'th 1007'th 1008'th 1009'th 1010'th 1011'th 1012'th 1013'th 1014'th 1015'th 1016'th 1017'th 1018'th 1019'th 1020'th 1021'st 1022'nd 1023'rd 1024'th 1025'th</pre> |
1000'th 1001'st 1002'nd 1003'rd 1004'th 1005'th 1006'th 1007'th 1008'th 1009'th 1010'th 1011'th 1012'th 1013'th 1014'th 1015'th 1016'th 1017'th 1018'th 1019'th 1020'th 1021'st 1022'nd 1023'rd 1024'th 1025'th</pre> |
||
=={{header|Raku}}== |
|||
(formerly Perl 6) |
|||
(Spurious apostrophes intentionally omitted.) |
|||
<syntaxhighlight lang="raku" line>my %irregulars = <1 st 2 nd 3 rd>, (11..13 X=> 'th'); |
|||
sub nth ($n) { $n ~ ( %irregulars{$n % 100} // %irregulars{$n % 10} // 'th' ) } |
|||
say .list».&nth for [^26], [250..265], [1000..1025];</syntaxhighlight> |
|||
{{out}} |
|||
<pre>0th 1st 2nd 3rd 4th 5th 6th 7th 8th 9th 10th 11th 12th 13th 14th 15th 16th 17th 18th 19th 20th 21st 22nd 23rd 24th 25th |
|||
250th 251st 252nd 253rd 254th 255th 256th 257th 258th 259th 260th 261st 262nd 263rd 264th 265th |
|||
1000th 1001st 1002nd 1003rd 1004th 1005th 1006th 1007th 1008th 1009th 1010th 1011th 1012th 1013th 1014th 1015th 1016th 1017th 1018th 1019th 1020th 1021st 1022nd 1023rd 1024th 1025th</pre> |
|||
If you want to get Unicodally fancy, use this version instead: |
|||
<syntaxhighlight lang="raku" line>my %irregulars = <1 ˢᵗ 2 ⁿᵈ 3 ʳᵈ>, (11..13 X=> 'ᵗʰ'); |
|||
sub nth ($n) { $n ~ ( %irregulars{$n % 100} // %irregulars{$n % 10} // 'ᵗʰ' ) } |
|||
say .list».&nth for [^26], [250..265], [1000..1025];</syntaxhighlight> |
|||
{{out}} |
|||
<blockquote>0ᵗʰ 1ˢᵗ 2ⁿᵈ 3ʳᵈ 4ᵗʰ 5ᵗʰ 6ᵗʰ 7ᵗʰ 8ᵗʰ 9ᵗʰ 10ᵗʰ 11ᵗʰ 12ᵗʰ 13ᵗʰ 14ᵗʰ 15ᵗʰ 16ᵗʰ 17ᵗʰ 18ᵗʰ 19ᵗʰ 20ᵗʰ 21ˢᵗ 22ⁿᵈ 23ʳᵈ 24ᵗʰ 25ᵗʰ<br> |
|||
250ᵗʰ 251ˢᵗ 252ⁿᵈ 253ʳᵈ 254ᵗʰ 255ᵗʰ 256ᵗʰ 257ᵗʰ 258ᵗʰ 259ᵗʰ 260ᵗʰ 261ˢᵗ 262ⁿᵈ 263ʳᵈ 264ᵗʰ 265ᵗʰ<br> |
|||
1000ᵗʰ 1001ˢᵗ 1002ⁿᵈ 1003ʳᵈ 1004ᵗʰ 1005ᵗʰ 1006ᵗʰ 1007ᵗʰ 1008ᵗʰ 1009ᵗʰ 1010ᵗʰ 1011ᵗʰ 1012ᵗʰ 1013ᵗʰ 1014ᵗʰ 1015ᵗʰ 1016ᵗʰ 1017ᵗʰ 1018ᵗʰ 1019ᵗʰ 1020ᵗʰ 1021ˢᵗ 1022ⁿᵈ 1023ʳᵈ 1024ᵗʰ 1025ᵗʰ</blockquote> |
|||
=={{header|Red}}== |
|||
<syntaxhighlight lang="rebol">Red[] |
|||
nth: function [n][ |
|||
d: n % 10 |
|||
dd: n % 100 |
|||
suffix: either any [ all [ dd > 3 dd < 20 ] d < 1 d > 4 1 = to-integer n / 10] [4] [d] |
|||
rejoin [n pick ["st" "nd" "rd" "th"] suffix] |
|||
] |
|||
test: function [low high][ |
|||
repeat i high - low + 1 [ |
|||
prin [nth i + low - 1 ""] |
|||
] |
|||
prin newline |
|||
] |
|||
test 0 25 |
|||
test 250 265 |
|||
test 1000 1025</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
0th 1st 2nd 3rd 4th 5th 6th 7th 8th 9th 10th 11th 12th 13th 14th 15th 16th 17th 18th 19th 20th 21st 22nd 23rd 24th 25th |
|||
250th 251st 252nd 253rd 254th 255th 256th 257th 258th 259th 260th 261st 262nd 263rd 264th 265th |
|||
1000th 1001st 1002nd 1003rd 1004th 1005th 1006th 1007th 1008th 1009th 1010th 1011th 1012th 1013th 1014th 1015th 1016th 1017th 1018th 1019th 1020th 1021st 1022nd 1023rd 1024th 1025th |
|||
</pre> |
|||
=={{header|Refal}}== |
|||
<syntaxhighlight lang="refal">$ENTRY Go { |
|||
= <Show 0 25> |
|||
<Show 250 265> |
|||
<Show 1000 1025>; |
|||
}; |
|||
Nth { |
|||
s.N, <Symb s.N>: { |
|||
e.X '1' s.Y = e.X '1' s.Y 'th'; |
|||
e.X '1' = e.X '1st'; |
|||
e.X '2' = e.X '2nd'; |
|||
e.X '3' = e.X '3rd'; |
|||
e.X = e.X 'th'; |
|||
}; |
|||
}; |
|||
Cell { |
|||
s.N, <First 8 <Nth s.N> ' '>: (e.1) e.2 = e.1; |
|||
} |
|||
Group { |
|||
s.N e.1 = <Group (s.N s.N) () e.1>; |
|||
(s.N s.M) (e.1) = (e.1); |
|||
(s.N 0) (e.1) e.2 = (e.1) <Group (s.N s.N) () e.2>; |
|||
(s.N s.M) (e.1) s.2 e.3 = <Group (s.N <- s.M 1>) (e.1 s.2) e.3>; |
|||
}; |
|||
Iota { |
|||
s.End s.End = s.End; |
|||
s.Start s.End = s.Start <Iota <+ s.Start 1> s.End>; |
|||
}; |
|||
Each { |
|||
s.F = ; |
|||
s.F t.I e.X = <Mu s.F t.I> <Each s.F e.X>; |
|||
}; |
|||
ShowLine { |
|||
(e.Line) = <Prout <Each Cell e.Line>>; |
|||
}; |
|||
Show { |
|||
s.Start s.End, |
|||
<Iota s.Start s.End>: e.Range, |
|||
<Group 10 e.Range>: e.Lines |
|||
= <Prout <Each ShowLine e.Lines>>; |
|||
};</syntaxhighlight> |
|||
{{out}} |
|||
<pre>0th 1st 2nd 3rd 4th 5th 6th 7th 8th 9th |
|||
10th 11th 12th 13th 14th 15th 16th 17th 18th 19th |
|||
20th 21st 22nd 23rd 24th 25th |
|||
250th 251st 252nd 253rd 254th 255th 256th 257th 258th 259th |
|||
260th 261st 262nd 263rd 264th 265th |
|||
1000th 1001st 1002nd 1003rd 1004th 1005th 1006th 1007th 1008th 1009th |
|||
1010th 1011th 1012th 1013th 1014th 1015th 1016th 1017th 1018th 1019th |
|||
1020th 1021st 1022nd 1023rd 1024th 1025th</pre> |
|||
=={{header|REXX}}== |
=={{header|REXX}}== |
||
This version adds suffixes without apostrophes. |
This version adds suffixes without apostrophes. |
||
Negative numbers |
Negative numbers are also handled. |
||
<lang rexx>/*REXX pgm shows ranges of numbers with ordinal (st/nd/rd/th) suffixes.*/ |
|||
Two alternate solutions added to the oiginal |
|||
call tell 0, 25 /*show the 1st range of numbers. */ |
|||
<syntaxhighlight lang="rexx">/*REXX program shows ranges of numbers with ordinal (st/nd/rd/th) suffixes attached.*/ |
|||
call tell 250, 265 /* " " 2nd " " " */ |
|||
Call tell 0,25 /* display the 1st range of numbers */ |
|||
Call tell 250,265 /* " " 2nd " " " */ |
|||
Call tell 1000,1025 /* " " 3rd " " " */ |
|||
/*──────────────────────────────────TELL subroutine─────────────────────*/ |
|||
Exit /* stick a fork in it, we're all done */ |
|||
tell: procedure; parse arg L,H; $= /*get Low & High #s, nullify list*/ |
|||
/*-------------------------------------------------------------------------*/ |
|||
do j=L to H /*process the range, low───►high.*/ |
|||
tell: Procedure |
|||
$=$ j || th(j) /*append the Nth number to list*/ |
|||
Parse Arg low,high /* get the Low and High numbers */ |
|||
out.='' |
|||
say 'numbers from ' L " to " H ' (inclusive):' /*display the title.*/ |
|||
Do n=low To high /* process the range, from low */ |
|||
out.1=out.1 th(n) |
|||
say /*display blank line*/ |
|||
out.2=out.2 th2(n) |
|||
return /*return to invoker.*/ |
|||
out.3=out.3 th3(n) |
|||
/*──────────────────────────────────TH subroutine───────────────────────*/ |
|||
End |
|||
th: procedure; parse arg x; x=abs(x) /*just use ABS value*/ |
|||
Say 'numbers from' low 'to' high '(inclusive):' /*display the output */ |
|||
return word('th st nd rd', 1 + x//10 * (x//100%10 \== 1) * (x//10 < 4) )</lang> |
|||
Say strip(out.1) |
|||
{{out}} using the default inputs: |
|||
Say '' |
|||
If out.2<>out.1 Then Say 'th2 must be wrong' |
|||
If out.3<>out.1 Then Say 'th3 must be wrong' |
|||
Return |
|||
/*-------------------------------------------------------------------------*/ |
|||
th: /* compact original */ |
|||
Parse Arg z |
|||
x=abs(z) |
|||
Return z||word('th st nd rd',1+x//10*(x//100%10\==1)*(x//10<4)) |
|||
/*-------------------------------------------------------------------------*/ |
|||
th2: /* rather verbose logic */ |
|||
Parse Arg z |
|||
x=abs(z) |
|||
Select |
|||
When length(x)=1 Then |
|||
If x<4 Then |
|||
suffix=word('th st nd rd',x+1) |
|||
Else |
|||
suffix='th' |
|||
When suffixstr(x,length(x)-1,1)=1 Then |
|||
suffix='th' |
|||
When right(x,1)<4 Then |
|||
suffix=word('th st nd rd',right(x,1)+1) |
|||
Otherwise |
|||
suffix='th' |
|||
End |
|||
Return z||suffix |
|||
/*-------------------------------------------------------------------------*/ |
|||
th3: /* compact yet quite readable */ |
|||
Parse Arg z |
|||
Parse Value reverse(z) with last +1 prev +1 |
|||
If last<4 &, /* last digit is 0,1,2,3 */ |
|||
prev<>1 Then /* and number is not **1x */ |
|||
suffix=word('th st nd rd',last+1) |
|||
Else /* all oher cases */ |
|||
suffix='th' |
|||
Return z||suffix</syntaxhighlight> |
|||
'''output''' using the default inputs: |
|||
<pre> |
<pre> |
||
numbers from 0 to 25 (inclusive): |
numbers from 0 to 25 (inclusive): |
||
0th 1st 2nd 3rd 4th 5th 6th 7th 8th 9th 10th 11th 12th 13th 14th 15th 16th 17th 18th 19th 20th 21st 22nd 23rd 24th 25th |
0th 1st 2nd 3rd 4th 5th 6th 7th 8th 9th 10th 11th 12th 13th 14th 15th 16th 17th 18th 19th 20th 21st 22nd 23rd 24th 25th |
||
numbers from 250 to 265 (inclusive): |
|||
numbers from 250 to 265 (inclusive): |
|||
250th 251st 252nd 253rd 254th 255th 256th 257th 258th 259th 260th 261st 262nd 263rd 264th 265th |
250th 251st 252nd 253rd 254th 255th 256th 257th 258th 259th 260th 261st 262nd 263rd 264th 265th |
||
numbers from 1000 to 1025 (inclusive): |
|||
numbers from 1000 to 1025 (inclusive): |
|||
1000th 1001st 1002nd 1003rd 1004th 1005th 1006th 1007th 1008th 1009th 1010th 1011th 1012th 1013th 1014th 1015th 1016th 1017th 1018th 1019th 1020th 1021st 1022nd 1023rd 1024th 1025th |
1000th 1001st 1002nd 1003rd 1004th 1005th 1006th 1007th 1008th 1009th 1010th 1011th 1012th 1013th 1014th 1015th 1016th 1017th 1018th 1019th 1020th 1021st 1022nd 1023rd 1024th 1025th |
||
</pre> |
|||
=={{header|Ring}}== |
|||
<syntaxhighlight lang="ring"> |
|||
for nr = 0 to 25 |
|||
see Nth(nr) + Nth(nr + 250) + Nth(nr + 1000) + nl |
|||
next |
|||
func getSuffix n |
|||
lastTwo = n % 100 |
|||
lastOne = n % 10 |
|||
if lastTwo > 3 and lastTwo < 21 "th" ok |
|||
if lastOne = 1 return "st" ok |
|||
if lastOne = 2 return "nd" ok |
|||
if lastOne = 3 return "rd" ok |
|||
return "th" |
|||
func Nth n |
|||
return "" + n + "'" + getSuffix(n) + " " |
|||
</syntaxhighlight> |
|||
=={{header|RPL}}== |
|||
{{works with|Halcyon Calc|4.2.7}} |
|||
===Clean version=== |
|||
≪ IP DUP 10 MOD OVER 100 MOD 10 / IP → units tens |
|||
≪ →STR "'" + |
|||
'''IF''' units 1 ≥ units 3 ≤ AND tens 1 ≠ AND |
|||
'''THEN''' { "st" "nd" "rd" } units GET |
|||
'''ELSE''' "th" |
|||
'''END''' |
|||
+ |
|||
≫ ≫ '<span style="color:blue">NTH</span>' STO |
|||
===One-liner=== |
|||
This version uses a mix of arithmetic and boolean operations to determine when to apply the "st/nd/rd" exception. |
|||
≪ IP →STR LAST { "'th" "'st" "'nd" "'rd" } OVER 10 MOD DUP 4 < * ROT 100 MOD 10 / IP 1 ≠ * 1 + GET + ≫ '<span style="color:blue">NTH</span>' STO |
|||
≪ { } ROT ROT '''FOR''' j j <span style="color:blue">NTH</span> + '''NEXT''' ≫ |
|||
'<span style="color:blue">SHOW</span>' STO |
|||
0 25 <span style="color:blue">SHOW</span> |
|||
250 265 <span style="color:blue">SHOW</span> |
|||
1000 1025 <span style="color:blue">SHOW</span> |
|||
{{out}} |
|||
<pre> |
|||
3: { "0'th" "1'st" "2'nd" "3'rd" "4'th" "5'th" "6'th" "7'th" "8'th" "9'th" "10'th" "11'th" "12'th" "13'th" "14'th" "15'th" "16'th" "17'th" "18'th" "19'th" "20'th" "21'st" "22'nd" "23'rd" "24'th" "25'th" } |
|||
2: { "250'th" "251'st" "252'nd" "253'rd" "254'th" "255'th" "256'th" "257'th" "258'th" "259'th" "260'th" "261'st" "262'nd" "263'rd" "264'th" "265'th" } |
|||
1: { "1000'th" "1001'st" "1002'nd" "1003'rd" "1004'th" "1005'th" "1006'th" "1007'th" "1008'th" "1009'th" "1010'th" "1011'th" "1012'th" "1013'th" "1014'th" "1015'th" "1016'th" "1017'th" "1018'th" "1019'th" "1020'th" "1021'st" "1022'nd" "1023'rd" "1024'th" "1025'th" } |
|||
</pre> |
</pre> |
||
=={{header|Ruby}}== |
=={{header|Ruby}}== |
||
Code (slightly adapted) and methodname taken from ActiveSupport (Ruby on Rails). |
Code (slightly adapted) and methodname taken from ActiveSupport (Ruby on Rails). |
||
< |
<syntaxhighlight lang="ruby">class Integer |
||
def ordinalize |
def ordinalize |
||
num = self.abs |
num = self.abs |
||
Line 1,527: | Line 4,308: | ||
[(0..25),(250..265),(1000..1025)].each{|r| puts r.map(&:ordinalize).join(", "); puts} |
[(0..25),(250..265),(1000..1025)].each{|r| puts r.map(&:ordinalize).join(", "); puts} |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,536: | Line 4,317: | ||
1000th, 1001st, 1002nd, 1003rd, 1004th, 1005th, 1006th, 1007th, 1008th, 1009th, 1010th, 1011th, 1012th, 1013th, 1014th, 1015th, 1016th, 1017th, 1018th, 1019th, 1020th, 1021st, 1022nd, 1023rd, 1024th, 1025th |
1000th, 1001st, 1002nd, 1003rd, 1004th, 1005th, 1006th, 1007th, 1008th, 1009th, 1010th, 1011th, 1012th, 1013th, 1014th, 1015th, 1016th, 1017th, 1018th, 1019th, 1020th, 1021st, 1022nd, 1023rd, 1024th, 1025th |
||
</pre> |
</pre> |
||
=={{header|Rust}}== |
=={{header|Rust}}== |
||
< |
<syntaxhighlight lang="rust">fn nth(num: isize) -> String { |
||
format!("{}{}", num, match (num % 10, num % 100) { |
format!("{}{}", num, match (num % 10, num % 100) { |
||
(1, 11) => "th", |
(1, 11) | (2, 12) | (3, 13) => "th", |
||
(1, _) |
(1, _) => "st", |
||
(2, |
(2, _) => "nd", |
||
( |
(3, _) => "rd", |
||
_ => "th", |
|||
(3, _) => "rd", |
|||
_ => "th" |
|||
}) |
}) |
||
} |
} |
||
fn main() { |
fn main() { |
||
let ranges = |
let ranges = [(0, 26), (250, 266), (1000, 1026)]; |
||
for &(s, e) in &ranges { |
|||
(250, 266), |
|||
(1000, 1026) |
|||
]; |
|||
for &(s, e) in ranges.iter() { |
|||
println!("[{}, {}) :", s, e); |
println!("[{}, {}) :", s, e); |
||
for i in s..e { |
for i in s..e { |
||
print!("{}, ", nth(i)); |
print!("{}, ", nth(i)); |
||
} |
} |
||
println!( |
println!(); |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,576: | Line 4,350: | ||
=={{header|Scala}}== |
=={{header|Scala}}== |
||
{{libheader|Scala}}< |
{{libheader|Scala}}<syntaxhighlight lang="scala">object Nth extends App { |
||
def abbrevNumber(i: Int) = print(s"$i${ordinalAbbrev(i)} ") |
def abbrevNumber(i: Int) = print(s"$i${ordinalAbbrev(i)} ") |
||
Line 1,595: | Line 4,369: | ||
println(); |
println(); |
||
(1000 to 1025).foreach(abbrevNumber) |
(1000 to 1025).foreach(abbrevNumber) |
||
}</ |
}</syntaxhighlight> |
||
=={{header|sed}}== |
|||
<syntaxhighlight lang="sed">/1.$/!{s/1$/1st/;s/2$/2nd/;s/3$/3rd/;};s/[0-9]$/&th/</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
$ for n in 0 250 1000; do echo $(seq $n $((n + 25)) | sed -f nth.sed); done |
|||
0th 1st 2nd 3rd 4th 5th 6th 7th 8th 9th 10th 11th 12th 13th 14th 15th 16th 17th 18th 19th 20th 21st 22nd 23rd 24th 25th |
|||
250th 251st 252nd 253rd 254th 255th 256th 257th 258th 259th 260th 261st 262nd 263rd 264th 265th 266th 267th 268th 269th 270th 271st 272nd 273rd 274th 275th |
|||
1000th 1001st 1002nd 1003rd 1004th 1005th 1006th 1007th 1008th 1009th 1010th 1011th 1012th 1013th 1014th 1015th 1016th 1017th 1018th 1019th 1020th 1021st 1022nd 1023rd 1024th 1025th |
|||
</pre> |
|||
=={{header|Seed7}}== |
=={{header|Seed7}}== |
||
< |
<syntaxhighlight lang="seed7">$ include "seed7_05.s7i"; |
||
const func string: suffix (in integer: num) is func |
const func string: suffix (in integer: num) is func |
||
Line 1,626: | Line 4,410: | ||
printImages( 250, 265); |
printImages( 250, 265); |
||
printImages(1000, 1025); |
printImages(1000, 1025); |
||
end func;</ |
end func;</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,634: | Line 4,418: | ||
1000th 1001st 1002nd 1003rd 1004th 1005th 1006th 1007th 1008th 1009th 1010th 1011th 1012th 1013th 1014th 1015th 1016th 1017th 1018th 1019th 1020th 1021st 1022nd 1023rd 1024th 1025th |
1000th 1001st 1002nd 1003rd 1004th 1005th 1006th 1007th 1008th 1009th 1010th 1011th 1012th 1013th 1014th 1015th 1016th 1017th 1018th 1019th 1020th 1021st 1022nd 1023rd 1024th 1025th |
||
</pre> |
</pre> |
||
=={{header|SETL}}== |
|||
<syntaxhighlight lang="setl">program nth; |
|||
loop for r in [[0..25], [250..265], [1000..1025]] do |
|||
showrange(r); |
|||
end loop; |
|||
proc showrange(r); |
|||
i := 0; |
|||
loop for s in [nth(n) : n in r] do |
|||
putchar(rpad(s, 8)); |
|||
if (i +:= 1) mod 6 = 0 then print(); end if; |
|||
end loop; |
|||
print(); |
|||
end proc; |
|||
proc nth(n); |
|||
sfx := {[1,"st"], [2,"nd"], [3,"rd"]}; |
|||
return str n + |
|||
if n div 10 mod 10 = 1 |
|||
then "th" |
|||
else sfx(n mod 10) ? "th" |
|||
end if; |
|||
end proc; |
|||
end program;</syntaxhighlight> |
|||
{{out}} |
|||
<pre>0th 1st 2nd 3rd 4th 5th |
|||
6th 7th 8th 9th 10th 11th |
|||
12th 13th 14th 15th 16th 17th |
|||
18th 19th 20th 21st 22nd 23rd |
|||
24th 25th |
|||
250th 251st 252nd 253rd 254th 255th |
|||
256th 257th 258th 259th 260th 261st |
|||
262nd 263rd 264th 265th |
|||
1000th 1001st 1002nd 1003rd 1004th 1005th |
|||
1006th 1007th 1008th 1009th 1010th 1011th |
|||
1012th 1013th 1014th 1015th 1016th 1017th |
|||
1018th 1019th 1020th 1021st 1022nd 1023rd |
|||
1024th 1025th</pre> |
|||
=={{header|Set lang}}== |
|||
Due to the language's specification, the input can only contain one character. Therefore, the following code only works with 0-9. |
|||
<syntaxhighlight lang="set_lang">set o 49 |
|||
set t 50 |
|||
set h 51 |
|||
set n ! |
|||
set ! n |
|||
set ! 39 |
|||
[n=o] set ? 13 |
|||
[n=t] set ? 16 |
|||
[n=h] set ? 19 |
|||
set ! T |
|||
set ! H |
|||
set ? 21 |
|||
set ! S |
|||
set ! T |
|||
set ? 21 |
|||
set ! N |
|||
set ! D |
|||
set ? 12 |
|||
set ! R |
|||
set ! D |
|||
> EOF</syntaxhighlight> |
|||
Input: I, Output: O |
|||
<pre>I: 1, O: 1'ST |
|||
I: 2, O: 2'ND |
|||
I: 3, O: 3'RD |
|||
I: 4, O: 4'TH |
|||
I: 5, O: 5'TH |
|||
...</pre> |
|||
=={{header|Sidef}}== |
=={{header|Sidef}}== |
||
{{trans| |
{{trans|Raku}} |
||
< |
<syntaxhighlight lang="ruby">func nth(n) { |
||
static irregulars = Hash(<1 ˢᵗ 2 ⁿᵈ 3 ʳᵈ 11 ᵗʰ 12 ᵗʰ 13 ᵗʰ>...) |
|||
n.to_s + (irregulars |
n.to_s + (irregulars{n % 100} \\ irregulars{n % 10} \\ 'ᵗʰ') |
||
} |
} |
||
[ |
for r in [0..25, 250..265, 1000..1025] { |
||
say r.map {|n| nth(n) }.join(" ") |
say r.map {|n| nth(n) }.join(" ") |
||
}</syntaxhighlight> |
|||
};</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,651: | Line 4,504: | ||
1000ᵗʰ 1001ˢᵗ 1002ⁿᵈ 1003ʳᵈ 1004ᵗʰ 1005ᵗʰ 1006ᵗʰ 1007ᵗʰ 1008ᵗʰ 1009ᵗʰ 1010ᵗʰ 1011ᵗʰ 1012ᵗʰ 1013ᵗʰ 1014ᵗʰ 1015ᵗʰ 1016ᵗʰ 1017ᵗʰ 1018ᵗʰ 1019ᵗʰ 1020ᵗʰ 1021ˢᵗ 1022ⁿᵈ 1023ʳᵈ 1024ᵗʰ 1025ᵗʰ |
1000ᵗʰ 1001ˢᵗ 1002ⁿᵈ 1003ʳᵈ 1004ᵗʰ 1005ᵗʰ 1006ᵗʰ 1007ᵗʰ 1008ᵗʰ 1009ᵗʰ 1010ᵗʰ 1011ᵗʰ 1012ᵗʰ 1013ᵗʰ 1014ᵗʰ 1015ᵗʰ 1016ᵗʰ 1017ᵗʰ 1018ᵗʰ 1019ᵗʰ 1020ᵗʰ 1021ˢᵗ 1022ⁿᵈ 1023ʳᵈ 1024ᵗʰ 1025ᵗʰ |
||
</pre> |
</pre> |
||
=={{header|SQL}}== |
|||
Oracle |
|||
<syntaxhighlight lang="sql"> |
|||
select level card, |
|||
to_char(to_date(level,'j'),'fmjth') ord |
|||
from dual |
|||
connect by level <= 15; |
|||
select to_char(to_date(5373485,'j'),'fmjth') |
|||
from dual; |
|||
</syntaxhighlight> |
|||
<pre> |
|||
CARD ORD |
|||
---------- ------------------------------ |
|||
1 1st |
|||
2 2nd |
|||
3 3rd |
|||
4 4th |
|||
5 5th |
|||
6 6th |
|||
7 7th |
|||
8 8th |
|||
9 9th |
|||
10 10th |
|||
11 11th |
|||
12 12th |
|||
13 13th |
|||
14 14th |
|||
15 15th |
|||
15 rows selected. |
|||
select to_char(to_date(5373485,'j'),'fmjth') |
|||
* |
|||
ERROR at line 1: |
|||
ORA-01854: julian date must be between 1 and 5373484 |
|||
</pre> |
|||
=={{header|Standard ML}}== |
|||
<syntaxhighlight lang="sml">local |
|||
val v = Vector.tabulate (10, fn 1 => "st" | 2 => "nd" | 3 => "rd" | _ => "th") |
|||
fun getSuffix x = |
|||
if 3 < x andalso x < 21 then "th" else Vector.sub (v, x mod 10) |
|||
in |
|||
fun nth n = |
|||
Int.toString n ^ getSuffix (n mod 100) |
|||
end |
|||
(* some test ouput *) |
|||
val () = (print o concat o List.tabulate) |
|||
(26, fn i => String.concatWith "\t" (map nth [i, i + 250, i + 1000]) ^ "\n")</syntaxhighlight> |
|||
{{out}} |
|||
<pre>0th 250th 1000th |
|||
1st 251st 1001st |
|||
2nd 252nd 1002nd |
|||
3rd 253rd 1003rd |
|||
4th 254th 1004th |
|||
5th 255th 1005th |
|||
6th 256th 1006th |
|||
7th 257th 1007th |
|||
8th 258th 1008th |
|||
9th 259th 1009th |
|||
10th 260th 1010th |
|||
11th 261st 1011th |
|||
12th 262nd 1012th |
|||
13th 263rd 1013th |
|||
14th 264th 1014th |
|||
15th 265th 1015th |
|||
16th 266th 1016th |
|||
17th 267th 1017th |
|||
18th 268th 1018th |
|||
19th 269th 1019th |
|||
20th 270th 1020th |
|||
21st 271st 1021st |
|||
22nd 272nd 1022nd |
|||
23rd 273rd 1023rd |
|||
24th 274th 1024th |
|||
25th 275th 1025th</pre> |
|||
=={{header|Stata}}== |
|||
We reuse here the '''maps''' function defined in the task [[Apply a callback to an array]]. |
|||
<syntaxhighlight lang="stata">mata |
|||
function maps(f,a) { |
|||
nr = rows(a) |
|||
nc = cols(a) |
|||
b = J(nr,nc,"") |
|||
for (i=1;i<=nr;i++) { |
|||
for (j=1;j<=nc;j++) b[i,j] = (*f)(a[i,j]) |
|||
} |
|||
return(b) |
|||
} |
|||
function nth(n) { |
|||
k = max((min((mod(n-1,10)+1,4)),4*(mod(n-10,100)<10))) |
|||
return(strofreal(n)+("st","nd","rd","th")[k]) |
|||
} |
|||
maps(&nth(),((0::25),(250::275),(1000::1025))) |
|||
end</syntaxhighlight> |
|||
'''Output:''' |
|||
<pre> 1 2 3 |
|||
+----------------------------+ |
|||
1 | 0th 250th 1000th | |
|||
2 | 1st 251st 1001st | |
|||
3 | 2nd 252nd 1002nd | |
|||
4 | 3rd 253rd 1003rd | |
|||
5 | 4th 254th 1004th | |
|||
6 | 5th 255th 1005th | |
|||
7 | 6th 256th 1006th | |
|||
8 | 7th 257th 1007th | |
|||
9 | 8th 258th 1008th | |
|||
10 | 9th 259th 1009th | |
|||
11 | 10th 260th 1010th | |
|||
12 | 11th 261st 1011th | |
|||
13 | 12th 262nd 1012th | |
|||
14 | 13th 263rd 1013th | |
|||
15 | 14th 264th 1014th | |
|||
16 | 15th 265th 1015th | |
|||
17 | 16th 266th 1016th | |
|||
18 | 17th 267th 1017th | |
|||
19 | 18th 268th 1018th | |
|||
20 | 19th 269th 1019th | |
|||
21 | 20th 270th 1020th | |
|||
22 | 21st 271st 1021st | |
|||
23 | 22nd 272nd 1022nd | |
|||
24 | 23rd 273rd 1023rd | |
|||
25 | 24th 274th 1024th | |
|||
26 | 25th 275th 1025th | |
|||
+----------------------------+</pre> |
|||
=={{header|Stringle}}== |
|||
<syntaxhighlight lang="stringle">#i |
|||
s "th" |
|||
.\#i "1" .:\#i !"1" s "st" |
|||
.\#i "2" .:\#i !"1" s "nd" |
|||
.\#i "3" .:\#i !"1" s "rd" |
|||
$ #i s |
|||
i i "." |
|||
#i +26 26 +#i #@i 250 |
|||
#i +266 266 +#i #@i 1000 |
|||
#i +1026 i "" |
|||
#i</syntaxhighlight> |
|||
=={{header|Swift}}== |
=={{header|Swift}}== |
||
< |
<syntaxhighlight lang="swift">func addSuffix(n:Int) -> String { |
||
if n % 100 / 10 == 1 { |
if n % 100 / 10 == 1 { |
||
return "th" |
return "th" |
||
Line 1,681: | Line 4,680: | ||
print("\(i)\(addSuffix(i)) ") |
print("\(i)\(addSuffix(i)) ") |
||
} |
} |
||
println()</ |
println()</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,690: | Line 4,689: | ||
=={{header|Tcl}}== |
=={{header|Tcl}}== |
||
< |
<syntaxhighlight lang="tcl">proc ordinal {n} { |
||
if {$n%100<10 || $n%100>20} { |
if {$n%100<10 || $n%100>20} { |
||
set suff [lindex {th st nd rd th th th th th th} [expr {$n % 10}]] |
set suff [lindex {th st nd rd th th th th th th} [expr {$n % 10}]] |
||
Line 1,704: | Line 4,703: | ||
} |
} |
||
puts $l |
puts $l |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,712: | Line 4,711: | ||
</pre> |
</pre> |
||
=={{header| |
== {{header|TypeScript}} == |
||
{{trans|Ada}} |
|||
<lang>For x = 0 to 25 ' Test range 0..25 |
|||
<syntaxhighlight lang="javascript"> |
|||
Push x : GoSub _PrintOrdinal |
|||
// N'th |
|||
Next x : Print |
|||
function suffix(n: number): string { |
|||
var nMod10: number = n % 10; |
|||
var nMod100: number = n % 100; |
|||
if (nMod10 == 1 && nMod100 != 11) |
|||
return "st"; |
|||
else if (nMod10 == 2 && nMod100 != 12) |
|||
return "nd"; |
|||
else if (nMod10 == 3 && nMod100 != 13) |
|||
return "rd"; |
|||
else |
|||
return "th"; |
|||
} |
|||
function printImages(loLim: number, hiLim: number) { |
|||
For x = 250 to 265 ' Test range 250..265 |
|||
for (i = loLim; i <= hiLim; i++) |
|||
Push x : GoSub _PrintOrdinal |
|||
process.stdout.write(`${i}` + suffix(i) + " "); |
|||
Next x : Print |
|||
process.stdout.write("\n"); |
|||
} |
|||
printImages( 0, 25); |
|||
printImages( 250, 265); |
|||
printImages(1000, 1025); |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
0th 1st 2nd 3rd 4th 5th 6th 7th 8th 9th 10th 11th 12th 13th 14th 15th 16th 17th 18th 19th 20th 21st 22nd 23rd 24th 25th |
|||
250th 251st 252nd 253rd 254th 255th 256th 257th 258th 259th 260th 261st 262nd 263rd 264th 265th |
|||
1000th 1001st 1002nd 1003rd 1004th 1005th 1006th 1007th 1008th 1009th 1010th 1011th 1012th 1013th 1014th 1015th 1016th 1017th 1018th 1019th 1020th 1021st 1022nd 1023rd 1024th 1025th |
|||
</pre> |
|||
=={{header|UNIX Shell}}== |
|||
For x = 1000 to 1025 ' Test range 1000..1025 |
|||
{{works with|Bourne Again Shell}} |
|||
Push x : GoSub _PrintOrdinal |
|||
<syntaxhighlight lang="sh">nth() { |
|||
Next x : Print |
|||
local ordinals=(th st nd rd) |
|||
local -i n=$1 i |
|||
if (( n < 0 )); then |
|||
printf '%s%s\n' - "$(nth $(( -n )) )" |
|||
return 0 |
|||
fi |
|||
case $(( n % 100 )) in |
|||
11|12|13) i=0;; |
|||
*) (( i= n%10 < 4 ? n%10 : 0 ));; |
|||
esac |
|||
printf '%d%s\n' "$n" "${ordinals[i]}" |
|||
} |
|||
for n in {0..25} {250..265} {1000..1025}; do |
|||
nth $n |
|||
done | column</syntaxhighlight> |
|||
{{Out}} |
|||
End ' End test program |
|||
<pre>0th 7th 14th 21st 252nd 259th 1000th 1007th 1014th 1021st |
|||
1st 8th 15th 22nd 253rd 260th 1001st 1008th 1015th 1022nd |
|||
2nd 9th 16th 23rd 254th 261st 1002nd 1009th 1016th 1023rd |
|||
3rd 10th 17th 24th 255th 262nd 1003rd 1010th 1017th 1024th |
|||
Print Using "____#";Tos();"'"; ' Print the number |
|||
4th 11th 18th 25th 256th 263rd 1004th 1011th 1018th 1025th |
|||
5th 12th 19th 250th 257th 264th 1005th 1012th 1019th |
|||
If (Tos()%100 > 10) * (Tos()%100 < 14) Then |
|||
6th 13th 20th 251st 258th 265th 1006th 1013th 1020th</pre> |
|||
Return ' We're done here |
|||
EndIf |
|||
=={{header|V (Vlang)}}== |
|||
Push Pop() % 10 ' Calculate n mod 10 |
|||
{{trans|go}} |
|||
GoSub 100 + 10 * ((Tos()>0) + (Tos()>1) + (Tos()>2) - (3 * (Pop()>3))) |
|||
<syntaxhighlight lang="v (vlang)">fn ord(n int) string { |
|||
Else ' And decide which ordinal to use |
|||
mut s := "th" |
|||
Print Pop();" is less than zero" ' Otherwise, it is an error |
|||
c := n % 10 |
|||
EndIf |
|||
if c in [1,2,3] { |
|||
if n%100/10 == 1 { |
|||
return "$n$s" |
|||
} |
|||
match c { |
|||
1 { |
|||
s = 'st' |
|||
} |
|||
2 { |
|||
s = 'nd' |
|||
} |
|||
3 { |
|||
s = 'rd' |
|||
} |
|||
else{} |
|||
} |
|||
} |
|||
return "$n$s" |
|||
} |
|||
fn main() { |
|||
for n := 0; n <= 25; n++ { |
|||
print("${ord(n)} ") |
|||
} |
|||
println('') |
|||
for n := 250; n <= 265; n++ { |
|||
print("${ord(n)} ") |
|||
} |
|||
println('') |
|||
for n := 1000; n <= 1025; n++ { |
|||
print("${ord(n)} ") |
|||
} |
|||
println('') |
|||
}</syntaxhighlight> |
|||
Return |
|||
' Select and print proper ordinal |
|||
100 Print "th"; : Return |
|||
110 Print "st"; : Return |
|||
120 Print "nd"; : Return |
|||
130 Print "rd"; : Return</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
0th 1st 2nd 3rd 4th 5th 6th 7th 8th 9th 10th 11th 12th 13th 14th 15th 16th 17th 18th 19th 20th 21st 22nd 23rd 24th 25th |
|||
0'th 1'st 2'nd 3'rd 4'th 5'th 6'th 7'th 8'th 9'th 10'th 11'th 12'th 13'th 14'th 15'th 16'th 17'th 18'th 19'th 20'th 21'st 22'nd 23'rd 24'th 25'th |
|||
250th 251st 252nd 253rd 254th 255th 256th 257th 258th 259th 260th 261st 262nd 263rd 264th 265th |
|||
250'th 251'st 252'nd 253'rd 254'th 255'th 256'th 257'th 258'th 259'th 260'th 261'st 262'nd 263'rd 264'th 265'th |
|||
1000th 1001st 1002nd 1003rd 1004th 1005th 1006th 1007th 1008th 1009th 1010th 1011th 1012th 1013th 1014th 1015th 1016th 1017th 1018th 1019th 1020th 1021st 1022nd 1023rd 1024th 1025th |
|||
1000'th 1001'st 1002'nd 1003'rd 1004'th 1005'th 1006'th 1007'th 1008'th 1009'th 1010'th 1011'th 1012'th 1013'th 1014'th 1015'th 1016'th 1017'th 1018'th 1019'th 1020'th 1021'st 1022'nd 1023'rd 1024'th 1025'th |
|||
</pre> |
|||
=={{header|VTL-2}}== |
|||
The subroutine at 2000 sets S and T to the first and second letters of the suffix for N. The subroutine at 3000 prints N and the suffix. |
|||
<syntaxhighlight lang="vtl2"> |
|||
1110 N=0 |
|||
1120 #=3000 |
|||
1130 N=N+1 |
|||
1140 #=N<26*1120 |
|||
1150 ?=" " |
|||
1210 N=250 |
|||
1220 #=3000 |
|||
1230 N=N+1 |
|||
1240 #=N<266*1220 |
|||
1250 ?=" " |
|||
1310 N=1000 |
|||
1320 #=3000 |
|||
1330 N=N+1 |
|||
1340 #=N<1026*1320 |
|||
1350 ?=" " |
|||
1400 #=9999 |
|||
2000 R=! |
|||
2010 S=116 |
|||
2020 T=104 |
|||
2030 M=N/100*0+% |
|||
2040 #=M<20+(M>4)=2*R |
|||
2050 M=N/10*0+% |
|||
2060 #=M=1=0*2100 |
|||
2070 S=115 |
|||
2080 T=116 |
|||
2090 #=R |
|||
2100 #=M=2=0*2140 |
|||
2110 S=110 |
|||
2120 T=100 |
|||
2130 #=R |
|||
2140 #=M=3=0*R |
|||
2150 S=114 |
|||
2160 T=100 |
|||
2170 #=R |
|||
3000 Z=! |
|||
3010 $=32 |
|||
3020 ?=N |
|||
3030 #=2000 |
|||
3040 $=S |
|||
3050 $=T |
|||
3060 #=Z |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
0th 1st 2nd 3rd 4th 5th 6th 7th 8th 9th 10th 11th 12th 13th 14th 15th 16th 17th 18th 19th 20th 21st 22nd 23rd 24th 25th |
|||
250th 251st 252nd 253rd 254th 255th 256th 257th 258th 259th 260th 261st 262nd 263rd 264th 265th |
|||
1000th 1001st 1002nd 1003rd 1004th 1005th 1006th 1007th 1008th 1009th 1010th 1011th 1012th 1013th 1014th 1015th 1016th 1017th 1018th 1019th 1020th 1021st 1022nd 1023rd 1024th 1025th |
|||
</pre> |
|||
=={{header|Wren}}== |
|||
{{libheader|Wren-fmt}} |
|||
<syntaxhighlight lang="wren">import "./fmt" for Fmt |
|||
var ranges = [ 0..25, 250..265, 1000..1025 ] |
|||
for (r in ranges) { |
|||
r.each { |i| Fmt.write("$r ", i) } |
|||
System.print("\n") |
|||
}</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
0th 1st 2nd 3rd 4th 5th 6th 7th 8th 9th 10th 11th 12th 13th 14th 15th 16th 17th 18th 19th 20th 21st 22nd 23rd 24th 25th |
|||
250th 251st 252nd 253rd 254th 255th 256th 257th 258th 259th 260th 261st 262nd 263rd 264th 265th |
|||
1000th 1001st 1002nd 1003rd 1004th 1005th 1006th 1007th 1008th 1009th 1010th 1011th 1012th 1013th 1014th 1015th 1016th 1017th 1018th 1019th 1020th 1021st 1022nd 1023rd 1024th 1025th |
|||
</pre> |
|||
=={{header|XLISP}}== |
|||
<syntaxhighlight lang="xlisp">(DEFUN NTH (N) |
|||
(COND |
|||
((AND (> (MOD N 100) 3) (< (MOD N 100) 21)) `(,N TH)) |
|||
((= (MOD N 10) 1) `(,N ST)) |
|||
((= (MOD N 10) 2) `(,N ND)) |
|||
((= (MOD N 10) 3) `(,N RD)) |
|||
(T `(,N TH)))) |
|||
(DEFUN RANGE (X Y) |
|||
(IF (<= X Y) |
|||
(CONS X (RANGE (+ X 1) Y)))) |
|||
(DEFUN TEST-NTH () |
|||
(DISPLAY (MAPCAR NTH (RANGE 1 25))) |
|||
(NEWLINE) |
|||
(DISPLAY (MAPCAR NTH (RANGE 250 265))) |
|||
(NEWLINE) |
|||
(DISPLAY (MAPCAR NTH (RANGE 1000 1025)))) |
|||
(TEST-NTH)</syntaxhighlight> |
|||
{{out}} |
|||
<pre>((1 ST) (2 ND) (3 RD) (4 TH) (5 TH) (6 TH) (7 TH) (8 TH) (9 TH) (10 TH) (11 TH) (12 TH) (13 TH) (14 TH) (15 TH) (16 TH) (17 TH) (18 TH) (19 TH) (20 TH) (21 ST) (22 ND) (23 RD) (24 TH) (25 TH)) |
|||
((250 TH) (251 ST) (252 ND) (253 RD) (254 TH) (255 TH) (256 TH) (257 TH) (258 TH) (259 TH) (260 TH) (261 ST) (262 ND) (263 RD) (264 TH) (265 TH)) |
|||
((1000 TH) (1001 ST) (1002 ND) (1003 RD) (1004 TH) (1005 TH) (1006 TH) (1007 TH) (1008 TH) (1009 TH) (1010 TH) (1011 TH) (1012 TH) (1013 TH) (1014 TH) (1015 TH) (1016 TH) (1017 TH) (1018 TH) (1019 TH) (1020 TH) (1021 ST) (1022 ND) (1023 RD) (1024 TH) (1025 TH))</pre> |
|||
=={{header|XPL0}}== |
|||
{{trans|Ada}} |
|||
{{works with|EXPL-32}} |
|||
<syntaxhighlight lang="xpl0"> |
|||
\N'th |
|||
code Rem=2, CrLf=9, IntIn=10, IntOut=11, Text=12; |
|||
procedure Suf(N, S); |
|||
integer N; |
|||
character S; |
|||
integer NMod10, NMod100; |
|||
begin |
|||
NMod10:= Rem(N/10); |
|||
NMod100:= Rem(N/100); |
|||
case of |
|||
NMod10 = 1 & NMod100 # 11: S(0):= "st"; |
|||
NMod10 = 2 & NMod100 # 12: S(0):= "nd"; |
|||
NMod10 = 3 & NMod100 # 13: S(0):= "rd" |
|||
other |
|||
S(0):= "th"; |
|||
end; |
|||
procedure PrintImages(LoLim, HiLim); |
|||
integer LoLim, HiLim; |
|||
integer I; |
|||
character S; |
|||
begin |
|||
for I:= LoLim, HiLim do |
|||
begin |
|||
Suf(I, addr S); |
|||
IntOut(0, I); Text(0, S); Text(0, " ") |
|||
end; |
|||
CrLf(0) |
|||
end; |
|||
begin |
|||
PrintImages(0, 25); |
|||
PrintImages(250, 265); |
|||
PrintImages(1000, 1025) |
|||
end |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
0th 1st 2nd 3rd 4th 5th 6th 7th 8th 9th 10th 11th 12th 13th 14th 15th 16th 17th 18th 19th 20th 21st 22nd 23rd 24th 25th |
|||
250th 251st 252nd 253rd 254th 255th 256th 257th 258th 259th 260th 261st 262nd 263rd 264th 265th |
|||
1000th 1001st 1002nd 1003rd 1004th 1005th 1006th 1007th 1008th 1009th 1010th 1011th 1012th 1013th 1014th 1015th 1016th 1017th 1018th 1019th 1020th 1021st 1022nd 1023rd 1024th 1025th |
|||
</pre> |
</pre> |
||
=={{header|zkl}}== |
=={{header|zkl}}== |
||
Two versions, your choice |
Two versions, your choice |
||
< |
<syntaxhighlight lang="zkl">#if 0 |
||
fcn addSuffix(n){ |
fcn addSuffix(n){ |
||
z:=n.abs()%100; |
z:=n.abs()%100; |
||
Line 1,770: | Line 4,981: | ||
String(n,(z<=10 or z>20) and suffixes[z%10] or "th"); |
String(n,(z<=10 or z>20) and suffixes[z%10] or "th"); |
||
} |
} |
||
#endif</ |
#endif</syntaxhighlight> |
||
< |
<syntaxhighlight lang="zkl">[0..25] .apply(addSuffix).concat(",").println(); |
||
[250..265] .apply(addSuffix).concat(",").println(); |
[250..265] .apply(addSuffix).concat(",").println(); |
||
[1000..1025].apply(addSuffix).concat(",").println();</ |
[1000..1025].apply(addSuffix).concat(",").println();</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
Latest revision as of 17:54, 7 July 2024
You are encouraged to solve this task according to the task description, using any language you may know.
Write a function/method/subroutine/... that when given an integer greater than or equal to zero returns a string of the number followed by an apostrophe then the ordinal suffix.
- Example
Returns would include 1'st 2'nd 3'rd 11'th 111'th 1001'st 1012'th
- Task
Use your routine to show here the output for at least the following (inclusive) ranges of integer inputs:
0..25, 250..265, 1000..1025
Note: apostrophes are now optional to allow correct apostrophe-less English.
11l
V _suffix = [‘th’, ‘st’, ‘nd’, ‘rd’, ‘th’, ‘th’, ‘th’, ‘th’, ‘th’, ‘th’]
F nth(n)
R ‘#.'#.’.format(n, I n % 100 <= 10 | n % 100 > 20 {:_suffix[n % 10]} E ‘th’)
L(j) (0..1000).step(250)
print_elements(Array(j.+25).map(i -> nth(i)))
- Output:
0'th 1'st 2'nd 3'rd 4'th 5'th 6'th 7'th 8'th 9'th 10'th 11'th 12'th 13'th 14'th 15'th 16'th 17'th 18'th 19'th 20'th 21'st 22'nd 23'rd 24'th 250'th 251'st 252'nd 253'rd 254'th 255'th 256'th 257'th 258'th 259'th 260'th 261'st 262'nd 263'rd 264'th 265'th 266'th 267'th 268'th 269'th 270'th 271'st 272'nd 273'rd 274'th 500'th 501'st 502'nd 503'rd 504'th 505'th 506'th 507'th 508'th 509'th 510'th 511'th 512'th 513'th 514'th 515'th 516'th 517'th 518'th 519'th 520'th 521'st 522'nd 523'rd 524'th 750'th 751'st 752'nd 753'rd 754'th 755'th 756'th 757'th 758'th 759'th 760'th 761'st 762'nd 763'rd 764'th 765'th 766'th 767'th 768'th 769'th 770'th 771'st 772'nd 773'rd 774'th 1000'th 1001'st 1002'nd 1003'rd 1004'th 1005'th 1006'th 1007'th 1008'th 1009'th 1010'th 1011'th 1012'th 1013'th 1014'th 1015'th 1016'th 1017'th 1018'th 1019'th 1020'th 1021'st 1022'nd 1023'rd 1024'th
68000 Assembly
The function itself:
Nth:
MOVEQ #1,D1
.loop:
MOVE.L D1,-(SP)
MOVE.L D0,-(SP)
MOVE.B (2,SP),D0
JSR printhex
MOVE.B (3,SP),D0
JSR printhex
MOVE.L (SP)+,D0
MOVE.L D0,-(SP)
AND.W #$00FF,D0
;HANDLE SPECIAL CASES
CMP.B #$11,D0
BEQ .fourth
CMP.B #$12,D0
BEQ .fourth
CMP.B #$13,D0
BEQ .fourth
;HANDLE THE REST
AND.W #$000F,D0
CMP.B #1,D0
BEQ .first
CMP.B #2,D0
BEQ .second
CMP.B #3,D0
BEQ .third
.fourth:
LEA th,A3
bra .print
.third:
LEA rd,a3
bra .print
.second:
LEA nd,a3
bra .print
.first:
LEA est,a3
.print:
JSR PrintString
JSR newline
MOVE.L (SP)+,d0
MOVE.L (SP)+,d1
ABCD D1,D0
DBRA D7,.loop
rts
th:
dc.b "th",255
even
est:
dc.b "st",255
even
nd:
dc.b "nd",255
even
rd:
dc.b "rd",255
even
And the test cases (each was executed in separate builds of the Sega Genesis cartridge to have enough room to see them all at once)
MOVE.w #0,D0
MOVE.W #25,D7
JSR Nth
MOVE.w #$250,D0 ;since we're working with binary-coded decimal, this number is stored as hex.
MOVE.W #15,D7
JSR Nth
MOVE.w #$1000,D0 ;since we're working with binary-coded decimal, this number is stored as hex.
MOVE.W #25,D7
JSR Nth
jmp * ;stop the cpu - we're done.
- Output:
0th 1st 2nd 3rd 4th 5th 6th 7th 8th 9th 10th 11th 12th 13th 14th 15th 16th 17th 18th 19th 20th 21st 22nd 23rd 24th 25th 250th 251st 252nd 253rd 254th 255th 256th 257th 258th 259th 260th 261st 262nd 263rd 264th 265th 1000th 1001st 1002nd 1003rd 1004th 1005th 1006th 1007th 1008th 1009th 1010th 1011th 1012th 1013th 1014th 1015th 1016th 1017th 1018th 1019th 1020th 1021st 1022nd 1023rd 1024th 1025th
8080 Assembly
org 100h
jmp demo
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Given a 16-bit unsigned integer in HL, return a string
;; consisting of its ASCII representation plus an ordinal
;; suffix in HL.
nth: lxi b,-10 ; Divisor for digit loop
push h ; Push integer to stack
lxi h,nthsfx ; Pointer to end of digit area
xthl ; Swap digit pointer with integer
nthdigit: lxi d,-1 ; Get current digit - DE holds quotient
nthdiv10: inx d ; Increment quotient,
dad b ; subtract 10 from integer,
jc nthdiv10 ; Keep going until HL<0
mvi a,'0'+10 ; Calculate ASCII value for digit
add l ; Modulus is in (H)L
xthl ; Swap integer with pointer
dcx h ; Point to one digit further left
mov m,a ; Store the digit
xthl ; Swap pointer with integer
xchg ; Put quotient in HL
mov a,h ; Is it zero?
ora l
jnz nthdigit ; If not, go calculate next digit
mvi e,0 ; Default suffix is 'th'.
lxi h,nthnum+3 ; Look at tens digit
mov a,m
cpi '1' ; Is it '1'?
jz nthsetsfx ; Then it is always 'th'.
inx h ; Look at zeroes digit
mov a,m
sui '0' ; Subtract ASCII '0'
cpi 4 ; 4 or higher?
jnc nthsetsfx ; Then it is always 'th'.
rlc ; Otherwise, suffix is at N*2+nthord
mov e,a
nthsetsfx: mvi d,0 ; Look up suffix in list
lxi h,nthord
dad d ; Pointer to suffix in HL
lxi d,nthsfx ; Pointer to space for suffix in DE
mov a,m ; Get first letter of suffix
stax d ; Store it in output
inx h ; Increment both pointers
inx d
mov a,m ; Get second letter of suffix
stax d ; Store it in output
pop h ; Return pointer to leftmost digit
ret
nthord: db 'thstndrd' ; Ordinal suffixes
nthnum: db '*****' ; Room for digits
nthsfx: db '**$' ; Room for suffix
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
demo: ;; Demo code
lxi h,0 ; Starting at 0...
mvi b,26 ; ...print 26 numbers [0..25]
call printnums
lxi h,250 ; Starting at 250...
mvi b,16 ; ...print 16 numbers [250..265]
call printnums
lxi h,1000 ; Starting at 1000...
mvi b,26 ; ...print 26 numbers [1000..1025]
;; Print values from HL up to HL+B
printnums: push h ; Keep number
push b ; Keep counter
call nth ; Get string for current HL
xchg ; Put it in DE
mvi c,9 ; CP/M print string
call 5
mvi e,' ' ; Separate numbers with spaces
mvi c,2 ; CP/M print character
call 5
pop b ; Restore counter
pop h ; Restore number
inx h ; Increment number
dcr b ; Decrement counter
jnz printnums ; If not zero, print next number
ret
- Output:
Line breaks added for clarity.
0th 1st 2nd 3rd 4th 5th 6th 7th 8th 9th 10th 11th 12th 13th 14th 15th 16th 17th 18th 19th 20th 21st 22nd 23rd 24th 25th 250th 251st 252nd 253rd 254th 255th 256th 257th 258th 259th 260th 261st 262nd 263rd 264th 265th 1000th 1001st 1002nd 1003rd 1004th 1005th 1006th 1007th 1008th 1009th 1010th 1011th 1012th 1013th 1014th 1015th 1016th 1017th 1018th 1019th 1020th 1021st 1022nd 1023rd 1024th 1025th
8086 Assembly
bits 16
cpu 8086
segment .text
org 100h
jmp demo
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Given a number in AX, return string with ordinal suffix
;; in DX.
nth: mov cx,10 ; Divisor
mov bx,.sfx ; Pointer to end of number
.digit: xor dx,dx ; Zero DX
div cx ; AX = DX:AX/10; DX=remainder
add dl,'0' ; Make digit
dec bx ; Back up the pointer
mov [bx],dl ; Store digit
and ax,ax ; Done yet? (AX=0?)
jnz .digit ; If not, get next digit
mov dx,bx ; Keep string pointer in DX
xor bx,bx ; Default suffix is 'th'.
cmp byte [.num+3],'1' ; Is the tens digit '1'?
je .setsfx ; Then the suffix is 'th'.
mov cl,[.num+4] ; Get the ones digit
cmp cl,'4' ; Is it '4' or higher?
jae .setsfx ; Then the suffix is 'th'.
mov bl,cl
sub bl,'0' ; Calculate offset.
shl bl,1 ; [0..3]*2 + .ord
.setsfx: mov ax,[bx+.ordsfx] ; Set suffix
mov [.sfx],ax
ret
.ordsfx: db 'thstndrd'
.num: db '*****'
.sfx: db '**$'
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Print numbers.
demo: mov ax,0 ; Starting at 0...
mov bl,26 ; ...print 26 numbers [0..25]
call printn
mov ax,250 ; Starting at 250...
mov bl,16 ; ...print 16 numbers [250..265]
call printn
mov ax,1000 ; Starting at 1000...
mov bl,26 ; ...print 26 numbers [1000..1025]
;; Print BL numbers starting at AX
printn: push ax ; Keep number
push bx ; Keep counter
call nth ; Get string for current AX
mov ah,9 ; MS-DOS print string
int 21h
mov dl,' ' ; Separate numbers by spaces
mov ah,2 ; MS-DOS print character
int 21h
pop bx ; Restore counter
pop ax ; Restore number
inc ax ; Next number
dec bl ; Are we done yet?
jnz printn
ret
- Output:
Line breaks added for clarity.
0th 1st 2nd 3rd 4th 5th 6th 7th 8th 9th 10th 11th 12th 13th 14th 15th 16th 17th 18th 19th 20th 21st 22nd 23rd 24th 25th 250th 251st 252nd 253rd 254th 255th 256th 257th 258th 259th 260th 261st 262nd 263rd 264th 265th 1000th 1001st 1002nd 1003rd 1004th 1005th 1006th 1007th 1008th 1009th 1010th 1011th 1012th 1013th 1014th 1015th 1016th 1017th 1018th 1019th 1020th 1021st 1022nd 1023rd 1024th 1025th
Action!
PROC Nth(CARD val,CHAR ARRAY s)
CHAR ARRAY sfx
BYTE d
StrC(val,s)
s(0)=s(0)+1
s(s(0))=''
d=val MOD 100
IF d>10 AND d<14 THEN
sfx="th"
ELSE
d=val MOD 10
IF d=1 THEN sfx="st"
ELSEIF d=2 THEN sfx="nd"
ELSEIF d=3 THEN sfx="rd"
ELSE sfx="th"
FI
FI
s(0)=s(0)+2
SAssign(s,sfx,s(0)-1,s(0)+1)
RETURN
PROC Main()
CARD ARRAY n=[0 250 1000]
CHAR ARRAY s(10)
CARD i,j
FOR i=0 TO 2
DO
FOR j=n(i) TO n(i)+25
DO
Nth(j,s)
PrintF("%S ",s)
OD
PutE() PutE()
OD
RETURN
- Output:
Screenshot from Atari 8-bit computer
0'th 1'st 2'nd 3'rd 4'th 5'th 6'th 7'th 8'th 9'th 10'th 11'th 12'th 13'th 14'th 15'th 16'th 17'th 18'th 19'th 20'th 21'st 22'nd 23'rd 24'th 25'th 250'th 251'st 252'nd 253'rd 254'th 255'th 256'th 257'th 258'th 259'th 260'th 261'st 262'nd 263'rd 264'th 265'th 266'th 267'th 268'th 269'th 270'th 271'st 272'nd 273'rd 274'th 275'th 1000'th 1001'st 1002'nd 1003'rd 1004'th 1005'th 1006'th 1007'th 1008'th 1009'th 1010'th 1011'th 1012'th 1013'th 1014'th 1015'th 1016'th 1017'th 1018'th 1019'th 1020'th 1021'st 1022'nd 1023'rd 1024'th 1025'th
Ada
with Ada.Text_IO;
procedure Nth is
function Suffix(N: Natural) return String is
begin
if N mod 10 = 1 and then N mod 100 /= 11 then return "st";
elsif N mod 10 = 2 and then N mod 100 /= 12 then return "nd";
elsif N mod 10 = 3 and then N mod 100 /= 13 then return "rd";
else return "th";
end if;
end Suffix;
procedure Print_Images(From, To: Natural) is
begin
for I in From .. To loop
Ada.Text_IO.Put(Natural'Image(I) & Suffix(I));
end loop;
Ada.Text_IO.New_Line;
end Print_Images;
begin
Print_Images( 0, 25);
Print_Images( 250, 265);
Print_Images(1000, 1025);
end Nth;
- Output:
0th 1st 2nd 3rd 4th 5th 6th 7th 8th 9th 10th 11th 12th 13th 14th 15th 16th 17th 18th 19th 20th 21st 22nd 23rd 24th 25th 250th 251st 252nd 253rd 254th 255th 256th 257th 258th 259th 260th 261st 262nd 263rd 264th 265th 1000th 1001st 1002nd 1003rd 1004th 1005th 1006th 1007th 1008th 1009th 1010th 1011th 1012th 1013th 1014th 1015th 1016th 1017th 1018th 1019th 1020th 1021st 1022nd 1023rd 1024th 1025th
ALGOL 68
# PROC to suffix a number with st, nd, rd or th as appropriate #
PROC nth = ( INT number )STRING:
BEGIN
INT number mod 100 = number MOD 100;
# RESULT #
whole( number, 0 )
+ IF number mod 100 >= 10 AND number mod 100 <= 20
THEN
# numbers in the range 10 .. 20 always have "th" #
"th"
ELSE
# not in the range 10 .. 20, suffix is st, nd, rd or th #
# depending on the final digit #
CASE number MOD 10
IN # 1 # "st"
, # 2 # "nd"
, # 3 # "rd"
OUT "th"
ESAC
FI
END; # nth #
# PROC to test nth, displays nth for all numbers in the range from .. to #
PROC test nth = ( INT from, INT to )VOID:
BEGIN
INT test count := 0;
FOR test value FROM from TO to
DO
STRING test result = nth( test value );
print( ( " "[ 1 : 8 - UPB test result ], nth( test value ) ) );
test count +:= 1;
IF test count MOD 8 = 0
THEN
print( ( newline ) )
FI
OD;
print( ( newline ) )
END; # test nth #
main: (
test nth( 0, 25 );
test nth( 250, 265 );
test nth( 1000, 1025 )
)
- Output:
0th 1st 2nd 3rd 4th 5th 6th 7th 8th 9th 10th 11th 12th 13th 14th 15th 16th 17th 18th 19th 20th 21st 22nd 23rd 24th 25th 250th 251st 252nd 253rd 254th 255th 256th 257th 258th 259th 260th 261st 262nd 263rd 264th 265th 1000th 1001st 1002nd 1003rd 1004th 1005th 1006th 1007th 1008th 1009th 1010th 1011th 1012th 1013th 1014th 1015th 1016th 1017th 1018th 1019th 1020th 1021st 1022nd 1023rd 1024th 1025th
ALGOL W
begin % suffix number with st, nd, rd or th as appropriate %
string(2) procedure ordinalSuffix ( integer value number ) ;
begin
integer numberRem100;
numberRem100 := number rem 100;
if numberRem100 >= 10 and numberRem100 <= 20 then begin
% numbers in the range 10 .. 20 always have "th" %
"th"
end
else begin
% not in the range 10 .. 20, suffix is st, nd, rd or th %
% depending on the final digit %
integer numberRem10;
numberRem10 := number rem 10;
if numberRem10 = 1 then "st"
else if numberRem10 = 2 then "nd"
else if numberRem10 = 3 then "rd"
else "th"
end if_numberRem100_in_10_to_20__
end ordinalSuffix ;
% tests ordinalSuffix, displays the suffix for all numbers in from .. to %
procedure testSuffix ( integer value from, to ) ;
begin
integer count;
count := 0;
for testValue := from until to do begin
writeon( i_w := 4, s_w := 0, " ", testValue, ordinalSuffix( testValue ) );
count := count + 1;
if count rem 8 = 0 then write()
end for_testValue ;
if count rem 8 not = 0 then write();
write()
end testSuffix ;
begin % task %
testSuffix( 0, 25 );
testSuffix( 250, 265 );
testSuffix( 1000, 1025 )
end
end.
- Output:
0th 1st 2nd 3rd 4th 5th 6th 7th 8th 9th 10th 11th 12th 13th 14th 15th 16th 17th 18th 19th 20th 21st 22nd 23rd 24th 25th 250th 251st 252nd 253rd 254th 255th 256th 257th 258th 259th 260th 261st 262nd 263rd 264th 265th 1000th 1001st 1002nd 1003rd 1004th 1005th 1006th 1007th 1008th 1009th 1010th 1011th 1012th 1013th 1014th 1015th 1016th 1017th 1018th 1019th 1020th 1021st 1022nd 1023rd 1024th 1025th
APL
nth←{
sfx←4 2⍴'stndrdth'
tens←(10<100|⍵)∧20>100|⍵
(⍕⍵),sfx[(4×tens)⌈(⍳3)⍳10|⍵;]
}
- Output:
2 13⍴ nth¨ 0,⍳25 0th 1st 2nd 3rd 4th 5th 6th 7th 8th 9th 10th 11th 12th 13th 14th 15th 16th 17th 18th 19th 20th 21st 22nd 23rd 24th 25th 2 7⍴ nth¨ 250+0,⍳15 250th 251st 252nd 253rd 254th 255th 256th 257th 258th 259th 260th 261st 262nd 263rd 2 13⍴ nth¨ 1000+0,⍳25 1000th 1001st 1002nd 1003rd 1004th 1005th 1006th 1007th 1008th 1009th 1010th 1011th 1012th 1013th 1014th 1015th 1016th 1017th 1018th 1019th 1020th 1021st 1022nd 1023rd 1024th 1025th
AppleScript
-- ORDINAL STRINGS -----------------------------------------------------------
-- ordinalString :: Int -> String
on ordinalString(n)
(n as string) & ordinalSuffix(n)
end ordinalString
-- ordinalSuffix :: Int -> String
on ordinalSuffix(n)
set modHundred to n mod 100
if (11 ≤ modHundred) and (13 ≥ modHundred) then
"th"
else
item ((n mod 10) + 1) of ¬
{"th", "st", "nd", "rd", "th", "th", "th", "th", "th", "th"}
end if
end ordinalSuffix
-- TEST ----------------------------------------------------------------------
on run
-- showOrdinals :: [Int] -> [String]
script showOrdinals
on |λ|(lstInt)
map(ordinalString, lstInt)
end |λ|
end script
map(showOrdinals, ¬
map(uncurry(enumFromTo), ¬
[[0, 25], [250, 265], [1000, 1025]]))
end run
-- GENERIC FUNCTIONS ---------------------------------------------------------
-- enumFromTo :: Int -> Int -> [Int]
on enumFromTo(m, n)
if m > n then
set d to -1
else
set d to 1
end if
set lst to {}
repeat with i from m to n by d
set end of lst to i
end repeat
return lst
end enumFromTo
-- map :: (a -> b) -> [a] -> [b]
on map(f, xs)
tell mReturn(f)
set lng to length of xs
set lst to {}
repeat with i from 1 to lng
set end of lst to |λ|(item i of xs, i, xs)
end repeat
return lst
end tell
end map
-- Lift 2nd class handler function into 1st class script wrapper
-- mReturn :: Handler -> Script
on mReturn(f)
if class of f is script then
f
else
script
property |λ| : f
end script
end if
end mReturn
-- uncurry :: Handler (a -> b -> c) -> Script |λ| ((a, b) -> c)
on uncurry(f)
script
on |λ|(xy)
set {x, y} to xy
mReturn(f)'s |λ|(x, y)
end |λ|
end script
end uncurry
- Output:
{{"0th", "1st", "2nd", "3rd", "4th", "5th", "6th", "7th", "8th", "9th", "10th", "11th", "12th", "13th", "14th", "15th", "16th", "17th", "18th", "19th", "20th", "21st", "22nd", "23rd", "24th", "25th"}, {"250th", "251st", "252nd", "253rd", "254th", "255th", "256th", "257th", "258th", "259th", "260th", "261st", "262nd", "263rd", "264th", "265th"}, {"1000th", "1001st", "1002nd", "1003rd", "1004th", "1005th", "1006th", "1007th", "1008th", "1009th", "1010th", "1011th", "1012th", "1013th", "1014th", "1015th", "1016th", "1017th", "1018th", "1019th", "1020th", "1021st", "1022nd", "1023rd", "1024th", "1025th"}}
Arturo
suffixes: ["th" "st" "nd" "rd" "th" "th" "th" "th" "th" "th"]
nth: function [n][
if? or? 100 >= n%100
20 < n%100
-> (to :string n) ++ "'" ++ suffixes\[n%10]
else -> (to :string n) ++ "'th"
]
loop range.step:250 0 1000 'j [
loop j..j+24 'i ->
prints (nth i)++" "
print ""
]
- Output:
0'th 1'st 2'nd 3'rd 4'th 5'th 6'th 7'th 8'th 9'th 10'th 11'st 12'nd 13'rd 14'th 15'th 16'th 17'th 18'th 19'th 20'th 21'st 22'nd 23'rd 24'th 250'th 251'st 252'nd 253'rd 254'th 255'th 256'th 257'th 258'th 259'th 260'th 261'st 262'nd 263'rd 264'th 265'th 266'th 267'th 268'th 269'th 270'th 271'st 272'nd 273'rd 274'th 500'th 501'st 502'nd 503'rd 504'th 505'th 506'th 507'th 508'th 509'th 510'th 511'st 512'nd 513'rd 514'th 515'th 516'th 517'th 518'th 519'th 520'th 521'st 522'nd 523'rd 524'th 750'th 751'st 752'nd 753'rd 754'th 755'th 756'th 757'th 758'th 759'th 760'th 761'st 762'nd 763'rd 764'th 765'th 766'th 767'th 768'th 769'th 770'th 771'st 772'nd 773'rd 774'th 1000'th 1001'st 1002'nd 1003'rd 1004'th 1005'th 1006'th 1007'th 1008'th 1009'th 1010'th 1011'st 1012'nd 1013'rd 1014'th 1015'th 1016'th 1017'th 1018'th 1019'th 1020'th 1021'st 1022'nd 1023'rd 1024'th
AutoHotkey
for k, v in [[0, 25], [250, 265], [1000, 1025]] {
while v[1] <= v[2] {
Out .= Ordinal(v[1]) " "
v[1]++
}
Out .= "`n"
}
MsgBox, % Out
Ordinal(n) {
s2 := Mod(n, 100)
if (s2 > 10 && s2 < 14)
return n "th"
s1 := Mod(n, 10)
return n (s1 = 1 ? "st" : s1 = 2 ? "nd" : s1 = 3 ? "rd" : "th")
}
- Output:
0th 1st 2nd 3rd 4th 5th 6th 7th 8th 9th 10th 11th 12th 13th 14th 15th 16th 17th 18th 19th 20th 21st 22nd 23rd 24th 25th 250th 251st 252nd 253rd 254th 255th 256th 257th 258th 259th 260th 261st 262nd 263rd 264th 265th 1000th 1001st 1002nd 1003rd 1004th 1005th 1006th 1007th 1008th 1009th 1010th 1011th 1012th 1013th 1014th 1015th 1016th 1017th 1018th 1019th 1020th 1021st 1022nd 1023rd 1024th 1025th
AWK
# syntax: GAWK -f NTH.AWK
BEGIN {
prn(0,25)
prn(250,265)
prn(1000,1025)
exit(0)
}
function prn(start,stop, i) {
printf("%d-%d: ",start,stop)
for (i=start; i<=stop; i++) {
printf("%d%s ",i,nth(i))
}
printf("\n")
}
function nth(yearday, nthday) {
if (yearday ~ /1[1-3]$/) { # 11th,12th,13th
nthday = "th"
}
else if (yearday ~ /1$/) { # 1st,21st,31st,etc.
nthday = "st"
}
else if (yearday ~ /2$/) { # 2nd,22nd,32nd,etc.
nthday = "nd"
}
else if (yearday ~ /3$/) { # 3rd,23rd,33rd,etc.
nthday = "rd"
}
else if (yearday ~ /[0456789]$/) { # 4th-10th,20th,24th-30th,etc.
nthday = "th"
}
return(nthday)
}
- Output:
0-25: 0th 1st 2nd 3rd 4th 5th 6th 7th 8th 9th 10th 11th 12th 13th 14th 15th 16th 17th 18th 19th 20th 21st 22nd 23rd 24th 25th 250-265: 250th 251st 252nd 253rd 254th 255th 256th 257th 258th 259th 260th 261st 262nd 263rd 264th 265th 1000-1025: 1000th 1001st 1002nd 1003rd 1004th 1005th 1006th 1007th 1008th 1009th 1010th 1011th 1012th 1013th 1014th 1015th 1016th 1017th 1018th 1019th 1020th 1021st 1022nd 1023rd 1024th 1025th
Babel
((irregular ("st" "nd" "rd"))
(main
{(0 250 1000)
{ test ! "\n" << }
each})
(test {
<-
{iter 1 - -> dup <- + ordinalify ! <<
{iter 10 %} {" "} {"\n"} ifte << }
26 times})
(ordinalify {
<-
{{ -> dup <- 100 % 10 cugt } !
{ -> dup <- 100 % 14 cult } !
and not
{ -> dup <- 10 % 0 cugt } !
{ -> dup <- 10 % 4 cult } !
and
and}
{ -> dup
<- %d "'"
irregular -> 10 % 1 - ith
. . }
{ -> %d "'th" . }
ifte }))
- Output:
25'th 24'th 23'rd 22'nd 21'st 20'th 19'th 18'th 17'th 16'th 15'th 14'th 13'th 12'th 11'th 10'th 9'th 8'th 7'th 6'th 5'th 4'th 3'rd 2'nd 1'st 0'th 275'th 274'th 273'rd 272'nd 271'st 270'th 269'th 268'th 267'th 266'th 265'th 264'th 263'rd 262'nd 261'st 260'th 259'th 258'th 257'th 256'th 255'th 254'th 253'rd 252'nd 251'st 250'th 1025'th 1024'th 1023'rd 1022'nd 1021'st 1020'th 1019'th 1018'th 1017'th 1016'th 1015'th 1014'th 1013'th 1012'th 1011'th 1010'th 1009'th 1008'th 1007'th 1006'th 1005'th 1004'th 1003'rd 1002'nd 1001'st 1000'th
BASIC
ANSI BASIC
100 REM Nth
110 DECLARE EXTERNAL FUNCTION Suffix$
120 DECLARE EXTERNAL SUB PrintImages
130 CALL PrintImages(0, 25)
140 CALL PrintImages(250, 265)
150 CALL PrintImages(1000, 1025)
160 END
170 REM
180 EXTERNAL SUB PrintImages(LoLim, HiLim)
190 FOR I = LoLim TO HiLim
200 PRINT STR$(I); Suffix$(I); " ";
210 NEXT I
220 PRINT
230 END SUB
240 REM
250 EXTERNAL FUNCTION Suffix$(N)
260 LET NMod10 = MOD(N, 10)
270 LET NMod100 = MOD(N, 100)
280 IF NMod10 = 1 AND NMod100 <> 11 THEN
290 LET Suffix$ = "st"
300 ELSEIF NMod10 = 2 AND NMod100 <> 12 THEN
310 LET Suffix$ = "nd"
320 ELSEIF NMod10 = 3 AND NMod100 <> 13 THEN
330 LET Suffix$ = "rd"
340 ELSE
350 LET Suffix$ = "th"
360 END IF
370 END FUNCTION
- Output:
0th 1st 2nd 3rd 4th 5th 6th 7th 8th 9th 10th 11th 12th 13th 14th 15th 16th 17th 18th 19th 20th 21st 22nd 23rd 24th 25th 250th 251st 252nd 253rd 254th 255th 256th 257th 258th 259th 260th 261st 262nd 263rd 264th 265th 1000th 1001st 1002nd 1003rd 1004th 1005th 1006th 1007th 1008th 1009th 1010th 1011th 1012th 1013th 1014th 1015th 1016th 1017th 1018th 1019th 1020th 1021st 1022nd 1023rd 1024th 1025th
Applesoft BASIC
0 OP = 1
10 FOR N = 0 TO 25 : GOSUB 100 : NEXT
20 FOR N = 250 TO 265 : GOSUB 100 : NEXT
30 FOR N = 1000 TO 1025 : GOSUB 100 : NEXT
40 END
100 GOSUB 200"NTH
110 PRINT NTH$ " ";
120 RETURN
200 M1 = N - INT(N / 10) * 10
210 M2 = N - INT(N / 100) * 100
220 NTH$ = "TH"
230 IF M1 = 1 AND M2 <> 11 THEN NTH$ = "ST"
240 IF M1 = 2 AND M2 <> 12 THEN NTH$ = "ND"
250 IF M1 = 3 AND M2 <> 13 THEN NTH$ = "RD"
260 IF NOT OP THEN NTH$ = "'" + NTH$
270 NTH$ = STR$(N) + NTH$
280 RETURN
- Output:
0'TH 1'ST 2'ND 3'RD 4'TH 5'TH 6'TH 7'TH 8'TH 9'TH 10'TH 11'TH 12'TH 13'TH 14'TH 15'TH 16'TH 17'TH 18'TH 19'TH 20'TH 21'ST 22'ND 23'RD 24'TH 25'TH 250'TH 251'ST 252'ND 253'RD 254'TH 255'TH 256'TH 257'TH 258'TH 259'TH 260'TH 261'ST 262'ND 263'RD 264'TH 265'TH 1000'TH 1001'ST 1002'ND 1003'RD 1004'TH 1005'TH 1006'TH 1007'TH 1008'TH 1009'TH 1010'TH 1011'TH 1012'TH 1013'TH 1014'TH 1015'TH 1016'TH 1017'TH 1018'TH 1019'TH 1020'TH 1021'ST 1022'ND 1023'RD 1024'TH 1025'TH
BaCon
' Nth (sans apostrophes)
FUNCTION nth$(NUMBER n) TYPE STRING
LOCAL suffix
IF n < 0 THEN RETURN STR$(n)
IF MOD(n, 100) >= 11 AND MOD(n, 100) <= 13 THEN
suffix = "th"
ELSE
suffix = MID$("thstndrdthththththth", MOD(n, 10) * 2 + 1, 2)
ENDIF
RETURN CONCAT$(STR$(n), suffix)
END FUNCTION
' Test a few ranges
FOR i = 1 TO 4
READ first, last
per = 1
FOR n = first TO last
PRINT nth$(n) FORMAT "%s "
' limit to 10 entries per line
IF per = 10 OR n = last THEN
per = 1
PRINT
ELSE
INCR per
ENDIF
NEXT
NEXT
DATA 0, 25, 250, 265, 1000, 1025, -20, -11
- Output:
prompt$ ./nth 0th 1st 2nd 3rd 4th 5th 6th 7th 8th 9th 10th 11th 12th 13th 14th 15th 16th 17th 18th 19th 20th 21st 22nd 23rd 24th 25th 250th 251st 252nd 253rd 254th 255th 256th 257th 258th 259th 260th 261st 262nd 263rd 264th 265th 1000th 1001st 1002nd 1003rd 1004th 1005th 1006th 1007th 1008th 1009th 1010th 1011th 1012th 1013th 1014th 1015th 1016th 1017th 1018th 1019th 1020th 1021st 1022nd 1023rd 1024th 1025th -20 -19 -18 -17 -16 -15 -14 -13 -12 -11
BASIC256
function ordinal(n)
ns$ = string(n)
begin case
case right(ns$, 1) = "1"
if right(ns$, 2) = "11" then return ns$ + "th"
return ns$ + "st"
case right(ns$, 1) = "2"
if right(ns$, 2) = "12" then return ns$ + "th"
return ns$ + "nd"
case right(ns$, 1) = "3"
if right(ns$, 2) = "13" then return ns$ + "th"
return ns$ + "rd"
else
return ns$ + "th"
end case
end function
subroutine imprimeOrdinal(a, b)
for i = a to b
print ordinal(i); " ";
next i
print
end subroutine
call imprimeOrdinal (0, 25)
call imprimeOrdinal (250, 265)
call imprimeOrdinal (1000, 1025)
end
BBC BASIC
PROCNth( 0, 25)
PROCNth( 250, 265)
PROCNth(1000,1025)
END
DEF PROCNth(s%,e%)
LOCAL i%,suff$
FOR i%=s% TO e%
suff$="th"
IF i% MOD 10 = 1 AND i% MOD 100 <> 11 suff$="st"
IF i% MOD 10 = 2 AND i% MOD 100 <> 12 suff$="nd"
IF i% MOD 10 = 3 AND i% MOD 100 <> 13 suff$="rd"
PRINT STR$i%+suff$+" ";
NEXT
PRINT
ENDPROC
- Output:
0th 1st 2nd 3rd 4th 5th 6th 7th 8th 9th 10th 11th 12th 13th 14th 15th 16th 17th 18th 19th 20th 21st 22nd 23rd 24th 25th 250th 251st 252nd 253rd 254th 255th 256th 257th 258th 259th 260th 261st 262nd 263rd 264th 265th 1000th 1001st 1002nd 1003rd 1004th 1005th 1006th 1007th 1008th 1009th 1010th 1011th 1012th 1013th 1014th 1015th 1016th 1017th 1018th 1019th 1020th 1021st 1022nd 1023rd 1024th 1025th
Chipmunk Basic
100 cls
110 imprimeordinal(0,25)
120 print : print
130 imprimeordinal(250,265)
140 print : print
150 imprimeordinal(1000,1025)
160 print
170 end
180 sub ordinal$(n)
190 ns$ = str$(n)
200 ordinal$ = ""
210 select case right$(ns$,1)
220 case "1"
230 if right$(ns$,2) = "11" then ordinal$ = ns$+"th" : goto 340
240 ordinal$ = ns$+"st" : goto 340
250 case "2"
260 if right$(ns$,2) = "12" then ordinal$ = ns$+"th" : goto 340
270 ordinal$ = ns$+"nd" : goto 340
280 case "3"
290 if right$(ns$,2) = "13" then ordinal$ = ns$+"th" : goto 340
300 ordinal$ = ns$+"rd" : goto 340
310 case else
320 ordinal$ = ns$+"th" : goto 340
330 end case
340 return
350 sub imprimeordinal(a,b)
360 for i = a to b
370 print ordinal$(i);" ";
380 next i
390 return
Commodore BASIC
0 REM ROSETTACODE.ORG
1 REM N'TH
2 REM WRITE A FUNCTION/METHOD/SUBROUTINE/... THAT WHEN GIVEN AN INTEGER GREATER
3 REM THAN OR EQUAL TO ZERO RETURNS A STRING OF THE NUMBER FOLLOWED BY
4 REM AN APOSTROPHE THEN THE ORDINAL SUFFIX.
5 REM BASED ON APPLESOFT BASIC VERSION @ ROSETTACODE.ORG
6 REM
7 REM *************************
8 PRINT CHR$(14): REM CHANGE TO LOWER/UPPER CASE CHAR SET
9 OP = 1
10 FOR N = 0 TO 25 : GOSUB 100 : NEXT : PRINT
20 FOR N = 250 TO 265 : GOSUB 100 : NEXT : PRINT
30 FOR N = 1000 TO 1025 : GOSUB 100 : NEXT : PRINT
40 END
50 REM *************************
100 GOSUB 200
110 PRINT NTH$ " ";
120 RETURN
130 REM ************************
200 M1 = N - INT(N / 10) * 10
210 M2 = N - INT(N / 100) * 100
220 NTH$ = "TH"
230 IF M1 = 1 AND M2 <> 11 THEN NTH$ = "ST"
240 IF M1 = 2 AND M2 <> 12 THEN NTH$ = "ND"
250 IF M1 = 3 AND M2 <> 13 THEN NTH$ = "RD"
260 IF NOT OP THEN NTH$ = "'" + NTH$
270 NTH$ = STR$(N) + NTH$
280 RETURN
- Output:
Commodore 64 (40 column text)
0'th 1'st 2'nd 3'rd 4'th 5'th 6't h 7'th 8'th 9'th 10'th 11'th 12'th 13'th 14'th 15'th 16'th 17'th 18' th 19'th 20'th 21'st 22'nd 23'rd 2 4'th 25'th 250'th 251'st 252'nd 253'rd 254'th 255'th 256'th 257'th 258'th 259'th 260'th 261'st 262'nd 263'rd 264'th 265'th 1000'th 1001'st 1002'nd 1003'rd 100 4'th 1005'th 1006'th 1007'th 1008'th 1009'th 1010'th 1011'th 1012'th 10 13'th 1014'th 1015'th 1016'th 1017't h 1018'th 1019'th 1020'th 1021'st 1 022'nd 1023'rd 1024'th 1025'th
FreeBASIC
' FB 1.05.0 Win64
' Apostrophes NOT used as incorrect English
Function ordinal(n As UInteger) As String
Dim ns As String = Str(n)
Select Case Right(ns, 1)
Case "0", "4" To "9"
Return ns + "th"
Case "1"
If Right(ns, 2) = "11" Then Return ns + "th"
Return ns + "st"
Case "2"
If Right(ns, 2) = "12" Then Return ns + "th"
Return ns + "nd"
Case "3"
If Right(ns, 2) = "13" Then Return ns + "th"
Return ns + "rd"
End Select
End Function
Dim i As Integer
For i = 0 To 25
Print ordinal(i); " ";
Next
Print : Print
For i = 250 To 265
Print ordinal(i); " ";
Next
Print : Print
For i = 1000 To 1025
Print ordinal(i); " ";
Next
Print : Print
Print "Press any key to quit"
Sleep
- Output:
0th 1st 2nd 3rd 4th 5th 6th 7th 8th 9th 10th 11th 12th 13th 14th 15th 16th 17th 18th 19th 20th 21st 22nd 23rd 24th 25th 250th 251st 252nd 253rd 254th 255th 256th 257th 258th 259th 260th 261st 262nd 263rd 264th 265th 1000th 1001st 1002nd 1003rd 1004th 1005th 1006th 1007th 1008th 1009th 1010th 1011th 1012th 1013th 1014th 1015th 1016th 1017th 1018th 1019th 1020th 1021st 1022nd 1023rd 1024th 1025th
Gambas
Click this link to run this code
Public Sub Main()
Dim siNums As Short[] = [0, 25, 250, 265, 1000, 1025]
Dim siCount, siNumbers As Short
Dim sOrdinal As String
For siNumbers = 0 To 4 Step 2
For siCount = siNums[siNumbers] To siNums[siNumbers + 1]
sOrdinal = "th"
If Right(Str(siCount), 1) = "1" And Right(Str(siCount), 2) <> "11" Then sOrdinal = "st"
If Right(Str(siCount), 1) = "2" And Right(Str(siCount), 2) <> "12" Then sOrdinal = "nd"
If Right(Str(siCount), 1) = "3" And Right(Str(siCount), 2) <> "13" Then sOrdinal = "rd"
Print siCount & sOrdinal;;
Next
Print gb.NewLine
Next
End
Output:
0th 1st 2nd 3rd 4th 5th 6th 7th 8th 9th 10th 11th 12th 13th 14th 15th 16th 17th 18th 19th 20th 21st 22nd 23rd 24th 25th 250th 251st 252nd 253rd 254th 255th 256th 257th 258th 259th 260th 261st 262nd 263rd 264th 265th 1000th 1001st 1002nd 1003rd 1004th 1005th 1006th 1007th 1008th 1009th 1010th 1011th 1012th 1013th 1014th 1015th 1016th 1017th 1018th 1019th 1020th 1021st 1022nd 1023rd 1024th 1025th
GW-BASIC
10 ' N'th
20 LET LOLIM% = 0
30 LET HILIM% = 25
40 GOSUB 1000
50 LET LOLIM% = 250
60 LET HILIM% = 265
70 GOSUB 1000
80 LET LOLIM% = 1000
90 LET HILIM% = 1025
100 GOSUB 1000
110 END
995 ' Print images
1000 FOR I% = LOLIM% TO HILIM%
1010 LET NR% = I%
1020 GOSUB 1500
1030 LET SI$ = STR$(I%)
1040 PRINT RIGHT$(SI$, LEN(SI$) - 1); SUF$; " ";
1050 NEXT I%
1060 PRINT
1070 RETURN
1495 ' Get suffix
1500 IF (NR% MOD 10 = 1) AND (NR% MOD 100 <> 11) THEN LET SUF$ = "st": GOTO 2000
1600 IF (NR% MOD 10 = 2) AND (NR% MOD 100 <> 12) THEN LET SUF$ = "nd": GOTO 2000
1700 IF (NR% MOD 10 = 3) AND (NR% MOD 100 <> 13) THEN LET SUF$ = "rd": GOTO 2000
1800 LET SUF$ = "th"
2000 RETURN
- Output:
0th 1st 2nd 3rd 4th 5th 6th 7th 8th 9th 10th 11th 12th 13th 14th 15th 16th 17th 18th 19th 20th 21st 22nd 23rd 24th 25th 250th 251st 252nd 253rd 254th 255th 256th 257th 258th 259th 260th 261st 262nd 263rd 264th 265th 1000th 1001st 1002nd 1003rd 1004th 1005th 1006th 1007th 1008th 1009th 1010th 1011th 1012th 1013th 1014th 1015th 1016th 1017th 1018th 1019th 1020th 1021st 1022nd 1023rd 1024th 1025th
Liberty BASIC
call printImages 0, 25
call printImages 250, 265
call printImages 1000, 1025
end
sub printImages loLim, hiLim
loLim = int(loLim)
hiLIm = int(hiLim)
for i = loLim to hiLim
print str$(i) + suffix$(i) + " ";
next i
print
end sub
function suffix$(n)
n = int(n)
nMod10 = n mod 10
nMod100 = n mod 100
if (nMod10 = 1) and (nMod100 <> 11) then
suffix$ = "st"
else
if (nMod10 = 2) and (nMod100 <> 12) then
suffix$ = "nd"
else
if (NMod10 = 3) and (NMod100 <> 13) then
suffix$ = "rd"
else
suffix$ = "th"
end if
end if
end if
end function
- Output:
0th 1st 2nd 3th 4th 5th 6th 7th 8th 9th 10th 11th 12th 13th 14th 15th 16th 17th 18th 19th 20th 21st 22nd 23th 24th 25th 250th 251st 252nd 253th 254th 255th 256th 257th 258th 259th 260th 261st 262nd 263th 264th 265th 1000th 1001st 1002nd 1003th 1004th 1005th 1006th 1007th 1008th 1009th 1010th 1011th 1012th 1013th 1014th 1015th 1016th 1017th 1018th 1019th 1020th 1021st 1022nd 1023th 1024th 1025th
Microsoft Small Basic
loLim = 0
hiLim = 25
PrintImages()
loLim = 250
hiLim = 265
PrintImages()
loLim = 1000
hiLim = 1025
PrintImages()
Sub PrintImages
For i = loLim To hiLim
nr = i
GetSuffix()
TextWindow.Write(i + suffix + " ")
EndFor
TextWindow.WriteLine("")
EndSub
Sub GetSuffix
rem10 = Math.Remainder(nr, 10)
rem100 = Math.Remainder(nr, 100)
If rem10 = 1 And rem100 <> 11 Then
suffix = "st"
ElseIf rem10 = 2 And rem100 <> 12 Then
suffix = "nd"
ElseIf rem10 = 3 And rem100 <> 13 Then
suffix = "rd"
Else
suffix = "th"
EndIf
EndSub
Nascom BASIC
10 REM N'th
20 LOLIM=0
30 HILIM=25
40 GOSUB 1000
50 LOLIM=250
60 HILIM=265
70 GOSUB 1000
80 LOLIM=1000
90 HILIM=1025
100 GOSUB 1000
110 END
995 REM ** Print images
1000 FOR I=LOLIM TO HILIM
1010 NR=I
1020 GOSUB 1500
1030 SI$=STR$(I)
1040 PRINT RIGHT$(SI$,LEN(SI$)-1);SUF$;" ";
1050 NEXT I
1060 PRINT
1070 RETURN
1495 REM ** Get suffix
1500 LA=NR-INT(NR/10)*10:NLA=NR-INT(NR/100)*100
1510 IF LA=1 AND NLA<>11 THEN SUF$="st":RETURN
1520 IF LA=2 AND NLA<>12 THEN SUF$="nd":RETURN
1530 IF LA=3 AND NLA<>13 THEN SUF$="rd":RETURN
1540 SUF$="th"
1550 RETURN
- Output:
0th 1st 2nd 3rd 4th 5th 6th 7th 8th 9th 10th 11th 12th 13th 14th 15th 16th 17th 18th 19th 20th 21st 22nd 23rd 24th 25th 250th 251st 252nd 253rd 254th 255th 256th 257th 258th 259th 260th 261st 262nd 263rd 264th 265th 1000th 1001st 1002nd 1003rd 1004th 1005th 1006th 1007th 1008th 1009th 1010th 1011th 1012th 1013th 1014th 1015th 1016th 1017th 1018th 1019th 1020th 1021st 1022nd 1023rd 1024th 1025th
PureBasic
Procedure.s Suffix(n.i)
Select n%10
Case 1 : If n%100<>11 : ProcedureReturn "st" : EndIf
Case 2 : If n%100<>12 : ProcedureReturn "nd" : EndIf
Case 3 : If n%100<>13 : ProcedureReturn "rd" : EndIf
EndSelect
ProcedureReturn "th"
EndProcedure
Procedure put(a.i,b.i)
For i=a To b : Print(Str(i)+Suffix(i)+" ") : Next
PrintN("")
EndProcedure
If OpenConsole()=0 : End 1 : EndIf
put(0,25)
put(250,265)
put(1000,1025)
Input()
- Output:
0th 1st 2nd 3rd 4th 5th 6th 7th 8th 9th 10th 11th 12th 13th 14th 15th 16th 17th 18th 19th 20th 21st 22nd 23rd 24th 25th 250th 251st 252nd 253rd 254th 255th 256th 257th 258th 259th 260th 261st 262nd 263rd 264th 265th 1000th 1001st 1002nd 1003rd 1004th 1005th 1006th 1007th 1008th 1009th 1010th 1011th 1012th 1013th 1014th 1015th 1016th 1017th 1018th 1019th 1020th 1021st 1022nd 1023rd 1024th 1025th
QBasic
DECLARE FUNCTION sufijo$ (n%)
DECLARE SUB imprimeOrdinal (loLim%, hiLim%)
imprimeOrdinal 0, 25
imprimeOrdinal 250, 265
imprimeOrdinal 1000, 1025
END
DEFINT A-Z
SUB imprimeOrdinal (loLim, hiLim)
loLim = INT(loLim)
hiLim = INT(hiLim)
FOR i = loLim TO hiLim
PRINT STR$(i) + sufijo$(i) + " ";
NEXT i
PRINT
END SUB
FUNCTION sufijo$ (n)
n = INT(n)
NMod10 = n MOD 10
NMod100 = n MOD 100
IF (NMod10 = 1) AND (NMod100 <> 11) THEN
sufijo$ = "st"
ELSE
IF (NMod10 = 2) AND (NMod100 <> 12) THEN
sufijo$ = "nd"
ELSE
IF (NMod10 = 3) AND (NMod100 <> 13) THEN
sufijo$ = "rd"
ELSE
sufijo$ = "th"
END IF
END IF
END IF
END FUNCTION
Run BASIC
The Liberty BASIC solution works without any changes.
Sinclair ZX81 BASIC
Works flawlessly with 2k or more of RAM. With 1k, the subroutine itself works but you can't quite print all the tests: the program crashes with an 'out of memory' error code after 1017th. (A slightly less useful and readable version gets as far as 1023rd; 1025th is probably attainable, but might involve obfuscating the program more than is appropriate for this site.)
10 FOR N=0 TO 25
20 GOSUB 160
30 PRINT N$;" ";
40 NEXT N
50 PRINT
60 FOR N=250 TO 265
70 GOSUB 160
80 PRINT N$;" ";
90 NEXT N
100 PRINT
110 FOR N=1000 TO 1025
120 GOSUB 160
130 PRINT N$;" ";
140 NEXT N
150 STOP
160 LET N$=STR$ N
170 LET S$="TH"
180 IF LEN N$=1 THEN GOTO 200
190 IF N$(LEN N$-1)="1" THEN GOTO 230
200 IF N$(LEN N$)="1" THEN LET S$="ST"
210 IF N$(LEN N$)="2" THEN LET S$="ND"
220 IF N$(LEN N$)="3" THEN LET S$="RD"
230 LET N$=N$+S$
240 RETURN
- Output:
0TH 1ST 2ND 3RD 4TH 5TH 6TH 7TH 8TH 9TH 10TH 11TH 12TH 13TH 14TH 15TH 16TH 17TH 18TH 19TH 20TH 21ST 22ND 23RD 24TH 25TH 250TH 251ST 252ND 253RD 254TH 255TH 256TH 257TH 258TH 259TH 260TH 261ST 262ND 263RD 264TH 265TH 1000TH 1001ST 1002ND 1003RD 1004TH 1005TH 1006TH 1007TH 1008TH 1009TH 1010TH 1011TH 1012TH 1013TH 1014TH 1015TH 1016TH 1017TH 1018TH 1019TH 1020TH 1021ST 1022ND 1023RD 1024TH 1025TH
True BASIC
SUB sufijo (n)
LET n = INT(n)
LET NMod10 = MOD(n, 10)
LET NMod100 = MOD(n, 100)
IF (NMod10 = 1) AND (NMod100 <> 11) THEN
LET sufi$ = "st"
ELSE
IF (NMod10 = 2) AND (NMod100 <> 12) THEN
LET sufi$ = "nd"
ELSE
IF (NMod10 = 3) AND (NMod100 <> 13) THEN
LET sufi$ = "rd"
ELSE
LET sufi$ = "th"
END IF
END IF
END IF
PRINT sufi$;
END SUB
SUB imprimeOrdinal (loLim, hiLim)
LET loLim = INT(loLim)
LET hiLim = INT(hiLim)
FOR i = loLim TO hiLim
PRINT i;
CALL sufijo (i)
PRINT " ";
NEXT i
PRINT
END SUB
CALL imprimeOrdinal (0, 25)
CALL imprimeOrdinal (250, 265)
CALL imprimeOrdinal (1000, 1025)
END
uBasic/4tH
For x = 0 to 25 ' Test range 0..25
Push x : GoSub _PrintOrdinal
Next x : Print
For x = 250 to 265 ' Test range 250..265
Push x : GoSub _PrintOrdinal
Next x : Print
For x = 1000 to 1025 ' Test range 1000..1025
Push x : GoSub _PrintOrdinal
Next x : Print
End ' End test program
' ( n --)
_PrintOrdinal ' Ordinal subroutine
If Tos() > -1 Then ' If within range then
Print Using "____#";Tos();"'"; ' Print the number
' Take care of 11, 12 and 13
If (Tos()%100 > 10) * (Tos()%100 < 14) Then
Gosub (Pop() * 0) + 100 ' Clear stack and print "th"
Return ' We're done here
EndIf
Push Pop() % 10 ' Calculate n mod 10
GoSub 100 + 10 * ((Tos()>0) + (Tos()>1) + (Tos()>2) - (3 * (Pop()>3)))
Else ' And decide which ordinal to use
Print Pop();" is less than zero" ' Otherwise, it is an error
EndIf
Return
' Select and print proper ordinal
100 Print "th"; : Return
110 Print "st"; : Return
120 Print "nd"; : Return
130 Print "rd"; : Return
- Output:
0'th 1'st 2'nd 3'rd 4'th 5'th 6'th 7'th 8'th 9'th 10'th 11'th 12'th 13'th 14'th 15'th 16'th 17'th 18'th 19'th 20'th 21'st 22'nd 23'rd 24'th 25'th 250'th 251'st 252'nd 253'rd 254'th 255'th 256'th 257'th 258'th 259'th 260'th 261'st 262'nd 263'rd 264'th 265'th 1000'th 1001'st 1002'nd 1003'rd 1004'th 1005'th 1006'th 1007'th 1008'th 1009'th 1010'th 1011'th 1012'th 1013'th 1014'th 1015'th 1016'th 1017'th 1018'th 1019'th 1020'th 1021'st 1022'nd 1023'rd 1024'th 1025'th
VBA
Private Function ordinals() As Variant
ordinals = [{"th","st","nd","rd"}]
End Function
Private Function Nth(n As Variant, Optional apostrophe As Boolean = False) As String
Dim mod10 As Integer: mod10 = n Mod 10 + 1
If mod10 > 4 Or n Mod 100 = mod10 + 9 Then mod10 = 1
Nth = CStr(n) & String$(Val(-apostrophe), "'") & ordinals()(mod10)
End Function
Public Sub main()
Ranges = [{0,25;250,265;1000,1025}]
For i = 1 To UBound(Ranges)
For j = Ranges(i, 1) To Ranges(i, 2)
If j Mod 10 = 0 Then Debug.Print
Debug.Print Format(Nth(j, i = 2), "@@@@@@@");
Next j
Debug.Print
Next i
End Sub
- Output:
0th 1st 2nd 3rd 4th 5th 6th 7th 8th 9th 10th 11th 12th 13th 14th 15th 16th 17th 18th 19th 20th 21st 22nd 23rd 24th 25th 250'th 251'st 252'nd 253'rd 254'th 255'th 256'th 257'th 258'th 259'th 260'th 261'st 262'nd 263'rd 264'th 265'th 1000th 1001st 1002nd 1003rd 1004th 1005th 1006th 1007th 1008th 1009th 1010th 1011th 1012th 1013th 1014th 1015th 1016th 1017th 1018th 1019th 1020th 1021st 1022nd 1023rd 1024th 1025th
XBasic
PROGRAM "nth"
VERSION "0.0002"
DECLARE FUNCTION Entry()
INTERNAL FUNCTION Suffix$(n&&)
INTERNAL FUNCTION PrintImages (loLim&&, hiLim&&)
FUNCTION Entry()
PrintImages( 0, 25)
PrintImages( 250, 265)
PrintImages(1000, 1025)
END FUNCTION
FUNCTION Suffix$(n&&)
nMod10@@ = n&& MOD 10
nMod100@@ = n&& MOD 100
SELECT CASE TRUE
CASE (nMod10@@ = 1) AND (nMod100@@ <> 11):
RETURN ("st")
CASE (nMod10@@ = 2) AND (nMod100@@ <> 12):
RETURN ("nd")
CASE (nMod10@@ = 3) AND (nMod100@@ <> 13):
RETURN ("rd")
CASE ELSE:
RETURN ("th")
END SELECT
END FUNCTION
FUNCTION PrintImages(loLim&&, hiLim&&)
FOR i&& = loLim&& TO hiLim&&
PRINT TRIM$(STRING$(i&&)); Suffix$(i&&); " ";
NEXT
PRINT
END FUNCTION
END PROGRAM
- Output:
0th 1st 2nd 3rd 4th 5th 6th 7th 8th 9th 10th 11th 12th 13th 14th 15th 16th 17th 18th 19th 20th 21st 22nd 23rd 24th 25th 250th 251st 252nd 253rd 254th 255th 256th 257th 258th 259th 260th 261st 262nd 263rd 264th 265th 1000th 1001st 1002nd 1003rd 1004th 1005th 1006th 1007th 1008th 1009th 1010th 1011th 1012th 1013th 1014th 1015th 1016th 1017th 1018th 1019th 1020th 1021st 1022nd 1023rd 1024th 1025th
Yabasic
sub ordinal$ (n)
NMod10 = mod(n, 10)
NMod100 = mod(n, 100)
if (NMod10 = 1) and (NMod100 <> 11) then
return "st"
else
if (NMod10 = 2) and (NMod100 <> 12) then
return "nd"
else
if (NMod10 = 3) and (NMod100 <> 13) then
return "rd"
else
return "th"
end if
end if
end if
end sub
sub imprimeOrdinal(a, b)
for i = a to b
print i, ordinal$(i), " ";
next i
print
end sub
imprimeOrdinal (0, 25)
imprimeOrdinal (250, 265)
imprimeOrdinal (1000, 1025)
end
ZX Spectrum Basic
10 FOR n=0 TO 25
20 GO SUB 140
30 PRINT n$;" ";
40 NEXT n
50 FOR n=250 TO 265
60 GO SUB 140
70 PRINT n$;" ";
80 NEXT n
90 FOR n=1000 TO 1025
100 GO SUB 140
110 PRINT n$;" ";
120 NEXT n
130 STOP
140 LET s$="th"
150 LET n$=STR$ n
160 IF LEN n$=1 THEN GO TO 180
170 IF n$(LEN n$-1)="1" THEN GO TO 210
180 IF n$(LEN n$)="1" THEN LET s$="st"
190 IF n$(LEN n$)="2" THEN LET s$="nd"
200 IF n$(LEN n$)="3" THEN LET s$="rd"
210 LET n$=n$+s$
220 RETURN
- Output:
0th 1st 2nd 3rd 4th 5th 6th 7th 8th 9th 10th 11th 12th 13th 14th 15th 16th 17th 18th 19th 20th 21st 22nd 23rd 24th 25th 250th 251st 252nd 253rd 254th 255th 256th 257th 258th 259th 260th 261st 262nd 263rd 264th 265th 1000th 1001st 1002nd 1003rd 1004th 1005th 1006th 1007th 1008th 1009th 1010th 1011th 1012th 1013th 1014th 1015th 1016th 1017th 1018th 1019th 1020th 1021st 1022nd 1023rd 1024th 1025th
Batch File
@echo off
::Main thing...
call :Nth 0 25
call :Nth 250 265
call :Nth 1000 1025
pause
exit /b
::The subroutine
:Nth <lbound> <ubound>
setlocal enabledelayedexpansion
for /l %%n in (%~1,1,%~2) do (
set curr_num=%%n
if !curr_num:~-2!==11 (set "out=%%nth"
) else if !curr_num:~-2!==12 (set "out=%%nth"
) else if !curr_num:~-2!==13 (set "out=%%nth"
) else if !curr_num:~-1!==1 (set "out=%%nst"
) else if !curr_num:~-1!==2 (set "out=%%nnd"
) else if !curr_num:~-1!==3 (set "out=%%nrd"
) else (set "out=%%nth")
set "range_output=!range_output! !out!"
)
echo."!range_output:~1!"
goto :EOF
- Output:
"0th 1st 2nd 3rd 4th 5th 6th 7th 8th 9th 10th 11th 12th 13th 14th 15th 16th 17th 18th 19th 20th 21st 22nd 23rd 24th 25th" "250th 251st 252nd 253rd 254th 255th 256th 257th 258th 259th 260th 261st 262nd 263rd 264th 265th" "1000th 1001st 1002nd 1003rd 1004th 1005th 1006th 1007th 1008th 1009th 1010th 1011th 1012th 1013th 1014th 1015th 1016th 1017th 1018th 1019th 1020th 1021st 1022nd 1023rd 1024th 1025th" Press any key to continue . . .
BCPL
get "libhdr"
// Generate ASCII string of number n with ordinal suffix
// The string is stored at v.
let nth(n, v) = valof
$( let sfx = "thstndrd"
let c, s = 0, ?
// Generate digits
$( c := c + 1
v%c := n rem 10 + '0'
n := n / 10
$) repeatuntil n = 0
// Put digits in correct order
for i = 1 to c/2 do
$( let d = v%i
v%i := v%(c+1-i)
v%(c+1-i) := d
$)
// The length of the string is the amount of digits + 2
v%0 := c+2;
// Figure out the proper suffix from the last two digits
test v%(c-1)='1' | v%c>'3'
then s := 0
else s := 2*(v%c - '0')
v%(c+1) := sfx%(s+1)
v%(c+2) := sfx%(s+2)
resultis v
$)
let start() be
$( let buf = vec 10
for i = 0 to 25 do writef("%S*N", nth(i, buf))
for i = 250 to 265 do writef("%S*N", nth(i, buf))
for i = 1000 to 1025 do writef("%S*N", nth(i, buf))
$)
- Output:
0th 1st 2nd 3rd 4th 5th 6th 7th 8th 9th 10th 11th 12th 13th 14th 15th 16th 17th 18th 19th 20th 21st 22nd 23rd 24th 25th 250th 251st 252nd 253rd 254th 255th 256th 257th 258th 259th 260th 261st 262nd 263rd 264th 265th 1000th 1001st 1002nd 1003rd 1004th 1005th 1006th 1007th 1008th 1009th 1010th 1011th 1012th 1013th 1014th 1015th 1016th 1017th 1018th 1019th 1020th 1021st 1022nd 1023rd 1024th 1025th
Befunge
The bottom section of code contains the "subroutine" that calculates the ordinal of a given number; the top section generates the list of values to test.
0>55*:>1-\:0\`!v
#v$#$<^:\+*8"}"_
>35*:>1-\:0\`!v
#v$#$<^:\+*2"}"_
5< v$_v#!::-<0*5
@v <,*>#81#4^# _
>>:0\>:55+%68*v:
tsnr |:/+ 55\+<,
htdd >$>:#,_$:vg
v"d"\*!`3:%+55<9
>%55+/1-!!*:8g,^
- Output:
0th 1st 2nd 3rd 4th 5th 6th 7th 8th 9th 10th 11th 12th 13th 14th 15th 16th 17th 18th 19th 20th 21st 22nd 23rd 24th 25th 250th 251st 252nd 253rd 254th 255th 256th 257th 258th 259th 260th 261st 262nd 263rd 264th 265th 1000th 1001st 1002nd 1003rd 1004th 1005th 1006th 1007th 1008th 1009th 1010th 1011th 1012th 1013th 1014th 1015th 1016th 1017th 1018th 1019th 1020th 1021st 1022nd 1023rd 1024th 1025th
BQN
Nth ← {
sfx ← ⟨"th","st","nd","rd"⟩
(•Fmt 𝕩)∾sfx⊑˜ (1≠10|⌊𝕩÷10)×(3≥10|𝕩)×10|𝕩
}
∘‿4⥊ Nth¨ (↕26) ∾ (250+↕16) ∾ (1000+↕26)
- Output:
┌─ ╵ "0th" "1st" "2nd" "3rd" "4th" "5th" "6th" "7th" "8th" "9th" "10th" "11th" "12th" "13th" "14th" "15th" "16th" "17th" "18th" "19th" "20th" "21st" "22nd" "23rd" "24th" "25th" "250th" "251st" "252nd" "253rd" "254th" "255th" "256th" "257th" "258th" "259th" "260th" "261st" "262nd" "263rd" "264th" "265th" "1000th" "1001st" "1002nd" "1003rd" "1004th" "1005th" "1006th" "1007th" "1008th" "1009th" "1010th" "1011th" "1012th" "1013th" "1014th" "1015th" "1016th" "1017th" "1018th" "1019th" "1020th" "1021st" "1022nd" "1023rd" "1024th" "1025th" ┘
C
#include <stdio.h>
char* addSuffix(int num, char* buf, size_t len)
{
char *suffixes[4] = { "th", "st", "nd", "rd" };
int i;
switch (num % 10)
{
case 1 : i = (num % 100 == 11) ? 0 : 1;
break;
case 2 : i = (num % 100 == 12) ? 0 : 2;
break;
case 3 : i = (num % 100 == 13) ? 0 : 3;
break;
default: i = 0;
};
snprintf(buf, len, "%d%s", num, suffixes[i]);
return buf;
}
int main(void)
{
int i;
printf("Set [0,25]:\n");
for (i = 0; i < 26; i++)
{
char s[5];
printf("%s ", addSuffix(i, s, 5));
}
putchar('\n');
printf("Set [250,265]:\n");
for (i = 250; i < 266; i++)
{
char s[6];
printf("%s ", addSuffix(i, s, 6));
}
putchar('\n');
printf("Set [1000,1025]:\n");
for (i = 1000; i < 1026; i++)
{
char s[7];
printf("%s ", addSuffix(i, s, 7));
}
putchar('\n');
return 0;
}
Another method with dynamic memory allocation
#include <stdlib.h>
#include <stdio.h>
static int digits(const int x) {
if (x / 10 == 0) return 1;
return 1 + digits(x / 10);
}
static char * get_ordinal(const int i) {
const int string_size = digits(i) + 3;
char * o_number = malloc(string_size);
char * ordinal;
if(i % 100 >= 11 && i % 100 <= 13) ordinal = "th";
else ordinal = i/10==1?"th":i%10==1?"st":i%10==2?"nd":i%10==3?"rd":"th";
sprintf_s(o_number, string_size, "%d%s", i, ordinal);
return o_number;
}
static void print_range(const int begin, const int end) {
printf("Set [%d,%d]:\n", begin, end);
for (int i = begin; i <= end; i++) {
char * o_number = get_ordinal(i);
printf("%s ", o_number);
free(o_number);
}
printf("\n");
}
int main(void) {
print_range(0, 25);
print_range(250, 265);
print_range(1000, 1025);
}
- Output:
Set [0,25] : 0th 1st 2nd 3rd 4th 5th 6th 7th 8th 9th 10th 11th 12th 13th 14th 15th 16th 17th 18th 19th 20th 21st 22nd 23rd 24th 25th Set [250,265] : 250th 251st 252nd 253rd 254th 255th 256th 257th 258th 259th 260th 261st 262nd 263rd 264th 265th Set [1000,1025] : 1000th 1001st 1002nd 1003rd 1004th 1005th 1006th 1007th 1008th 1009th 1010th 1011th 1012th 1013th 1014th 1015th 1016th 1017th 1018th 1019th 1020th 1021st 1022nd 1023rd 1024th 1025th
C#
using System;
using System.Linq;
class Program
{
private static string Ordinalize(int i)
{
i = Math.Abs(i);
if (new[] {11, 12, 13}.Contains(i%100))
return i + "th";
switch (i%10)
{
case 1:
return i + "st";
case 2:
return i + "nd";
case 3:
return i + "rd";
default:
return i + "th";
}
}
static void Main()
{
Console.WriteLine(string.Join(" ", Enumerable.Range(0, 26).Select(Ordinalize)));
Console.WriteLine(string.Join(" ", Enumerable.Range(250, 16).Select(Ordinalize)));
Console.WriteLine(string.Join(" ", Enumerable.Range(1000, 26).Select(Ordinalize)));
}
}
Dotnet 5 version without LINQ
using System;
static string Ordinalize(int i)
{
return i + (
i % 100 is >= 11 and <= 13 ? "th" :
i % 10 == 1 ? "st" :
i % 10 == 2 ? "nd" :
i % 10 == 3 ? "rd" :
"th");
}
static void PrintRange(int begin, int end)
{
for(var i = begin; i <= end; i++)
Console.Write(Ordinalize(i) + (i == end ? "" : " "));
Console.WriteLine();
}
PrintRange(0, 25);
PrintRange(250, 265);
PrintRange(1000, 1025);
- Output:
0th 1st 2nd 3rd 4th 5th 6th 7th 8th 9th 10th 11th 12th 13th 14th 15th 16th 17th 18th 19th 20th 21st 22nd 23rd 24th 25th 250th 251st 252nd 253rd 254th 255th 256th 257th 258th 259th 260th 261st 262nd 263rd 264th 265th 1000th 1001st 1002nd 1003rd 1004th 1005th 1006th 1007th 1008th 1009th 1010th 1011th 1012th 1013th 1014th 1015th 1016th 1017th 1018th 1019th 1020th 1021st 1022nd 1023rd 1024th 1025th
C++
#include <string>
#include <iostream>
using namespace std;
string Suffix(int num)
{
switch (num % 10)
{
case 1 : if(num % 100 != 11) return "st";
break;
case 2 : if(num % 100 != 12) return "nd";
break;
case 3 : if(num % 100 != 13) return "rd";
}
return "th";
}
int main()
{
cout << "Set [0,25]:" << endl;
for (int i = 0; i < 26; i++)
cout << i << Suffix(i) << " ";
cout << endl;
cout << "Set [250,265]:" << endl;
for (int i = 250; i < 266; i++)
cout << i << Suffix(i) << " ";
cout << endl;
cout << "Set [1000,1025]:" << endl;
for (int i = 1000; i < 1026; i++)
cout << i << Suffix(i) << " ";
cout << endl;
return 0;
}
- Output:
Set [0,25] : 0th 1st 2nd 3rd 4th 5th 6th 7th 8th 9th 10th 11th 12th 13th 14th 15th 16th 17th 18th 19th 20th 21st 22nd 23rd 24th 25th Set [250,265] : 250th 251st 252nd 253rd 254th 255th 256th 257th 258th 259th 260th 261st 262nd 263rd 264th 265th Set [1000,1025] : 1000th 1001st 1002nd 1003rd 1004th 1005th 1006th 1007th 1008th 1009th 1010th 1011th 1012th 1013th 1014th 1015th 1016th 1017th 1018th 1019th 1020th 1021st 1022nd 1023rd 1024th 1025th
Clojure
(defn n-th [n]
(str n
(let [rem (mod n 100)]
(if (and (>= rem 11) (<= rem 13))
"th"
(condp = (mod n 10)
1 "st"
2 "nd"
3 "rd"
"th")))))
(apply str (interpose " " (map n-th (range 0 26))))
(apply str (interpose " " (map n-th (range 250 266))))
(apply str (interpose " " (map n-th (range 1000 1026))))
- Output:
"0th 1st 2nd 3rd 4th 5th 6th 7th 8th 9th 10th 11th 12th 13th 14th 15th 16th 17th 18th 19th 20th 21st 22nd 23rd 24th 25th" "250th 251st 252nd 253rd 254th 255th 256th 257th 258th 259th 260th 261st 262nd 263rd 264th 265th" "1000th 1001st 1002nd 1003rd 1004th 1005th 1006th 1007th 1008th 1009th 1010th 1011th 1012th 1013th 1014th 1015th 1016th 1017th 1018th 1019th 1020th 1021st 1022nd 1023rd 1024th 1025th"
Alternatively, if you want to print the full ordinal English, it becomes trivial with pprint:
(apply str (interpose " " (map #(clojure.pprint/cl-format nil "~:R" %) (range 0 26))))
- Output:
"zeroth first second third fourth fifth sixth seventh eighth ninth tenth eleventh twelfth thirteenth fourteenth fifteenth sixteenth seventeenth eighteenth nineteenth twentieth twenty-first twenty-second twenty-third twenty-fourth twenty-fifth"
CLU
nth = proc (n: int) returns (string)
num: string := int$unparse(n)
sfx: array[string] := array[string]$[0: "th", "st", "nd", "rd"]
if n / 10 // 10 = 1 cor n // 10 > 3 then
return(num || sfx[0])
else
return(num || sfx[n // 10])
end
end nth
do_range = proc (from, to: int)
po: stream := stream$primary_output()
col: int := 0
for i: int in int$from_to(from,to) do
stream$putleft(po, nth(i), 7)
col := col + 1
if col = 10 then
stream$putc(po, '\n')
col := 0
end
end
stream$putl(po, "\n")
end do_range
start_up = proc ()
do_range(0,25)
do_range(250,265)
do_range(1000,1025)
end start_up
- Output:
0th 1st 2nd 3rd 4th 5th 6th 7th 8th 9th 10th 11th 12th 13th 14th 15th 16th 17th 18th 19th 20th 21st 22nd 23rd 24th 25th 250th 251st 252nd 253rd 254th 255th 256th 257th 258th 259th 260th 261st 262nd 263rd 264th 265th 1000th 1001st 1002nd 1003rd 1004th 1005th 1006th 1007th 1008th 1009th 1010th 1011th 1012th 1013th 1014th 1015th 1016th 1017th 1018th 1019th 1020th 1021st 1022nd 1023rd 1024th 1025th
COBOL
COBOL stores numbers in decimal form, so there is no need to use a modulo function: the last digit or the last two digits can be extracted directly.
IDENTIFICATION DIVISION.
PROGRAM-ID. NTH-PROGRAM.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-NUMBER.
05 N PIC 9(8).
05 LAST-TWO-DIGITS PIC 99.
05 LAST-DIGIT PIC 9.
05 N-TO-OUTPUT PIC Z(7)9.
05 SUFFIX PIC AA.
PROCEDURE DIVISION.
TEST-PARAGRAPH.
PERFORM NTH-PARAGRAPH VARYING N FROM 0 BY 1 UNTIL N IS GREATER THAN 25.
PERFORM NTH-PARAGRAPH VARYING N FROM 250 BY 1 UNTIL N IS GREATER THAN 265.
PERFORM NTH-PARAGRAPH VARYING N FROM 1000 BY 1 UNTIL N IS GREATER THAN 1025.
STOP RUN.
NTH-PARAGRAPH.
MOVE 'TH' TO SUFFIX.
MOVE N (7:2) TO LAST-TWO-DIGITS.
IF LAST-TWO-DIGITS IS LESS THAN 4,
OR LAST-TWO-DIGITS IS GREATER THAN 20,
THEN PERFORM DECISION-PARAGRAPH.
MOVE N TO N-TO-OUTPUT.
DISPLAY N-TO-OUTPUT WITH NO ADVANCING.
DISPLAY SUFFIX WITH NO ADVANCING.
DISPLAY SPACE WITH NO ADVANCING.
DECISION-PARAGRAPH.
MOVE N (8:1) TO LAST-DIGIT.
IF LAST-DIGIT IS EQUAL TO 1 THEN MOVE 'ST' TO SUFFIX.
IF LAST-DIGIT IS EQUAL TO 2 THEN MOVE 'ND' TO SUFFIX.
IF LAST-DIGIT IS EQUAL TO 3 THEN MOVE 'RD' TO SUFFIX.
Output:
0TH 1ST 2ND 3RD 4TH 5TH 6TH 7TH 8TH 9TH 10TH 11TH 12TH 13TH 14TH 15TH 16TH 17TH 18TH 19TH 20TH 21ST 22ND 23RD 24TH 25TH 250TH 251ST 252ND 253RD 254TH 255TH 256TH 257TH 258TH 259TH 260TH 261ST 262ND 263RD 264TH 265TH 1000TH 1001ST 1002ND 1003RD 1004TH 1005TH 1006TH 1007TH 1008TH 1009TH 1010TH 1011TH 1012TH 1013TH 1014TH 1015TH 1016TH 1017TH 1018TH 1019TH 1020TH 1021ST 1022ND 1023RD 1024TH 1025TH
Common Lisp
(defun add-suffix (number)
(let* ((suffixes #10("th" "st" "nd" "rd" "th"))
(last2 (mod number 100))
(last-digit (mod number 10))
(suffix (if (< 10 last2 20)
"th"
(svref suffixes last-digit))))
(format nil "~a~a" number suffix)))
A more concise, albeit less readable version:
(defun add-suffix (n)
(format nil "~d'~:[~[th~;st~;nd~;rd~:;th~]~;th~]" n (< (mod (- n 10) 100) 10) (mod n 10)))
Display the results:
(loop for (low high) in '((0 25) (250 265) (1000 1025))
do (progn
(format t "~a to ~a: " low high)
(loop for n from low to high
do (format t "~a " (add-suffix n))
finally (terpri))))
- Output:
0 to 25: 0th 1st 2nd 3rd 4th 5th 6th 7th 8th 9th 10th 11th 12th 13th 14th 15th 16th 17th 18th 19th 20th 21st 22nd 23rd 24th 25th 250 to 265: 250th 251st 252nd 253rd 254th 255th 256th 257th 258th 259th 260th 261st 262nd 263rd 264th 265th 1000 to 1025: 1000th 1001st 1002nd 1003rd 1004th 1005th 1006th 1007th 1008th 1009th 1010th 1011th 1012th 1013th 1014th 1015th 1016th 1017th 1018th 1019th 1020th 1021st 1022nd 1023rd 1024th 1025th
Cowgol
include "cowgol.coh";
include "strings.coh";
# Given an int32, return decimal string and add an ordinal suffix
sub ordsfx(n: int32, buf: [uint8]): (out: [uint8]) is
var sfx: [uint8][] := {"th", "st", "nd", "rd"};
out := buf;
buf := IToA(n, 10, buf); # IToA is included in standard library
# Since we now already have the digits, we can make the decision
# without doing any more work.
if (n > 10 and [@prev @prev buf] == '1')
or ([@prev buf] > '3') then
CopyString(sfx[0], buf);
else
CopyString(sfx[[@prev buf]-'0'], buf);
end if;
end sub;
# Print suffixed numerals from start to end inclusive
sub test(start: int32, end_: int32) is
var buf: uint8[16]; # buffer
var n: uint8 := 10;
while start <= end_ loop
print(ordsfx(start, &buf[0]));
print_char(' ');
start := start + 1;
n := n - 1;
if n == 0 then
print_nl();
n := 10;
end if;
end loop;
print_nl();
end sub;
test(0, 25);
test(250,265);
test(1000,1025);
- Output:
0th 1st 2nd 3rd 4th 5th 6th 7th 8th 9th 10th 11th 12th 13th 14th 15th 16th 17th 18th 19th 20th 21st 22nd 23rd 24th 25th 250th 251st 252nd 253rd 254th 255th 256th 257th 258th 259th 260th 261st 262nd 263rd 264th 265th 1000th 1001st 1002nd 1003rd 1004th 1005th 1006th 1007th 1008th 1009th 1010th 1011th 1012th 1013th 1014th 1015th 1016th 1017th 1018th 1019th 1020th 1021st 1022nd 1023rd 1024th 1025th
Crystal
struct Int
def ordinalize
num = self.abs
ordinal = if (11..13).includes?(num % 100)
"th"
else
case num % 10
when 1; "st"
when 2; "nd"
when 3; "rd"
else "th"
end
end
"#{self}#{ordinal}"
end
end
[(0..25),(250..265),(1000..1025)].each{|r| puts r.map{ |n| n.ordinalize }.join(", "); puts}
- Output:
0th, 1st, 2nd, 3rd, 4th, 5th, 6th, 7th, 8th, 9th, 10th, 11th, 12th, 13th, 14th, 15th, 16th, 17th, 18th, 19th, 20th, 21st, 22nd, 23rd, 24th, 25th 250th, 251st, 252nd, 253rd, 254th, 255th, 256th, 257th, 258th, 259th, 260th, 261st, 262nd, 263rd, 264th, 265th 1000th, 1001st, 1002nd, 1003rd, 1004th, 1005th, 1006th, 1007th, 1008th, 1009th, 1010th, 1011th, 1012th, 1013th, 1014th, 1015th, 1016th, 1017th, 1018th, 1019th, 1020th, 1021st, 1022nd, 1023rd, 1024th, 1025th
D
import std.stdio, std.string, std.range, std.algorithm;
string nth(in uint n) pure {
static immutable suffix = "th st nd rd th th th th th th".split;
return "%d'%s".format(n, (n % 100 <= 10 || n % 100 > 20) ?
suffix[n % 10] : "th");
}
void main() {
foreach (r; [iota(26), iota(250, 266), iota(1000, 1026)])
writefln("%-(%s %)", r.map!nth);
}
- Output:
0'th 1'st 2'nd 3'rd 4'th 5'th 6'th 7'th 8'th 9'th 10'th 11'th 12'th 13'th 14'th 15'th 16'th 17'th 18'th 19'th 20'th 21'st 22'nd 23'rd 24'th 25'th 250'th 251'st 252'nd 253'rd 254'th 255'th 256'th 257'th 258'th 259'th 260'th 261'st 262'nd 263'rd 264'th 265'th 1000'th 1001'st 1002'nd 1003'rd 1004'th 1005'th 1006'th 1007'th 1008'th 1009'th 1010'th 1011'th 1012'th 1013'th 1014'th 1015'th 1016'th 1017'th 1018'th 1019'th 1020'th 1021'st 1022'nd 1023'rd 1024'th 1025'th
Delphi
See Pascal.
Draco
proc nonrec nth(word n; *char buf) *char:
channel output text ch;
open(ch, buf);
write(ch; n,
if (n/10)%10=1 then "th"
elif n%10=1 then "st"
elif n%10=2 then "nd"
elif n%10=3 then "rd"
else "th"
fi
);
close(ch);
buf
corp;
proc nonrec print_range(word start, stop) void:
[8] char buf;
word col, n;
col := 0;
for n from start upto stop do
write(nth(n, &buf[0]));
col := col + 1;
if col%10=0 then writeln() else write('\t') fi
od;
writeln()
corp
proc nonrec main() void:
print_range(0, 25);
print_range(250, 265);
print_range(1000, 1025)
corp
- Output:
0th 1st 2nd 3rd 4th 5th 6th 7th 8th 9th 10th 11th 12th 13th 14th 15th 16th 17th 18th 19th 20th 21st 22nd 23rd 24th 25th 250th 251st 252nd 253rd 254th 255th 256th 257th 258th 259th 260th 261st 262nd 263rd 264th 265th 1000th 1001st 1002nd 1003rd 1004th 1005th 1006th 1007th 1008th 1009th 1010th 1011th 1012th 1013th 1014th 1015th 1016th 1017th 1018th 1019th 1020th 1021st 1022nd 1023rd 1024th 1025th
EasyLang
func$ nth num .
last2 = num mod 100
last = num mod 10
if last2 >= 11 and last2 <= 13
return num & "th"
elif last = 1
return num & "st"
elif last = 2
return num & "nd"
elif last = 3
return num & "rd"
else
return num & "th"
.
.
print "0 to 25:"
for i = 0 to 25
write nth i & " "
.
print ""
print "250 to 265:"
for i = 250 to 265
write nth i & " "
.
print ""
print "1000 to 1025:"
for i = 1000 to 1025
write nth i & " "
.
- Output:
0 to 25: 0th 1st 2nd 3rd 4th 5th 6th 7th 8th 9th 10th 11th 12th 13th 14th 15th 16th 17th 18th 19th 20th 21st 22nd 23rd 24th 25th 250 to 265: 250th 251st 252nd 253rd 254th 255th 256th 257th 258th 259th 260th 261st 262nd 263rd 264th 265th 1000 to 1025: 1000th 1001st 1002nd 1003rd 1004th 1005th 1006th 1007th 1008th 1009th 1010th 1011th 1012th 1013th 1014th 1015th 1016th 1017th 1018th 1019th 1020th 1021st 1022nd 1023rd 1024th 1025th
ed
H
# Special case: 10-19
g/1[0-9]$/s//&'th/
g/1$/s//&'st/
g/2$/s//&'nd/
g/3$/s//&'rd/
g/[0-9]$/s//&'th/
# Join by spaces, purely for aesthetics.
g/.*/s//& /
,j
,p
Q
- Output:
$ ed -s nth.input < nth.ed Newline appended 0'th 1'st 2'nd 3'rd 4'th 5'th 6'th 7'th 8'th 9'th 10'th 11'th 12'th 13'th 14'th 15'th 16'th 17'th 18'th 19'th 20'th 21'st 22'nd 23'rd 24'th 25'th 250'th 251'st 252'nd 253'rd 254'th 255'th 256'th 257'th 258'th 259'th 260'th 261'st 262'nd 263'rd 264'th 265'th 1000'th 1001'st 1002'nd 1003'rd 1004'th 1005'th 1006'th 1007'th 1008'th 1009'th 1010'th 1011'th 1012'th 1013'th 1014'th 1015'th 1016'th 1017'th 1018'th 1019'th 1020'th 1021'st 1022'nd 1023'rd 1024'th 1025'th
Elena
ELENA 6.x :
import extensions;
import system'math;
import system'routines;
extension op
{
ordinalize()
{
int i := self.Absolute;
if (new int[]{11,12,13}.ifExists(i.mod(100)))
{
^ i.toPrintable() + "th"
};
(i.mod(10)) =>
1 { ^ i.toPrintable() + "st" }
2 { ^ i.toPrintable() + "nd" }
3 { ^ i.toPrintable() + "rd" };
^ i.toPrintable() + "th"
}
}
public program()
{
console.printLine(new Range(0,26).selectBy(mssgconst ordinalize<op>[1]));
console.printLine(new Range(250,26).selectBy(mssgconst ordinalize<op>[1]));
console.printLine(new Range(1000,26).selectBy(mssgconst ordinalize<op>[1]))
}
- Output:
0th,1st,2nd,3rd,4th,5th,6th,7th,8th,9th,10th,11th,12th,13th,14th,15th,16th,17th,18th,19th,20th,21st,22nd,23rd,24th,25th 250th,251st,252nd,253rd,254th,255th,256th,257th,258th,259th,260th,261st,262nd,263rd,264th,265th,266th,267th,268th,269th,270th,271st,272nd,273rd,274th,275th 1000th,1001st,1002nd,1003rd,1004th,1005th,1006th,1007th,1008th,1009th,1010th,1011th,1012th,1013th,1014th,1015th,1016th,1017th,1018th,1019th,1020th,1021st,1022nd,1023rd,1024th,1025th
Elixir
defmodule RC do
def ordinalize(n) do
num = abs(n)
ordinal = if rem(num, 100) in 4..20 do
"th"
else
case rem(num, 10) do
1 -> "st"
2 -> "nd"
3 -> "rd"
_ -> "th"
end
end
"#{n}#{ordinal}"
end
end
Enum.each([0..25, 250..265, 1000..1025], fn range ->
Enum.map(range, fn n -> RC.ordinalize(n) end) |> Enum.join(" ") |> IO.puts
end)
- Output:
0th 1st 2nd 3rd 4th 5th 6th 7th 8th 9th 10th 11th 12th 13th 14th 15th 16th 17th 18th 19th 20th 21st 22nd 23rd 24th 25th 250th 251st 252nd 253rd 254th 255th 256th 257th 258th 259th 260th 261st 262nd 263rd 264th 265th 1000th 1001st 1002nd 1003rd 1004th 1005th 1006th 1007th 1008th 1009th 1010th 1011th 1012th 1013th 1014th 1015th 1016th 1017th 1018th 1019th 1020th 1021st 1022nd 1023rd 1024th 1025th
ERRE
PROGRAM NTH_SOLVE
!
! for rosettacode.org
!
PROCEDURE NTH(S%,E%)
LOCAL I%,SUFF$
FOR I%=S% TO E% DO
SUFF$="th"
IF I% MOD 10=1 AND I% MOD 100<>11 THEN SUFF$="st" END IF
IF I% MOD 10=2 AND I% MOD 100<>12 THEN SUFF$="nd" END IF
IF I% MOD 10=3 AND I% MOD 100<>13 THEN SUFF$="rd" END IF
PRINT(STR$(I%)+SUFF$+" ";)
END FOR
PRINT
END PROCEDURE
BEGIN
NTH(0,25)
NTH(250,265)
NTH(1000,1025)
END PROGRAM
- Output:
0th 1st 2nd 3rd 4th 5th 6th 7th 8th 9th 10th 11th 12th 13th 14th 15th 16th 17th 18th 19th 20th 21st 22nd 23rd 24th 25th 250th 251st 252nd 253rd 254th 255th 256th 257th 258th 259th 260th 261st 262nd 263rd 264th 265th 1000th 1001st 1002nd 1003rd 1004th 1005th 1006th 1007th 1008th 1009th 1010th 1011th 1012th 1013th 1014th 1015th 1016th 1017th 1018th 1019th 1020th 1021st 1022nd 1023rd 1024th 1025th
F#
open System
let ordinalsuffix n =
let suffixstrings = [|"th"; "st"; "nd"; "rd"|]
let (d, r) = Math.DivRem(n, 10)
n.ToString() + suffixstrings.[ if r < 4 && (d &&& 1) = 0 then r else 0 ]
[<EntryPoint>]
let main argv =
let show = (Seq.iter (ordinalsuffix >> (printf " %s"))) >> (Console.WriteLine)
[0..25] |> show
[250..265] |> show
[1000..1025] |> show
0
- Output:
0th 1st 2nd 3rd 4th 5th 6th 7th 8th 9th 10th 11th 12th 13th 14th 15th 16th 17th 18th 19th 20th 21st 22nd 23rd 24th 25th 250th 251th 252th 253th 254th 255th 256th 257th 258th 259th 260th 261st 262nd 263rd 264th 265th 1000th 1001st 1002nd 1003rd 1004th 1005th 1006th 1007th 1008th 1009th 1010th 1011th 1012th 1013th 1014th 1015th 1016th 1017th 1018th 1019th 1020th 1021st 1022nd 1023rd 1024th 1025th
Factor
USING: io kernel math math.order math.parser math.ranges qw
sequences ;
IN: rosetta-code.nth
: n'th ( n -- str )
dup 10 /mod swap 1 = [ drop 0 ] when
[ number>string ]
[ 4 min qw{ th st nd rd th } nth ] bi* append ;
: n'th-demo ( -- )
0 25 250 265 1000 1025 [ [a,b] ] 2tri@
[ [ n'th write bl ] each nl ] tri@ ;
MAIN: n'th-demo
- Output:
0th 1st 2nd 3rd 4th 5th 6th 7th 8th 9th 10th 11th 12th 13th 14th 15th 16th 17th 18th 19th 20th 21st 22nd 23rd 24th 25th 250th 251st 252nd 253rd 254th 255th 256th 257th 258th 259th 260th 261st 262nd 263rd 264th 265th 1000th 1001st 1002nd 1003rd 1004th 1005th 1006th 1007th 1008th 1009th 1010th 1011th 1012th 1013th 1014th 1015th 1016th 1017th 1018th 1019th 1020th 1021st 1022nd 1023rd 1024th 1025th
Fennel
(fn get-suffix [n]
(let [last-two (% n 100)
last-one (% n 10)]
(if (and (> last-two 3) (< last-two 21)) :th
(= last-one 1) :st
(= last-one 2) :nd
(= last-one 3) :rd
:th)))
(fn nth [n]
(.. n "'" (get-suffix n)))
(for [i 0 25]
(print (nth i) (nth (+ i 250)) (nth (+ i 1000))))
- Output:
Same as Lua.
Forth
: 'nth ( -- c-addr ) s" th st nd rd th th th th th th " drop ;
: .nth ( n -- )
dup 100 mod 10 20 within if 0 .r ." th " exit then
dup 0 .r 10 mod 3 * 'nth + 3 type ;
: test ( n n -- ) cr do i 5 mod 0= if cr then i .nth loop ;
: tests ( -- )
26 0 test 266 250 test 1026 1000 test ;
tests
- Output:
0th 1st 2nd 3rd 4th 5th 6th 7th 8th 9th 10th 11th 12th 13th 14th 15th 16th 17th 18th 19th 20th 21st 22nd 23rd 24th 25th 250th 251st 252nd 253rd 254th 255th 256th 257th 258th 259th 260th 261st 262nd 263rd 264th 265th 1000th 1001st 1002nd 1003rd 1004th 1005th 1006th 1007th 1008th 1009th 1010th 1011th 1012th 1013th 1014th 1015th 1016th 1017th 1018th 1019th 1020th 1021st 1022nd 1023rd 1024th 1022th ok
Fortran
THE INSTRUCTIONS! Write a function/method/subroutine/... that when given an integer greater than or equal to zero returns a string of the number followed by an apostrophe then the ordinal suffix. Example returns would include 1'st 2'nd 3'rd 11'th 111'th 1001'st 1012'th
Please find the compilation instructions and examples in comments at the start of the source.
!-*- mode: compilation; default-directory: "/tmp/" -*-
!Compilation started at Fri Jun 6 15:40:18
!
!a=./f && make -k $a && echo 0 25 | $a && echo 250 265 | $a && echo 1000 1025 | $a
!gfortran -std=f2008 -Wall -fopenmp -ffree-form -fall-intrinsics -fimplicit-none -g f.f08 -o f
! 0'th 1'st 2'nd
! 3'rd 4'th 5'th
! 6'th 7'th 8'th
! 9'th 10'th 11'th
! 12'th 13'th 14'th
! 15'th 16'th 17'th
! 18'th 19'th 20'th
! 21'st 22'nd 23'rd
! 24'th 25'th
! 250'th 251'st
! 252'nd 253'rd 254'th
! 255'th 256'th 257'th
! 258'th 259'th 260'th
! 261'st 262'nd 263'rd
! 264'th 265'th
! 1000th 1001st
! 1002nd 1003rd 1004th
! 1005th 1006th 1007th
! 1008th 1009th 1010th
! 1011th 1012th 1013th
! 1014th 1015th 1016th
! 1017th 1018th 1019th
! 1020th 1021st 1022nd
! 1023rd 1024th 1025th
!
!Compilation finished at Fri Jun 6 15:40:18
program nth
implicit none
logical :: need
integer :: here, there, n, i, iostat
read(5,*,iostat=iostat) here, there
if (iostat .ne. 0) then
write(6,*)'such bad input never before seen.'
write(6,*)'I AYE EYE QUIT!'
call exit(1)
end if
need = .false.
n = abs(there - here) + 1
i = 0
do while (0 /= mod(3+mod(here-i, 3), 3))
write(6,'(a22)',advance='no') ''
i = i+1
end do
do i = here, there, sign(1, there-here)
write(6,'(a22)',advance='no') ordinate(i)
if (2 /= mod(i,3)) then
need = .true.
else
write(6,'(a)')''
need = .false.
end if
end do
if (need) write(6,'(a)')''
contains
character(len=22) function ordinate(n)
character(len=19) :: a
character(len=20), parameter :: &
&a09 = "thstndrdthththththth",&
&ateen = "thththththththththth"
integer :: ones, tens, ones_index
integer, intent(in) :: n
write(a,'(i19)') n
ones = mod(n,10)
tens = mod(n,100)
ones_index = ones*2+1
if (n < 1000) then
if ((10 .le. tens) .and. (tens .lt. 20)) then
ordinate = a // "'" // ateen(ones_index:ones_index+1)
! ^^^^^^ remove these characters to remove the important '
else
ordinate = a // "'" // a09(ones_index:ones_index+1)
! ^^^^^^ remove these characters to remove the important '
end if
else
if ((10 .le. tens) .and. (tens .lt. 20)) then
ordinate = a // ateen(ones_index:ones_index+1)
else
ordinate = a // a09(ones_index:ones_index+1)
end if
end if
end function ordinate
end program nth
Go
package main
import "fmt"
func ord(n int) string {
s := "th"
switch c := n % 10; c {
case 1, 2, 3:
if n%100/10 == 1 {
break
}
switch c {
case 1:
s = "st"
case 2:
s = "nd"
case 3:
s = "rd"
}
}
return fmt.Sprintf("%d%s", n, s)
}
func main() {
for n := 0; n <= 25; n++ {
fmt.Printf("%s ", ord(n))
}
fmt.Println()
for n := 250; n <= 265; n++ {
fmt.Printf("%s ", ord(n))
}
fmt.Println()
for n := 1000; n <= 1025; n++ {
fmt.Printf("%s ", ord(n))
}
fmt.Println()
}
- Output:
0th 1st 2nd 3rd 4th 5th 6th 7th 8th 9th 10th 11th 12th 13th 14th 15th 16th 17th 18th 19th 20th 21st 22nd 23rd 24th 25th 250th 251st 252nd 253rd 254th 255th 256th 257th 258th 259th 260th 261st 262nd 263rd 264th 265th 1000th 1001st 1002nd 1003rd 1004th 1005th 1006th 1007th 1008th 1009th 1010th 1011th 1012th 1013th 1014th 1015th 1016th 1017th 1018th 1019th 1020th 1021st 1022nd 1023rd 1024th 1025th
Haskell
import Data.Array
ordSuffs :: Array Integer String
ordSuffs = listArray (0,9) ["th", "st", "nd", "rd", "th",
"th", "th", "th", "th", "th"]
ordSuff :: Integer -> String
ordSuff n = show n ++ suff n
where suff m | (m `rem` 100) >= 11 && (m `rem` 100) <= 13 = "th"
| otherwise = ordSuffs ! (m `rem` 10)
printOrdSuffs :: [Integer] -> IO ()
printOrdSuffs = putStrLn . unwords . map ordSuff
main :: IO ()
main = do
printOrdSuffs [ 0.. 25]
printOrdSuffs [ 250.. 265]
printOrdSuffs [1000..1025]
- Output:
0th 1st 2nd 3rd 4th 5th 6th 7th 8th 9th 10th 11th 12th 13th 14th 15th 16th 17th 18th 19th 20th 21st 22nd 23rd 24th 25th 250th 251st 252nd 253rd 254th 255th 256th 257th 258th 259th 260th 261st 262nd 263rd 264th 265th 1000th 1001st 1002nd 1003rd 1004th 1005th 1006th 1007th 1008th 1009th 1010th 1011th 1012th 1013th 1014th 1015th 1016th 1017th 1018th 1019th 1020th 1021st 1022nd 1023rd 1024th 1025th
Icon and Unicon
The following works in both languages.
procedure main(A)
every writes(" ",nth(0 to 25) | "\n")
every writes(" ",nth(250 to 265) | "\n")
every writes(" ",nth(1000 to 1025) | "\n")
end
procedure nth(n)
return n || ((n%10 = 1, n%100 ~= 11, "st") |
(n%10 = 2, n%100 ~= 12, "nd") |
(n%10 = 3, n%100 ~= 13, "rd") | "th")
end
- Output:
->nth 0th 1st 2nd 3rd 4th 5th 6th 7th 8th 9th 10th 11th 12th 13h 14th 15th 16th 17th 18th 19th 20th 21st 22nd 23rd 24th 25th 250th 251st 252nd 253rd 254th 255th 256th 257th 258th 259th 260th 261st 262nd 263rd 264th 265th 1000th 1001st 1002nd 1003rd 1004th 1005th 1006th 1007th 1008th 1009th 1010th 1011th 1012th 1013h 1014th 1015th 1016th 1017th 1018th 1019th 1020th 1021st 1022nd 1023rd 1024th 1025th ->
Insitux
(function ordinal n
(if ([11 12 13] (rem n 100))
(str n "th")
(str n (or ((rem n 10) ["th" "st" "nd" "rd"]) "th"))))
(function line x y
(-> (range x y)
(map (comp ordinal (pad-left " " 6)))
(join " ")))
(.. str (line 0 13) "\n" (line 1000 1013)
"\n\n" (line 13 26) "\n" (line 1013 1026)
"\n\n" (line 250 266))
- Output:
0th 1st 2nd 3rd 4th 5th 6th 7th 8th 9th 10th 11th 12th 1000th 1001st 1002nd 1003rd 1004th 1005th 1006th 1007th 1008th 1009th 1010th 1011th 1012th 13th 14th 15th 16th 17th 18th 19th 20th 21st 22nd 23rd 24th 25th 1013th 1014th 1015th 1016th 1017th 1018th 1019th 1020th 1021st 1022nd 1023rd 1024th 1025th 250th 251st 252nd 253rd 254th 255th 256th 257th 258th 259th 260th 261st 262nd 263rd 264th 265th
J
Implementation:
suf=: (;:'th st nd rd th') {::~ 4 <. 10 10 (* 1&~:)~/@#: ]
nth=: [: ;:inv (": , suf)each
Task:
thru=: <./ + i.@(+ *)@-~
nth 0 thru 25
0th 1st 2nd 3rd 4th 5th 6th 7th 8th 9th 10th 11th 12th 13th 14th 15th 16th 17th 18th 19th 20th 21st 22nd 23rd 24th 25th
nth 250 thru 265
250th 251st 252nd 253rd 254th 255th 256th 257th 258th 259th 260th 261st 262nd 263rd 264th 265th
nth 1000 thru 1025
1000th 1001st 1002nd 1003rd 1004th 1005th 1006th 1007th 1008th 1009th 1010th 1011th 1012th 1013th 1014th 1015th 1016th 1017th 1018th 1019th 1020th 1021st 1022nd 1023rd 1024th 1025th
Java
public class Nth {
public static String ordinalAbbrev(int n){
String ans = "th"; //most of the time it should be "th"
if(n % 100 / 10 == 1) return ans; //teens are all "th"
switch(n % 10){
case 1: ans = "st"; break;
case 2: ans = "nd"; break;
case 3: ans = "rd"; break;
}
return ans;
}
public static void main(String[] args){
for(int i = 0; i <= 25;i++){
System.out.print(i + ordinalAbbrev(i) + " ");
}
System.out.println();
for(int i = 250; i <= 265;i++){
System.out.print(i + ordinalAbbrev(i) + " ");
}
System.out.println();
for(int i = 1000; i <= 1025;i++){
System.out.print(i + ordinalAbbrev(i) + " ");
}
}
}
package nth;
import java.util.stream.IntStream;
import java.util.stream.Stream;
public interface Nth {
public static String suffix(int n){
if(n % 100 / 10 == 1){
return "th"; //teens are all "th"
}
switch(n % 10){
case 1: return "st";
case 2: return "nd";
case 3: return "rd";
default: return "th"; //most of the time it should be "th"
}
}
public static void print(int start, int end) {
IntStream.rangeClosed(start, end)
.parallel()
.mapToObj(i -> i + suffix(i) + " ")
.reduce(String::concat)
.ifPresent(System.out::println)
;
}
public static void print(int[] startAndEnd) {
print(startAndEnd[0], startAndEnd[1]);
}
public static int[] startAndEnd(int start, int end) {
return new int[] {
start,
end
};
}
public static void main(String... arguments){
Stream.of(
startAndEnd(0, 25),
startAndEnd(250, 265),
startAndEnd(1000, 1025)
)
.forEach(Nth::print)
;
}
}
- Output:
0th 1st 2nd 3rd 4th 5th 6th 7th 8th 9th 10th 11th 12th 13th 14th 15th 16th 17th 18th 19th 20th 21st 22nd 23rd 24th 25th 250th 251st 252nd 253rd 254th 255th 256th 257th 258th 259th 260th 261st 262nd 263rd 264th 265th 1000th 1001st 1002nd 1003rd 1004th 1005th 1006th 1007th 1008th 1009th 1010th 1011th 1012th 1013th 1014th 1015th 1016th 1017th 1018th 1019th 1020th 1021st 1022nd 1023rd 1024th 1025th
JavaScript
ES5
console.log(function () {
var lstSuffix = 'th st nd rd th th th th th th'.split(' '),
fnOrdinalForm = function (n) {
return n.toString() + (
11 <= n % 100 && 13 >= n % 100 ?
"th" : lstSuffix[n % 10]
);
},
range = function (m, n) {
return Array.apply(
null, Array(n - m + 1)
).map(function (x, i) {
return m + i;
});
};
return [[0, 25], [250, 265], [1000, 1025]].map(function (tpl) {
return range.apply(null, tpl).map(fnOrdinalForm).join(' ');
}).join('\n\n');
}());
- Output:
0th 1st 2nd 3rd 4th 5th 6th 7th 8th 9th 10th 11th 12th 13th 14th 15th 16th 17th 18th 19th 20th 21st 22nd 23rd 24th 25th
250th 251st 252nd 253rd 254th 255th 256th 257th 258th 259th 260th 261st 262nd 263rd 264th 265th
1000th 1001st 1002nd 1003rd 1004th 1005th 1006th 1007th 1008th 1009th 1010th 1011th 1012th 1013th 1014th 1015th 1016th 1017th 1018th 1019th 1020th 1021st 1022nd 1023rd 1024th 1025th
ES6
(function (lstTestRanges) {
'use strict'
let lstSuffix = 'th st nd rd th th th th th th'.split(' '),
// ordinalString :: Int -> String
ordinalString = n =>
n.toString() + (
11 <= n % 100 && 13 >= n % 100 ?
"th" : lstSuffix[n % 10]
),
// range :: Int -> Int -> [Int]
range = (m, n) =>
Array.from({
length: (n - m) + 1
}, (_, i) => m + i);
return lstTestRanges
.map(tpl => range
.apply(null, tpl)
.map(ordinalString)
);
})([[0, 25], [250, 265], [1000, 1025]]);
- Output:
[["0th", "1st", "2nd", "3rd", "4th", "5th", "6th", "7th", "8th", "9th", "10th", "11th", "12th", "13th", "14th", "15th", "16th", "17th", "18th", "19th", "20th", "21st", "22nd", "23rd", "24th", "25th"], ["250th", "251st", "252nd", "253rd", "254th", "255th", "256th", "257th", "258th", "259th", "260th", "261st", "262nd", "263rd", "264th", "265th"], ["1000th", "1001st", "1002nd", "1003rd", "1004th", "1005th", "1006th", "1007th", "1008th", "1009th", "1010th", "1011th", "1012th", "1013th", "1014th", "1015th", "1016th", "1017th", "1018th", "1019th", "1020th", "1021st", "1022nd", "1023rd", "1024th", "1025th"]]
Joy
DEFINE
suffix == abs 89 + 100 rem 9 - 10 rem [{1 2 3} in] [] [pop 0] ifte ["th" "st" "nd" "rd"] of;
nth == ['d 1 1 format] [suffix] cleave concat.
[0 250 1000] [25 [dup nth putchars ' putch succ] times nth putchars '\n putch] step.
- Output:
0th 1st 2nd 3rd 4th 5th 6th 7th 8th 9th 10th 11th 12th 13th 14th 15th 16th 17th 18th 19th 20th 21st 22nd 23rd 24th 25th 250th 251st 252nd 253rd 254th 255th 256th 257th 258th 259th 260th 261st 262nd 263rd 264th 265th 266th 267th 268th 269th 270th 271st 272nd 273rd 274th 275th 1000th 1001st 1002nd 1003rd 1004th 1005th 1006th 1007th 1008th 1009th 1010th 1011th 1012th 1013th 1014th 1015th 1016th 1017th 1018th 1019th 1020th 1021st 1022nd 1023rd 1024th 1025th
jq
# ordinalize an integer input, positive or negative
def ordinalize:
(if . < 0 then -(.) else . end) as $num
| ($num % 100) as $small
| (if 11 <= $small and $small <= 13 then "th"
else
( $num % 10)
| (if . == 1 then "st"
elif . == 2 then "nd"
elif . == 3 then "rd"
else "th"
end)
end) as $ordinal
| "\(.)\($ordinal)" ;
([range(-5; -1)], [range(0;26)], [range(250;266)], [range(1000;1026)])
| map(ordinalize)
- Output:
["-5th","-4th","-3rd","-2nd"] ["0th","1st","2nd","3rd","4th","5th","6th","7th","8th","9th","10th","11th","12th","13th","14th","15th","16th","17th","18th","19th","20th","21st","22nd","23rd","24th","25th"] ["250th","251st","252nd","253rd","254th","255th","256th","257th","258th","259th","260th","261st","262nd","263rd","264th","265th"] ["1000th","1001st","1002nd","1003rd","1004th","1005th","1006th","1007th","1008th","1009th","1010th","1011th","1012th","1013th","1014th","1015th","1016th","1017th","1018th","1019th","1020th","1021st","1022nd","1023rd","1024th","1025th"]
Julia
using Printf
Function:
function ordinal(n::Integer)
n < 0 && throw(DomainError())
suffixes = ("st", "nd", "rd")
u = n % 10
t = n ÷ 10 % 10
if u > 3 || u == 0 || t == 1
suf = "th"
else
suf = suffixes[u]
end
return string(n, suf)
end
Main:
println("Tests of ordinal formatting of integers.")
for (i, n) in enumerate(0:25)
(i - 1) % 10 == 0 && println()
@printf("%7s", ordinal(n))
end
println()
for (i, n) in enumerate(250:265)
(i - 1) % 10 == 0 && println()
@printf("%7s", ordinal(n))
end
println()
for (i, n) in enumerate(1000:1025)
(i - 1) % 10 == 0 && println()
@printf("%7s", ordinal(n))
end
Main2:
Nth(x::Integer) = if x % 100 ∈ [11, 12, 13] "th" else ["th", "st", "nd", "rd", "th"][min(x % 10 + 1, 5)] end
NthA(x::Integer) = "$(x)'$(Nth(x)) "
[0:25..., 250:265..., 1000:1025...] .|> NthA .|> print;
- Output:
Tests of ordinal formatting of integers. 0th 1st 2nd 3rd 4th 5th 6th 7th 8th 9th 10th 11th 12th 13th 14th 15th 16th 17th 18th 19th 20th 21st 22nd 23rd 24th 25th 250th 251st 252nd 253rd 254th 255th 256th 257th 258th 259th 260th 261st 262nd 263rd 264th 265th 1000th 1001st 1002nd 1003rd 1004th 1005th 1006th 1007th 1008th 1009th 1010th 1011th 1012th 1013th 1014th 1015th 1016th 1017th 1018th 1019th 1020th 1021st 1022nd 1023rd 1024th 1025th
Kotlin
fun Int.ordinalAbbrev() =
if (this % 100 / 10 == 1) "th"
else when (this % 10) { 1 -> "st" 2 -> "nd" 3 -> "rd" else -> "th" }
fun IntRange.ordinalAbbrev() = map { "$it" + it.ordinalAbbrev() }.joinToString(" ")
fun main(args: Array<String>) {
listOf((0..25), (250..265), (1000..1025)).forEach { println(it.ordinalAbbrev()) }
}
Lambdatalk
Translation from the javascript entry
{def fnOrdinalForm
{lambda {:n}
{if {and {<= 11 {% :n 100}} {>= 13 {% :n 100}}}
then :nth
else :n{A.get {% :n 10} th st nd rd th th th th th th}}}}
-> fnOrdinalForm
{S.map fnOrdinalForm {S.serie 0 25}}
-> 0th 1st 2nd 3rd 4th 5th 6th 7th 8th 9th 10th 11th 12th 13th 14th 15th
16th 17th 18th 19th 20th 21st 22nd 23rd 24th 25th
{S.map fnOrdinalForm {S.serie 250 265}}
-> 250th 251st 252nd 253rd 254th 255th 256th 257th 258th 259th 260th
261st 262nd 263rd 264th 265th
{S.map fnOrdinalForm {S.serie 1000 1025}}
-> 1000th 1001st 1002nd 1003rd 1004th 1005th 1006th 1007th 1008th 1009th 1010th
1011th 1012th 1013th 1014th 1015th 1016th 1017th 1018th 1019th 1020th
1021st 1022nd 1023rd 1024th 1025th
Lua
The apostrophe just looks weird if you ask me. No one did, obviously.
function getSuffix (n)
local lastTwo, lastOne = n % 100, n % 10
if lastTwo > 3 and lastTwo < 21 then return "th" end
if lastOne == 1 then return "st" end
if lastOne == 2 then return "nd" end
if lastOne == 3 then return "rd" end
return "th"
end
function Nth (n) return n .. "'" .. getSuffix(n) end
for i = 0, 25 do print(Nth(i), Nth(i + 250), Nth(i + 1000)) end
- Output:
0'th 250'th 1000'th 1'st 251'st 1001'st 2'nd 252'nd 1002'nd 3'rd 253'rd 1003'rd 4'th 254'th 1004'th 5'th 255'th 1005'th 6'th 256'th 1006'th 7'th 257'th 1007'th 8'th 258'th 1008'th 9'th 259'th 1009'th 10'th 260'th 1010'th 11'th 261'st 1011'th 12'th 262'nd 1012'th 13'th 263'rd 1013'th 14'th 264'th 1014'th 15'th 265'th 1015'th 16'th 266'th 1016'th 17'th 267'th 1017'th 18'th 268'th 1018'th 19'th 269'th 1019'th 20'th 270'th 1020'th 21'st 271'st 1021'st 22'nd 272'nd 1022'nd 23'rd 273'rd 1023'rd 24'th 274'th 1024'th 25'th 275'th 1025'th
Maple
toOrdinal := proc(n:: nonnegint)
if 1 <= n and n <= 10 then
if n >= 4 then
printf("%ath", n);
elif n = 3 then
printf("%ard", n);
elif n = 2 then
printf("%and", n);
else
printf("%ast", n);
end if:
else
printf(convert(n, 'ordinal'));
end if:
return NULL;
end proc:
a := [[0, 25], [250, 265], [1000, 1025]]:
for i in a do
for j from i[1] to i[2] do
toOrdinal(j);
printf(" ");
end do;
printf("\n\n");
end do;
- Output:
0th 1st 2nd 3rd 4th 5th 6th 7th 8th 9th 10th 11th 12th 13th 14th 15th 16th 17th 18th 19th 20th 21st 22nd 23rd 24th 25th 250th 251st 252nd 253rd 254th 255th 256th 257th 258th 259th 260th 261st 262nd 263rd 264th 265th 1000th 1001st 1002nd 1003rd 1004th 1005th 1006th 1007th 1008th 1009th 1010th 1011th 1012th 1013th 1014th 1015th 1016th 1017th 1018th 1019th 1020th 1021st 1022nd 1023rd 1024th 1025th
Mathematica/Wolfram Language
I borrowed the logic from the Python code.
suffixlist = {"th", "st", "nd", "rd", "th", "th", "th", "th", "th","th"};
addsuffix[n_] := Module[{suffix},
suffix = Which[
Mod[n, 100] <= 10, suffixlist[[Mod[n, 10] + 1]],
Mod[n, 100] > 20, suffixlist[[Mod[n, 10] + 1]],
True, "th"
];
ToString[n] <> suffix
]
addsuffix[#] & /@ Range[0, 25] (* test 1 *)
addsuffix[#] & /@ Range[250, 265] (* test 2 *)
addsuffix[#] & /@ Range[1000, 1025] (* test 3 *)
- Output:
{"0th", "1st", "2nd", "3rd", "4th", "5th", "6th", "7th", "8th", "9th", "10th", "11th", "12th", "13th", "14th", "15th", "16th", "17th", "18th", "19th", "20th", "21st", "22nd", "23rd", "24th", "25th"} {"250th", "251st", "252nd", "253rd", "254th", "255th", "256th", "257th", "258th", "259th", "260th", "261st", "262nd", "263rd", "264th", "265th"} {"1000th", "1001st", "1002nd", "1003rd", "1004th", "1005th", "1006th", "1007th", "1008th", "1009th", "1010th", "1011th", "1012th", "1013th", "1014th", "1015th", "1016th", "1017th", "1018th", "1019th", "1020th", "1021st", "1022nd", "1023rd", "1024th", "1025th"}
MATLAB
function s = nth(n)
tens = mod(n, 100);
if tens > 9 && tens < 20
suf = 'th';
else
switch mod(n, 10)
case 1
suf = 'st';
case 2
suf = 'nd';
case 3
suf = 'rd';
otherwise
suf = 'th';
end
end
s = sprintf('%d%s', n, suf);
end
- Output:
0th 1st 2nd 3rd 4th 5th 6th 7th 8th 9th 10th 11th 12th 13th 14th 15th 16th 17th 18th 19th 20th 21st 22nd 23rd 24th 25th 250th 251st 252nd 253rd 254th 255th 256th 257th 258th 259th 260th 261st 262nd 263rd 264th 265th 1000th 1001st 1002nd 1003rd 1004th 1005th 1006th 1007th 1008th 1009th 1010th 1011th 1012th 1013th 1014th 1015th 1016th 1017th 1018th 1019th 1020th 1021st 1022nd 1023rd 1024th 1025th
MiniScript
To get the output all on one line, we append it to a list as we go, and then print the list all at once at the end.
ordinal = function(n)
if n % 100 > 3 and n %100 < 20 then return n + "th"
if n % 10 == 1 then return n + "st"
if n % 10 == 2 then return n + "nd"
if n % 10 == 3 then return n + "rd"
return n + "th"
end function
test = function(from, to)
out = []
for i in range(from, to)
out.push ordinal(i)
end for
print out.join
end function
test 0, 25
test 250, 265
test 1000, 1025
- Output:
0th 1st 2nd 3rd 4th 5th 6th 7th 8th 9th 10th 11th 12th 13th 14th 15th 16th 17th 18th 19th 20th 21st 22nd 23rd 24th 25th 250th 251st 252nd 253rd 254th 255th 256th 257th 258th 259th 260th 261st 262nd 263rd 264th 265th 1000th 1001st 1002nd 1003rd 1004th 1005th 1006th 1007th 1008th 1009th 1010th 1011th 1012th 1013th 1014th 1015th 1016th 1017th 1018th 1019th 1020th 1021st 1022nd 1023rd 1024th 1025th
Modula-2
MODULE Nth;
FROM STextIO IMPORT
WriteString, WriteLn;
FROM WholeStr IMPORT
IntToStr;
PROCEDURE Suffix(N: CARDINAL; VAR OUT Destination: ARRAY OF CHAR);
VAR
NMod10, NMod100: CARDINAL;
BEGIN
NMod10 := N MOD 10;
NMod100 := N MOD 100;
IF (NMod10 = 1) AND (NMod100 <> 11) THEN
Destination := "st";
ELSIF (NMod10 = 2) AND (NMod100 <> 12) THEN
Destination := "nd";
ELSIF (NMod10 = 3) AND (NMod100 <> 13) THEN
Destination := "rd";
ELSE
Destination := "th";
END;
END Suffix;
PROCEDURE PrintImages(LoLim, HiLim: CARDINAL);
VAR
I: CARDINAL;
IString: ARRAY [0 .. 15] OF CHAR;
ISuff: ARRAY [0 .. 1] OF CHAR;
BEGIN
FOR I := LoLim TO HiLim DO
IntToStr(I, IString);
Suffix(I, ISuff);
WriteString(IString);
WriteString(ISuff);
WriteString(" ");
END;
WriteLn;
END PrintImages;
BEGIN
PrintImages( 0, 25);
PrintImages( 250, 265);
PrintImages(1000, 1025);
END Nth.
Nanoquery
def ordinalAbbrev(n)
if int(n % 100 / 10) = 1
return "th"
end
if n % 10 = 1
return "st"
else if n % 10 = 2
return "nd"
else if n % 10 = 3
return "rd"
else
return "th"
end
end
for i in range(0, 25)
print (i + "'" + ordinalAbbrev(i) + " ")
end
println
for i in range(250, 265)
print (i + "'" + ordinalAbbrev(i) + " ")
end
println
for i in range(1000, 1025)
print (i + "'" + ordinalAbbrev(i) + " ")
end
println
- Output:
0'th 1'st 2'nd 3'rd 4'th 5'th 6'th 7'th 8'th 9'th 10'th 11'th 12'th 13'th 14'th 15'th 16'th 17'th 18'th 19'th 20'th 21'st 22'nd 23'rd 24'th 25'th 250'th 251'st 252'nd 253'rd 254'th 255'th 256'th 257'th 258'th 259'th 260'th 261'st 262'nd 263'rd 264'th 265'th 1000'th 1001'st 1002'nd 1003'rd 1004'th 1005'th 1006'th 1007'th 1008'th 1009'th 1010'th 1011'th 1012'th 1013'th 1014'th 1015'th 1016'th 1017'th 1018'th 1019'th 1020'th 1021'st 1022'nd 1023'rd 1024'th 1025'th
Nim
const Suffix = ["th", "st", "nd", "rd", "th", "th", "th", "th", "th", "th"]
proc nth(n: Natural): string =
$n & "'" & (if n mod 100 in 11..20: "th" else: Suffix[n mod 10])
for j in countup(0, 1000, 250):
for i in j..j+24:
stdout.write nth(i), " "
echo ""
- Output:
0'th 1'st 2'nd 3'rd 4'th 5'th 6'th 7'th 8'th 9'th 10'th 11'th 12'th 13'th 14'th 15'th 16'th 17'th 18'th 19'th 20'th 21'st 22'nd 23'rd 24'th 250'th 251'st 252'nd 253'rd 254'th 255'th 256'th 257'th 258'th 259'th 260'th 261'st 262'nd 263'rd 264'th 265'th 266'th 267'th 268'th 269'th 270'th 271'st 272'nd 273'rd 274'th 500'th 501'st 502'nd 503'rd 504'th 505'th 506'th 507'th 508'th 509'th 510'th 511'th 512'th 513'th 514'th 515'th 516'th 517'th 518'th 519'th 520'th 521'st 522'nd 523'rd 524'th 750'th 751'st 752'nd 753'rd 754'th 755'th 756'th 757'th 758'th 759'th 760'th 761'st 762'nd 763'rd 764'th 765'th 766'th 767'th 768'th 769'th 770'th 771'st 772'nd 773'rd 774'th 1000'th 1001'st 1002'nd 1003'rd 1004'th 1005'th 1006'th 1007'th 1008'th 1009'th 1010'th 1011'th 1012'th 1013'th 1014'th 1015'th 1016'th 1017'th 1018'th 1019'th 1020'th 1021'st 1022'nd 1023'rd 1024'th
Objeck
class Nth {
function : OrdinalAbbrev(n : Int ) ~ String {
ans := "th"; # most of the time it should be "th"
if(n % 100 / 10 = 1) {
return ans; # teens are all "th"
};
select(n % 10){
label 1: { ans := "st"; }
label 2: { ans := "nd"; }
label 3: { ans := "rd"; }
};
return ans;
}
function : Main(args : String[]) ~ Nil {
for(i := 0; i <= 25; i+=1;) {
abbr := OrdinalAbbrev(i);
"{$i}{$abbr} "->Print();
};
""->PrintLine();
for(i := 250; i <= 265; i+=1;) {
abbr := OrdinalAbbrev(i);
"{$i}{$abbr} "->Print();
};
""->PrintLine();
for(i := 1000; i <= 1025; i+=1;) {
abbr := OrdinalAbbrev(i);
"{$i}{$abbr} "->Print();
};
}
}
- Output:
0th 1st 2nd 3rd 4th 5th 6th 7th 8th 9th 10th 11th 12th 13th 14th 15th 16th 17th 18th 19th 20th 21st 22nd 23rd 24th 25th 250th 251st 252nd 253rd 254th 255th 256th 257th 258th 259th 260th 261st 262nd 263rd 264th 265th 1000th 1001st 1002nd 1003rd 1004th 1005th 1006th 1007th 1008th 1009th 1010th 1011th 1012th 1013th 1014th 1015th 1016th 1017th 1018th 1019th 1020th 1021st 1022nd 1023rd 1024th 1025th
OCaml
let show_nth n =
if (n mod 10 = 1) && (n mod 100 <> 11) then "st"
else if (n mod 10 = 2) && (n mod 100 <> 12) then "nd"
else if (n mod 10 = 3) && (n mod 100 <> 13) then "rd"
else "th"
let () =
let show_ordinals (min, max) =
for i=min to max do
Printf.printf "%d%s " i (show_nth i)
done;
print_newline() in
List.iter show_ordinals [ (0,25); (250,265); (1000,1025) ]
- Output:
0th 1st 2nd 3rd 4th 5th 6th 7th 8th 9th 10th 11th 12th 13th 14th 15th 16th 17th 18th 19th 20th 21st 22nd 23rd 24th 25th 250th 251st 252nd 253rd 254th 255th 256th 257th 258th 259th 260th 261st 262nd 263rd 264th 265th 1000th 1001st 1002nd 1003rd 1004th 1005th 1006th 1007th 1008th 1009th 1010th 1011th 1012th 1013th 1014th 1015th 1016th 1017th 1018th 1019th 1020th 1021st 1022nd 1023rd 1024th 1025th
Oforth
: nth(n)
| r |
n "th" over 10 mod ->r
r 1 == ifTrue: [ n 100 mod 11 == ifFalse: [ drop "st" ] ]
r 2 == ifTrue: [ n 100 mod 12 == ifFalse: [ drop "nd" ] ]
r 3 == ifTrue: [ n 100 mod 13 == ifFalse: [ drop "rd" ] ]
+ ;
- Output:
seqFrom(0, 25) map(#nth) println [0th, 1st, 2nd, 3rd, 4th, 5th, 6th, 7th, 8th, 9th, 10th, 11th, 12th, 13th, 14th, 15th, 16t h, 17th, 18th, 19th, 20th, 21st, 22nd, 23rd, 24th, 25th] seqFrom(250, 265) map(#nth) println [250th, 251st, 252nd, 253rd, 254th, 255th, 256th, 257th, 258th, 259th, 260th, 261st, 262nd , 263rd, 264th, 265th] seqFrom(1000, 1025) map(#nth) println [1000th, 1001st, 1002nd, 1003rd, 1004th, 1005th, 1006th, 1007th, 1008th, 1009th, 1010th, 1 011th, 1012th, 1013th, 1014th, 1015th, 1016th, 1017th, 1018th, 1019th, 1020th, 1021st, 102 2nd, 1023rd, 1024th, 1025th]
PARI/GP
(Spurious apostrophes intentionally omitted, following Raku.)
ordinal(n)=my(k=n%10,m=n%100); Str(n,if(m<21&&m>3,"th",k==1,"st",k==2,"nd",k==3,"rd","th"));
apply(ordinal, [0..25])
apply(ordinal, [250..265])
apply(ordinal, [1000..1025])
apply(ordinal, [111, 1012])
- Output:
%1 = ["0th", "1st", "2nd", "3rd", "4th", "5th", "6th", "7th", "8th", "9th", "10th", "11th", "12th", "13th", "14th", "15th", "16th", "17th", "18th", "19th", "20th", "21st", "22nd", "23rd", "24th", "25th"] %2 = ["250th", "251st", "252nd", "253rd", "254th", "255th", "256th", "257th", "258th", "259th", "260th", "261st", "262nd", "263rd", "264th", "265th"] %3 = ["1000th", "1001st", "1002nd", "1003rd", "1004th", "1005th", "1006th", "1007th", "1008th", "1009th", "1010th", "1011th", "1012th", "1013th", "1014th", "1015th", "1016th", "1017th", "1018th", "1019th", "1020th", "1021st", "1022nd", "1023rd", "1024th", "1025th"] %4 = ["111th", "1012th"]
Pascal
nearly copy of Ada
Program n_th;
function Suffix(N: NativeInt):AnsiString;
var
res: AnsiString;
begin
res:= 'th';
case N mod 10 of
1:IF N mod 100 <> 11 then
res:= 'st';
2:IF N mod 100 <> 12 then
res:= 'nd';
3:IF N mod 100 <> 13 then
res:= 'rd';
else
end;
Suffix := res;
end;
procedure Print_Images(loLim, HiLim: NativeInt);
var
i : NativeUint;
begin
for I := LoLim to HiLim do
write(i,Suffix(i),' ');
writeln;
end;
begin
Print_Images( 0, 25);
Print_Images( 250, 265);
Print_Images(1000, 1025);
end.
- Output:
shortened
0th 1st 2nd 3rd 4th ... 11th 12th 13th ..20th 21st 22nd 23rd 24th 25th 250th 251st 252nd 253rd 254th ..261st 262nd 263rd 264th 265th ..1001st 1002nd 1003rd 1004th..1011th..1013th..1021st 1022nd 1023rd 1024th
PascalABC.NET
var suffix := |'th', 'st', 'nd', 'rd'| + |'th'| * 6;
function Nth(n: integer)
:= $'{n}''' + if n mod 100 not in 11..19 then suffix[n mod 10] else 'th';
begin
((0..24) + (500..524) + (700..724) + (1000..1024))
.Select(Nth).Println;
end.
- Output:
0'th 1'st 2'nd 3'rd 4'th 5'th 6'th 7'th 8'th 9'th 10'th 11'th 12'th 13'th 14'th 15'th 16'th 17'th 18'th 19'th 20'th 21'st 22'nd 23'rd 24'th 500'th 501'st 502'nd 503'rd 504'th 505'th 506'th 507'th 508'th 509'th 510'th 511'th 512'th 513'th 514'th 515'th 516'th 517'th 518'th 519'th 520'th 521'st 522'nd 523'rd 524'th 700'th 701'st 702'nd 703'rd 704'th 705'th 706'th 707'th 708'th 709'th 710'th 711'th 712'th 713'th 714'th 715'th 716'th 717'th 718'th 719'th 720'th 721'st 722'nd 723'rd 724'th 1000'th 1001'st 1002'nd 1003'rd 1004'th 1005'th 1006'th 1007'th 1008'th 1009'th 1010'th 1011'th 1012'th 1013'th 1014'th 1015'th 1016'th 1017'th 1018'th 1019'th 1020'th 1021'st 1022'nd 1023'rd 1024'th
Perl
Requires Perl 5.10 or newer for the Defined OR operator (//).
use 5.10.0;
my %irregulars = ( 1 => 'st',
2 => 'nd',
3 => 'rd',
11 => 'th',
12 => 'th',
13 => 'th');
sub nth
{
my $n = shift;
$n . # q(') . # Uncomment this to add apostrophes to output
($irregulars{$n % 100} // $irregulars{$n % 10} // 'th');
}
sub range { join ' ', map { nth($_) } @{$_[0]} }
print range($_), "\n" for ([0..25], [250..265], [1000..1025]);
- Output:
0th 1st 2nd 3rd 4th 5th 6th 7th 8th 9th 10th 11th 12th 13th 14th 15th 16th 17th 18th 19th 20th 21st 22nd 23rd 24th 25th 250th 251st 252nd 253rd 254th 255th 256th 257th 258th 259th 260th 261st 262nd 263rd 264th 265th 1000th 1001st 1002nd 1003rd 1004th 1005th 1006th 1007th 1008th 1009th 1010th 1011th 1012th 1013th 1014th 1015th 1016th 1017th 1018th 1019th 1020th 1021st 1022nd 1023rd 1024th 1025th
Alternatively, can use a library.
use Lingua::EN::Numbers::Ordinate 'ordinate';
foreach my $i (0..25, 250..265, 1000..1025) {
print ordinate($i),"\n";
}
Phix
constant ordinals = {"th","st","nd","rd"} function Nth(integer n, bool apostrophe=false) integer mod10 = mod(n,10)+1 if mod10>4 or mod(n,100)=mod10+9 then mod10 = 1 end if return sprintf("%d%s",{n,repeat('\'',apostrophe)&ordinals[mod10]}) end function constant ranges = {{0,25},{250,265},{1000,1025}} for i=1 to length(ranges) do for j=ranges[i][1] to ranges[i][2] do if mod(j,10)=0 then puts(1,"\n") end if printf(1," %6s",{Nth(j,i=2)}) end for puts(1,"\n") end for
- Output:
0th 1st 2nd 3rd 4th 5th 6th 7th 8th 9th 10th 11th 12th 13th 14th 15th 16th 17th 18th 19th 20th 21st 22nd 23rd 24th 25th 250'th 251'st 252'nd 253'rd 254'th 255'th 256'th 257'th 258'th 259'th 260'th 261'st 262'nd 263'rd 264'th 265'th 1000th 1001st 1002nd 1003rd 1004th 1005th 1006th 1007th 1008th 1009th 1010th 1011th 1012th 1013th 1014th 1015th 1016th 1017th 1018th 1019th 1020th 1021st 1022nd 1023rd 1024th 1025th
Alternatively you can use the builtin ord(), and a more functional style, as follows (same output)
function do_one(integer n, string apostrophe) return pad_head(sprintf("%d%s%s",{n,apostrophe,ord(n)}),7) end function procedure do_set(sequence s, bool bApostrophe=false) puts(1,join_by(apply(true,do_one,{s,{repeat('`',bApostrophe)}}),1,10,"")&"\n") end procedure constant {rs,re} = columnize({{0,25},{250,265},{1000,1025}}) papply(true,do_set,{apply(true,tagset,{re,rs}),{false,true,false}})
PHP
function nth($num) {
$os = "th";
if ($num % 100 <= 10 or $num % 100 > 20) {
switch ($num % 10) {
case 1:
$os = "st";
break;
case 2:
$os = "nd";
break;
case 3:
$os = "rd";
break;
}
}
return $num . $os;
}
foreach ([[0,25], [250,265], [1000,1025]] as $i) {
while ($i[0] <= $i[1]) {
echo nth($i[0]) . " ";
$i[0]++;
}
echo "\n";
}
- Output:
0th 1st 2nd 3rd 4th 5th 6th 7th 8th 9th 10th 11th 12th 13th 14th 15th 16th 17th 18th 19th 20th 21st 22nd 23rd 24th 25th 250th 251st 252nd 253rd 254th 255th 256th 257th 258th 259th 260th 261st 262nd 263rd 264th 265th 1000th 1001st 1002nd 1003rd 1004th 1005th 1006th 1007th 1008th 1009th 1010th 1011th 1012th 1013th 1014th 1015th 1016th 1017th 1018th 1019th 1020th 1021st 1022nd 1023rd 1024th 1025th
Picat
nth/3
is a built-in predicate in Picat, so let's call the functions nth2/1
and onward.
Prolog style
nth2(N) = N.to_string() ++ Th =>
( tween(N) -> Th = "th"
; 1 = N mod 10 -> Th = "st"
; 2 = N mod 10 -> Th = "nd"
; 3 = N mod 10 -> Th = "rd"
; Th = "th" ).
tween(N) => Tween = N mod 100, between(11, 13, Tween).
Function with explicit conditions
nth3(N) = cc(N,"th"), tween(N) => true.
nth3(N) = cc(N,"st"), N mod 10 = 1 => true.
nth3(N) = cc(N,"nd"), N mod 10 = 2 => true.
nth3(N) = cc(N,"rd"), N mod 10 = 3 => true.
nth3(N) = cc(N,"th") => true.
% helper function
cc(N,Th) = N.to_string() ++ Th.
List of suffixes
nth4(N) = Nth =>
Suffix = ["th","st","nd","rd","th","th","th","th","th","th"],
Nth = N.to_string() ++ cond((N mod 100 <= 10; N mod 100 > 20), Suffix[1 + N mod 10], "th").
Test
go =>
Ranges = [ 0..25, 250..265, 1000..1025],
foreach(Range in Ranges) println([nth2(I) : I in Range])
end,
nl.
- Output:
[0th,1st,2nd,3rd,4th,5th,6th,7th,8th,9th,10th,11th,12th,13th,14th,15th,16th,17th,18th,19th,20th,21st,22nd,23rd,24th,25th] [250th,251st,252nd,253rd,254th,255th,256th,257th,258th,259th,260th,261st,262nd,263rd,264th,265th] [1000th,1001st,1002nd,1003rd,1004th,1005th,1006th,1007th,1008th,1009th,1010th,1011th,1012th,1013th,1014th,1015th,1016th,1017th,1018th,1019th,1020th,1021st,1022nd,1023rd,1024th,1025th]
PicoLisp
(de rangeth (A B)
(mapcar
'((I)
(pack I
(if (member (% I 100) (11 12 13))
'th
(case (% I 10)
(1 'st)
(2 'nd)
(3 'rd)
(T 'th) ) ) ) )
(range A B) ) )
(prinl (glue " " (rangeth 0 25)))
(prinl (glue " " (rangeth 250 265)))
(prinl (glue " " (rangeth 1000 1025)))
(bye)
- Output:
0th 1st 2nd 3rd 4th 5th 6th 7th 8th 9th 10th 11th 12th 13th 14th 15th 16th 17th 18th 19th 20th 21st 22nd 23rd 24th 25th 250th 251st 252nd 253rd 254th 255th 256th 257th 258th 259th 260th 261st 262nd 263rd 264th 265th 1000th 1001st 1002nd 1003rd 1004th 1005th 1006th 1007th 1008th 1009th 1010th 1011th 1012th 1013th 1014th 1015th 1016th 1017th 1018th 1019th 1020th 1021st 1022nd 1023rd 1024th 1025th
PL/I
Nth: procedure options (main); /* 1 June 2014 */
declare i fixed (10);
do i = 0 to 25, 250 to 265, 1000 to 1025;
if i = 250 | i = 1000 then put skip (2);
put edit (enth(i)) (x(1), a);
end;
enth: procedure (i) returns (character (25) varying);
declare i fixed (10);
declare suffix character (2);
select (mod(i, 10));
when (1) suffix = 'st';
when (2) suffix = 'nd';
when (3) suffix = 'rd';
otherwise suffix = 'th';
end;
select (mod(i, 100));
when (11, 12, 13) suffix = 'th';
otherwise ;
end;
return ( trim(i) || suffix );
end enth;
end Nth;
- Output:
0th 1st 2nd 3rd 4th 5th 6th 7th 8th 9th 10th 11th 12th 13th 14th 15th 16th 17th 18th 19th 20th 21st 22nd 23rd 24th 25th 250th 251st 252nd 253rd 254th 255th 256th 257th 258th 259th 260th 261st 262nd 263rd 264th 265th 1000th 1001st 1002nd 1003rd 1004th 1005th 1006th 1007th 1008th 1009th 1010th 1011th 1012th 1013th 1014th 1015th 1016th 1017th 1018th 1019th 1020th 1021st 1022nd 1023rd 1024th 1025th
PL/M
... under CP/M (or an emulator)
The 8080 PL/M compiler doesn't recognise lower-case letters, so the letters for the suffixes have to be specified by their ASCII codes.
100H: /* SUFFIX NUMBERS WITH ST, ND, RD OR TH AS APPROPRIATE */
/* CP/M BDOS SYSTEM CALL AND I/O ROUTINES */
BDOS: PROCEDURE( FN, ARG ); DECLARE FN BYTE, ARG ADDRESS; GOTO 5; END;
PR$CHAR: PROCEDURE( C ); DECLARE C BYTE; CALL BDOS( 2, C ); END;
PR$STRING: PROCEDURE( S ); DECLARE S ADDRESS; CALL BDOS( 9, S ); END;
PR$NL: PROCEDURE; CALL PR$CHAR( 0DH ); CALL PR$CHAR( 0AH ); END;
PR$NUMBER: PROCEDURE( N ); /* PRINTS A NUMBER IN THE MINIMUN FIELD WIDTH */
DECLARE N ADDRESS;
DECLARE V ADDRESS, N$STR ( 6 )BYTE, W BYTE;
V = N;
W = LAST( N$STR );
N$STR( W ) = '$';
N$STR( W := W - 1 ) = '0' + ( V MOD 10 );
DO WHILE( ( V := V / 10 ) > 0 );
N$STR( W := W - 1 ) = '0' + ( V MOD 10 );
END;
CALL PR$STRING( .N$STR( W ) );
END PR$NUMBER;
/* TASK */
DECLARE LD LITERALLY '100' /* LOWER CASE LETTERS NEEDED FOR THE SUFFICES */
, LH LITERALLY '104'
, LN LITERALLY '110'
, LR LITERALLY '114'
, LS LITERALLY '115'
, LT LITERALLY '116'
;
/* RETURNS THE TWO CHARACTER ORDINAL SUFFIX FOR N */
SUFFIX: PROCEDURE( N )ADDRESS;
DECLARE N ADDRESS;
DECLARE ( S1, S2 ) BYTE;
S1 = LT;
S2 = LH;
IF N MOD 100 < 4 OR N MOD 100 > 20 THEN DO;
DECLARE N10 ADDRESS;
N10 = N MOD 10;
IF N10 = 1 THEN DO;
S1 = LS;
S2 = LT;
END;
ELSE IF N10 = 2 THEN DO;
S1 = LN;
S2 = LD;
END;
ELSE IF N10 = 3 THEN DO;
S1 = LR;
S2 = LD;
END;
END;
RETURN ( S1 * 256 ) + S2;
END SUFFIX;
/* PRINTS THE TWO CHARACTER SUFFIX IN S */
PR$SUFFIX: PROCEDURE( S );
DECLARE S ADDRESS;
CALL PR$CHAR( HIGH( S ) );
CALL PR$CHAR( LOW( S ) );
END PR$SUFFIX ;
DECLARE I ADDRESS;
DO I = 0 TO 25;
IF I < 10 THEN CALL PR$CHAR( ' ' );
CALL PR$CHAR( ' ' );CALL PR$NUMBER( I );CALL PR$SUFFIX( SUFFIX( I ) );
IF ( I + 1 ) MOD 10 = 0 THEN CALL PR$NL;
END;
CALL PR$NL;
CALL PR$NL;
DO I = 250 TO 265;
CALL PR$CHAR( ' ' );CALL PR$NUMBER( I );CALL PR$SUFFIX( SUFFIX( I ) );
IF ( I - 249 ) MOD 10 = 0 THEN CALL PR$NL;
END;
CALL PR$NL;
CALL PR$NL;
DO I = 1000 TO 1025;
CALL PR$CHAR( ' ' );CALL PR$NUMBER( I );CALL PR$SUFFIX( SUFFIX( I ) );
IF ( I - 999 ) MOD 10 = 0 THEN CALL PR$NL;
END;
CALL PR$NL;
EOF
- Output:
0th 1st 2nd 3rd 4th 5th 6th 7th 8th 9th 10th 11th 12th 13th 14th 15th 16th 17th 18th 19th 20th 21st 22nd 23rd 24th 25th 250th 251st 252nd 253rd 254th 255th 256th 257th 258th 259th 260th 261st 262nd 263rd 264th 265th 1000th 1001st 1002nd 1003rd 1004th 1005th 1006th 1007th 1008th 1009th 1010th 1011th 1012th 1013th 1014th 1015th 1016th 1017th 1018th 1019th 1020th 1021st 1022nd 1023rd 1024th 1025th
PowerShell
function nth($inp){
$suffix = "th"
switch($inp % 100){
11{$suffix="th"}
12{$suffix="th"}
13{$suffix="th"}
default{
switch($inp % 10){
1{$suffix="st"}
2{$suffix="nd"}
3{$suffix="rd"}
}
}
}
return "$inp$suffix "
}
0..25 | %{Write-host -nonewline (nth "$_")};""
250..265 | %{Write-host -nonewline (nth "$_")};""
1000..1025 | %{Write-host -nonewline (nth "$_")};""
- Output:
0th 1st 2nd 3rd 4th 5th 6th 7th 8th 9th 10th 11th 12th 13th 14th 15th 16th 17th 18th 19th 20th 21st 22nd 23rd 24th 25th 250th 251st 252nd 253rd 254th 255th 256th 257th 258th 259th 260th 261st 262nd 263rd 264th 265th 1000th 1001st 1002nd 1003rd 1004th 1005th 1006th 1007th 1008th 1009th 1010th 1011th 1012th 1013th 1014th 1015th 1016th 1017th 1018th 1019th 1020th 1021st 1022nd 1023rd 1024th 1025th
An Alternate Version
This is, I think, is a more "PowerShelly" way:
function Get-Nth ([int]$Number)
{
$suffix = "th"
switch ($Number % 100){
11 {$suffix = "th"}
12 {$suffix = "th"}
13 {$suffix = "th"}
default {
switch ($Number % 10){
1 {$suffix = "st"}
2 {$suffix = "nd"}
3 {$suffix = "rd"}
}
}
}
"$Number$suffix"
}
1..25 | ForEach-Object {Get-Nth $_} | Format-Wide {$_} -Column 5 -Force
251..265 | ForEach-Object {Get-Nth $_} | Format-Wide {$_} -Column 5 -Force
1001..1025 | ForEach-Object {Get-Nth $_} | Format-Wide {$_} -Column 5 -Force
- Output:
1st 2nd 3rd 4th 5th 6th 7th 8th 9th 10th 11th 12th 13th 14th 15th 16th 17th 18th 19th 20th 21st 22nd 23rd 24th 25th 251st 252nd 253rd 254th 255th 256th 257th 258th 259th 260th 261st 262nd 263rd 264th 265th 1001st 1002nd 1003rd 1004th 1005th 1006th 1007th 1008th 1009th 1010th 1011th 1012th 1013th 1014th 1015th 1016th 1017th 1018th 1019th 1020th 1021st 1022nd 1023rd 1024th 1025th
Prolog
Following Icon:
nth(N, N_Th) :-
( tween(N) -> Th = "th"
; 1 is N mod 10 -> Th = "st"
; 2 is N mod 10 -> Th = "nd"
; 3 is N mod 10 -> Th = "rd"
; Th = "th" ),
string_concat(N, Th, N_Th).
tween(N) :- Tween is N mod 100, between(11, 13, Tween).
test :-
forall( between(0,25, N), (nth(N, N_Th), format('~w, ', N_Th)) ),
nl, nl,
forall( between(250,265,N), (nth(N, N_Th), format('~w, ', N_Th)) ),
nl, nl,
forall( between(1000,1025,N), (nth(N, N_Th), format('~w, ', N_Th)) ).
- Output:
of `test/0`
?- test. 0th, 1st, 2nd, 3rd, 4th, 5th, 6th, 7th, 8th, 9th, 10th, 11th, 12th, 13th, 14th, 15th, 16th, 17th, 18th, 19th, 20th, 21st, 22nd, 23rd, 24th, 25th, 250th, 251st, 252nd, 253rd, 254th, 255th, 256th, 257th, 258th, 259th, 260th, 261st, 262nd, 263rd, 264th, 265th, 1000th, 1001st, 1002nd, 1003rd, 1004th, 1005th, 1006th, 1007th, 1008th, 1009th, 1010th, 1011th, 1012th, 1013th, 1014th, 1015th, 1016th, 1017th, 1018th, 1019th, 1020th, 1021st, 1022nd, 1023rd, 1024th, 1025th, true.
Python
_suffix = ['th', 'st', 'nd', 'rd', 'th', 'th', 'th', 'th', 'th', 'th']
def nth(n):
return "%i'%s" % (n, _suffix[n%10] if n % 100 <= 10 or n % 100 > 20 else 'th')
if __name__ == '__main__':
for j in range(0,1001, 250):
print(' '.join(nth(i) for i in list(range(j, j+25))))
- Output:
0'th 1'st 2'nd 3'rd 4'th 5'th 6'th 7'th 8'th 9'th 10'th 11'th 12'th 13'th 14'th 15'th 16'th 17'th 18'th 19'th 20'th 21'st 22'nd 23'rd 24'th 250'th 251'st 252'nd 253'rd 254'th 255'th 256'th 257'th 258'th 259'th 260'th 261'st 262'nd 263'rd 264'th 265'th 266'th 267'th 268'th 269'th 270'th 271'st 272'nd 273'rd 274'th 500'th 501'st 502'nd 503'rd 504'th 505'th 506'th 507'th 508'th 509'th 510'th 511'th 512'th 513'th 514'th 515'th 516'th 517'th 518'th 519'th 520'th 521'st 522'nd 523'rd 524'th 750'th 751'st 752'nd 753'rd 754'th 755'th 756'th 757'th 758'th 759'th 760'th 761'st 762'nd 763'rd 764'th 765'th 766'th 767'th 768'th 769'th 770'th 771'st 772'nd 773'rd 774'th 1000'th 1001'st 1002'nd 1003'rd 1004'th 1005'th 1006'th 1007'th 1008'th 1009'th 1010'th 1011'th 1012'th 1013'th 1014'th 1015'th 1016'th 1017'th 1018'th 1019'th 1020'th 1021'st 1022'nd 1023'rd 1024'th
Alternate version
#!/usr/bin/env python3
def ord(n):
try:
s = ['st', 'nd', 'rd'][(n-1)%10]
if (n-10)%100//10:
return str(n)+s
except IndexError:
pass
return str(n)+'th'
if __name__ == '__main__':
print(*(ord(n) for n in range(26)))
print(*(ord(n) for n in range(250,266)))
print(*(ord(n) for n in range(1000,1026)))
- Output:
0th 1st 2nd 3rd 4th 5th 6th 7th 8th 9th 10th 11th 12th 13th 14th 15th 16th 17th 18th 19th 20th 21st 22nd 23rd 24th 25th 250th 251st 252nd 253rd 254th 255th 256th 257th 258th 259th 260th 261st 262nd 263rd 264th 265th 1000th 1001st 1002nd 1003rd 1004th 1005th 1006th 1007th 1008th 1009th 1010th 1011th 1012th 1013th 1014th 1015th 1016th 1017th 1018th 1019th 1020th 1021st 1022nd 1023rd 1024th 1025th
Quackery
[ table ] is suffix ( n --> $ )
$ "th st nd rd th th th th th th"
nest$ witheach [ ' suffix put ]
[ dup number$
swap dup 100 mod
10 21 within iff
[ drop $ "th" join ]
else
[ 10 mod
suffix join ] ] is ordinal$ ( n --> $ )
[ over - 1+
[] swap times
[ over i^ + ordinal$
nested join ]
nip 50 wrap$ ] is test ( n n --> )
0 25 test
cr
250 265 test
cr
1000 1025 test
- Output:
0th 1st 2nd 3rd 4th 5th 6th 7th 8th 9th 10th 11th 12th 13th 14th 15th 16th 17th 18th 19th 20th 21st 22nd 23rd 24th 25th 250th 251st 252nd 253rd 254th 255th 256th 257th 258th 259th 260th 261st 262nd 263rd 264th 265th 1000th 1001st 1002nd 1003rd 1004th 1005th 1006th 1007th 1008th 1009th 1010th 1011th 1012th 1013th 1014th 1015th 1016th 1017th 1018th 1019th 1020th 1021st 1022nd 1023rd 1024th 1025th
R
Note that R vectors are 1-indexed.
nth <- function(n)
{
if (length(n) > 1) return(sapply(n, nth))
mod <- function(m, n) ifelse(!(m%%n), n, m%%n)
suffices <- c("th", "st", "nd", "rd", "th", "th", "th", "th", "th", "th")
if (n %% 100 <= 10 || n %% 100 > 20)
suffix <- suffices[mod(n+1, 10)]
else
suffix <- 'th'
paste(n, "'", suffix, sep="")
}
range <- list(0:25, 250:275, 500:525, 750:775, 1000:1025)
sapply(range, nth)
- Output:
[,1] [,2] [,3] [,4] [,5] [1,] "0'th" "250'th" "500'th" "750'th" "1000'th" [2,] "1'st" "251'st" "501'st" "751'st" "1001'st" [3,] "2'nd" "252'nd" "502'nd" "752'nd" "1002'nd" [4,] "3'rd" "253'rd" "503'rd" "753'rd" "1003'rd" [5,] "4'th" "254'th" "504'th" "754'th" "1004'th" [6,] "5'th" "255'th" "505'th" "755'th" "1005'th" [7,] "6'th" "256'th" "506'th" "756'th" "1006'th" [8,] "7'th" "257'th" "507'th" "757'th" "1007'th" [9,] "8'th" "258'th" "508'th" "758'th" "1008'th" [10,] "9'th" "259'th" "509'th" "759'th" "1009'th" [11,] "10'th" "260'th" "510'th" "760'th" "1010'th" [12,] "11'th" "261'st" "511'th" "761'st" "1011'th" [13,] "12'th" "262'nd" "512'th" "762'nd" "1012'th" [14,] "13'th" "263'rd" "513'th" "763'rd" "1013'th" [15,] "14'th" "264'th" "514'th" "764'th" "1014'th" [16,] "15'th" "265'th" "515'th" "765'th" "1015'th" [17,] "16'th" "266'th" "516'th" "766'th" "1016'th" [18,] "17'th" "267'th" "517'th" "767'th" "1017'th" [19,] "18'th" "268'th" "518'th" "768'th" "1018'th" [20,] "19'th" "269'th" "519'th" "769'th" "1019'th" [21,] "20'th" "270'th" "520'th" "770'th" "1020'th" [22,] "21'st" "271'st" "521'st" "771'st" "1021'st" [23,] "22'nd" "272'nd" "522'nd" "772'nd" "1022'nd" [24,] "23'rd" "273'rd" "523'rd" "773'rd" "1023'rd" [25,] "24'th" "274'th" "524'th" "774'th" "1024'th" [26,] "25'th" "275'th" "525'th" "775'th" "1025'th"
Racket
#lang racket
(define (teen? n) (<= 11 (modulo n 100) 19))
(define (Nth n)
(format
"~a'~a" n
(match* ((modulo n 10) n)
[((or 1 2 3) (? teen?)) 'th] [(1 _) 'st] [(2 _) 'nd] [(3 _) 'rd] [(_ _) 'th])))
(for ((range (list (in-range 26) (in-range 250 266) (in-range 1000 1026))))
(displayln (string-join (for/list ((nth (sequence-map Nth range))) nth) " ")))
- Output:
0'th 1'st 2'nd 3'rd 4'th 5'th 6'th 7'th 8'th 9'th 10'th 11'th 12'th 13'th 14'th 15'th 16'th 17'th 18'th 19'th 20'th 21'st 22'nd 23'rd 24'th 25'th 250'th 251'st 252'nd 253'rd 254'th 255'th 256'th 257'th 258'th 259'th 260'th 261'st 262'nd 263'rd 264'th 265'th 1000'th 1001'st 1002'nd 1003'rd 1004'th 1005'th 1006'th 1007'th 1008'th 1009'th 1010'th 1011'th 1012'th 1013'th 1014'th 1015'th 1016'th 1017'th 1018'th 1019'th 1020'th 1021'st 1022'nd 1023'rd 1024'th 1025'th
Raku
(formerly Perl 6) (Spurious apostrophes intentionally omitted.)
my %irregulars = <1 st 2 nd 3 rd>, (11..13 X=> 'th');
sub nth ($n) { $n ~ ( %irregulars{$n % 100} // %irregulars{$n % 10} // 'th' ) }
say .list».&nth for [^26], [250..265], [1000..1025];
- Output:
0th 1st 2nd 3rd 4th 5th 6th 7th 8th 9th 10th 11th 12th 13th 14th 15th 16th 17th 18th 19th 20th 21st 22nd 23rd 24th 25th 250th 251st 252nd 253rd 254th 255th 256th 257th 258th 259th 260th 261st 262nd 263rd 264th 265th 1000th 1001st 1002nd 1003rd 1004th 1005th 1006th 1007th 1008th 1009th 1010th 1011th 1012th 1013th 1014th 1015th 1016th 1017th 1018th 1019th 1020th 1021st 1022nd 1023rd 1024th 1025th
If you want to get Unicodally fancy, use this version instead:
my %irregulars = <1 ˢᵗ 2 ⁿᵈ 3 ʳᵈ>, (11..13 X=> 'ᵗʰ');
sub nth ($n) { $n ~ ( %irregulars{$n % 100} // %irregulars{$n % 10} // 'ᵗʰ' ) }
say .list».&nth for [^26], [250..265], [1000..1025];
- Output:
0ᵗʰ 1ˢᵗ 2ⁿᵈ 3ʳᵈ 4ᵗʰ 5ᵗʰ 6ᵗʰ 7ᵗʰ 8ᵗʰ 9ᵗʰ 10ᵗʰ 11ᵗʰ 12ᵗʰ 13ᵗʰ 14ᵗʰ 15ᵗʰ 16ᵗʰ 17ᵗʰ 18ᵗʰ 19ᵗʰ 20ᵗʰ 21ˢᵗ 22ⁿᵈ 23ʳᵈ 24ᵗʰ 25ᵗʰ
250ᵗʰ 251ˢᵗ 252ⁿᵈ 253ʳᵈ 254ᵗʰ 255ᵗʰ 256ᵗʰ 257ᵗʰ 258ᵗʰ 259ᵗʰ 260ᵗʰ 261ˢᵗ 262ⁿᵈ 263ʳᵈ 264ᵗʰ 265ᵗʰ
1000ᵗʰ 1001ˢᵗ 1002ⁿᵈ 1003ʳᵈ 1004ᵗʰ 1005ᵗʰ 1006ᵗʰ 1007ᵗʰ 1008ᵗʰ 1009ᵗʰ 1010ᵗʰ 1011ᵗʰ 1012ᵗʰ 1013ᵗʰ 1014ᵗʰ 1015ᵗʰ 1016ᵗʰ 1017ᵗʰ 1018ᵗʰ 1019ᵗʰ 1020ᵗʰ 1021ˢᵗ 1022ⁿᵈ 1023ʳᵈ 1024ᵗʰ 1025ᵗʰ
Red
Red[]
nth: function [n][
d: n % 10
dd: n % 100
suffix: either any [ all [ dd > 3 dd < 20 ] d < 1 d > 4 1 = to-integer n / 10] [4] [d]
rejoin [n pick ["st" "nd" "rd" "th"] suffix]
]
test: function [low high][
repeat i high - low + 1 [
prin [nth i + low - 1 ""]
]
prin newline
]
test 0 25
test 250 265
test 1000 1025
- Output:
0th 1st 2nd 3rd 4th 5th 6th 7th 8th 9th 10th 11th 12th 13th 14th 15th 16th 17th 18th 19th 20th 21st 22nd 23rd 24th 25th 250th 251st 252nd 253rd 254th 255th 256th 257th 258th 259th 260th 261st 262nd 263rd 264th 265th 1000th 1001st 1002nd 1003rd 1004th 1005th 1006th 1007th 1008th 1009th 1010th 1011th 1012th 1013th 1014th 1015th 1016th 1017th 1018th 1019th 1020th 1021st 1022nd 1023rd 1024th 1025th
Refal
$ENTRY Go {
= <Show 0 25>
<Show 250 265>
<Show 1000 1025>;
};
Nth {
s.N, <Symb s.N>: {
e.X '1' s.Y = e.X '1' s.Y 'th';
e.X '1' = e.X '1st';
e.X '2' = e.X '2nd';
e.X '3' = e.X '3rd';
e.X = e.X 'th';
};
};
Cell {
s.N, <First 8 <Nth s.N> ' '>: (e.1) e.2 = e.1;
}
Group {
s.N e.1 = <Group (s.N s.N) () e.1>;
(s.N s.M) (e.1) = (e.1);
(s.N 0) (e.1) e.2 = (e.1) <Group (s.N s.N) () e.2>;
(s.N s.M) (e.1) s.2 e.3 = <Group (s.N <- s.M 1>) (e.1 s.2) e.3>;
};
Iota {
s.End s.End = s.End;
s.Start s.End = s.Start <Iota <+ s.Start 1> s.End>;
};
Each {
s.F = ;
s.F t.I e.X = <Mu s.F t.I> <Each s.F e.X>;
};
ShowLine {
(e.Line) = <Prout <Each Cell e.Line>>;
};
Show {
s.Start s.End,
<Iota s.Start s.End>: e.Range,
<Group 10 e.Range>: e.Lines
= <Prout <Each ShowLine e.Lines>>;
};
- Output:
0th 1st 2nd 3rd 4th 5th 6th 7th 8th 9th 10th 11th 12th 13th 14th 15th 16th 17th 18th 19th 20th 21st 22nd 23rd 24th 25th 250th 251st 252nd 253rd 254th 255th 256th 257th 258th 259th 260th 261st 262nd 263rd 264th 265th 1000th 1001st 1002nd 1003rd 1004th 1005th 1006th 1007th 1008th 1009th 1010th 1011th 1012th 1013th 1014th 1015th 1016th 1017th 1018th 1019th 1020th 1021st 1022nd 1023rd 1024th 1025th
REXX
This version adds suffixes without apostrophes.
Negative numbers are also handled.
Two alternate solutions added to the oiginal
/*REXX program shows ranges of numbers with ordinal (st/nd/rd/th) suffixes attached.*/
Call tell 0,25 /* display the 1st range of numbers */
Call tell 250,265 /* " " 2nd " " " */
Call tell 1000,1025 /* " " 3rd " " " */
Exit /* stick a fork in it, we're all done */
/*-------------------------------------------------------------------------*/
tell: Procedure
Parse Arg low,high /* get the Low and High numbers */
out.=''
Do n=low To high /* process the range, from low */
out.1=out.1 th(n)
out.2=out.2 th2(n)
out.3=out.3 th3(n)
End
Say 'numbers from' low 'to' high '(inclusive):' /*display the output */
Say strip(out.1)
Say ''
If out.2<>out.1 Then Say 'th2 must be wrong'
If out.3<>out.1 Then Say 'th3 must be wrong'
Return
/*-------------------------------------------------------------------------*/
th: /* compact original */
Parse Arg z
x=abs(z)
Return z||word('th st nd rd',1+x//10*(x//100%10\==1)*(x//10<4))
/*-------------------------------------------------------------------------*/
th2: /* rather verbose logic */
Parse Arg z
x=abs(z)
Select
When length(x)=1 Then
If x<4 Then
suffix=word('th st nd rd',x+1)
Else
suffix='th'
When suffixstr(x,length(x)-1,1)=1 Then
suffix='th'
When right(x,1)<4 Then
suffix=word('th st nd rd',right(x,1)+1)
Otherwise
suffix='th'
End
Return z||suffix
/*-------------------------------------------------------------------------*/
th3: /* compact yet quite readable */
Parse Arg z
Parse Value reverse(z) with last +1 prev +1
If last<4 &, /* last digit is 0,1,2,3 */
prev<>1 Then /* and number is not **1x */
suffix=word('th st nd rd',last+1)
Else /* all oher cases */
suffix='th'
Return z||suffix
output using the default inputs:
numbers from 0 to 25 (inclusive): 0th 1st 2nd 3rd 4th 5th 6th 7th 8th 9th 10th 11th 12th 13th 14th 15th 16th 17th 18th 19th 20th 21st 22nd 23rd 24th 25th numbers from 250 to 265 (inclusive): 250th 251st 252nd 253rd 254th 255th 256th 257th 258th 259th 260th 261st 262nd 263rd 264th 265th numbers from 1000 to 1025 (inclusive): 1000th 1001st 1002nd 1003rd 1004th 1005th 1006th 1007th 1008th 1009th 1010th 1011th 1012th 1013th 1014th 1015th 1016th 1017th 1018th 1019th 1020th 1021st 1022nd 1023rd 1024th 1025th
Ring
for nr = 0 to 25
see Nth(nr) + Nth(nr + 250) + Nth(nr + 1000) + nl
next
func getSuffix n
lastTwo = n % 100
lastOne = n % 10
if lastTwo > 3 and lastTwo < 21 "th" ok
if lastOne = 1 return "st" ok
if lastOne = 2 return "nd" ok
if lastOne = 3 return "rd" ok
return "th"
func Nth n
return "" + n + "'" + getSuffix(n) + " "
RPL
Clean version
≪ IP DUP 10 MOD OVER 100 MOD 10 / IP → units tens
≪ →STR "'" +
IF units 1 ≥ units 3 ≤ AND tens 1 ≠ AND
THEN { "st" "nd" "rd" } units GET
ELSE "th"
END
+
≫ ≫ 'NTH' STO
One-liner
This version uses a mix of arithmetic and boolean operations to determine when to apply the "st/nd/rd" exception.
≪ IP →STR LAST { "'th" "'st" "'nd" "'rd" } OVER 10 MOD DUP 4 < * ROT 100 MOD 10 / IP 1 ≠ * 1 + GET + ≫ 'NTH' STO
≪ { } ROT ROT FOR j j NTH + NEXT ≫ 'SHOW' STO 0 25 SHOW 250 265 SHOW 1000 1025 SHOW
- Output:
3: { "0'th" "1'st" "2'nd" "3'rd" "4'th" "5'th" "6'th" "7'th" "8'th" "9'th" "10'th" "11'th" "12'th" "13'th" "14'th" "15'th" "16'th" "17'th" "18'th" "19'th" "20'th" "21'st" "22'nd" "23'rd" "24'th" "25'th" } 2: { "250'th" "251'st" "252'nd" "253'rd" "254'th" "255'th" "256'th" "257'th" "258'th" "259'th" "260'th" "261'st" "262'nd" "263'rd" "264'th" "265'th" } 1: { "1000'th" "1001'st" "1002'nd" "1003'rd" "1004'th" "1005'th" "1006'th" "1007'th" "1008'th" "1009'th" "1010'th" "1011'th" "1012'th" "1013'th" "1014'th" "1015'th" "1016'th" "1017'th" "1018'th" "1019'th" "1020'th" "1021'st" "1022'nd" "1023'rd" "1024'th" "1025'th" }
Ruby
Code (slightly adapted) and methodname taken from ActiveSupport (Ruby on Rails).
class Integer
def ordinalize
num = self.abs
ordinal = if (11..13).include?(num % 100)
"th"
else
case num % 10
when 1; "st"
when 2; "nd"
when 3; "rd"
else "th"
end
end
"#{self}#{ordinal}"
end
end
[(0..25),(250..265),(1000..1025)].each{|r| puts r.map(&:ordinalize).join(", "); puts}
- Output:
0th, 1st, 2nd, 3rd, 4th, 5th, 6th, 7th, 8th, 9th, 10th, 11th, 12th, 13th, 14th, 15th, 16th, 17th, 18th, 19th, 20th, 21st, 22nd, 23rd, 24th, 25th 250th, 251st, 252nd, 253rd, 254th, 255th, 256th, 257th, 258th, 259th, 260th, 261st, 262nd, 263rd, 264th, 265th 1000th, 1001st, 1002nd, 1003rd, 1004th, 1005th, 1006th, 1007th, 1008th, 1009th, 1010th, 1011th, 1012th, 1013th, 1014th, 1015th, 1016th, 1017th, 1018th, 1019th, 1020th, 1021st, 1022nd, 1023rd, 1024th, 1025th
Rust
fn nth(num: isize) -> String {
format!("{}{}", num, match (num % 10, num % 100) {
(1, 11) | (2, 12) | (3, 13) => "th",
(1, _) => "st",
(2, _) => "nd",
(3, _) => "rd",
_ => "th",
})
}
fn main() {
let ranges = [(0, 26), (250, 266), (1000, 1026)];
for &(s, e) in &ranges {
println!("[{}, {}) :", s, e);
for i in s..e {
print!("{}, ", nth(i));
}
println!();
}
}
- Output:
[0, 26) : 0th, 1st, 2nd, 3rd, 4th, 5th, 6th, 7th, 8th, 9th, 10th, 11th, 12th, 13th, 14th, 15th, 16th, 17th, 18th, 19th, 20th, 21st, 22nd, 23rd, 24th, 25th, [250, 266) : 250th, 251st, 252nd, 253rd, 254th, 255th, 256th, 257th, 258th, 259th, 260th, 261st, 262nd, 263rd, 264th, 265th, [1000, 1026) : 1000th, 1001st, 1002nd, 1003rd, 1004th, 1005th, 1006th, 1007th, 1008th, 1009th, 1010th, 1011th, 1012th, 1013th, 1014th, 1015th, 1016th, 1017th, 1018th, 1019th, 1020th, 1021st, 1022nd, 1023rd, 1024th, 1025th,
Scala
object Nth extends App {
def abbrevNumber(i: Int) = print(s"$i${ordinalAbbrev(i)} ")
def ordinalAbbrev(n: Int) = {
val ans = "th" //most of the time it should be "th"
if (n % 100 / 10 == 1) ans //teens are all "th"
else (n % 10) match {
case 1 => "st"
case 2 => "nd"
case 3 => "rd"
case _ => ans
}
}
(0 to 25).foreach(abbrevNumber)
println()
(250 to 265).foreach(abbrevNumber)
println();
(1000 to 1025).foreach(abbrevNumber)
}
sed
/1.$/!{s/1$/1st/;s/2$/2nd/;s/3$/3rd/;};s/[0-9]$/&th/
- Output:
$ for n in 0 250 1000; do echo $(seq $n $((n + 25)) | sed -f nth.sed); done 0th 1st 2nd 3rd 4th 5th 6th 7th 8th 9th 10th 11th 12th 13th 14th 15th 16th 17th 18th 19th 20th 21st 22nd 23rd 24th 25th 250th 251st 252nd 253rd 254th 255th 256th 257th 258th 259th 260th 261st 262nd 263rd 264th 265th 266th 267th 268th 269th 270th 271st 272nd 273rd 274th 275th 1000th 1001st 1002nd 1003rd 1004th 1005th 1006th 1007th 1008th 1009th 1010th 1011th 1012th 1013th 1014th 1015th 1016th 1017th 1018th 1019th 1020th 1021st 1022nd 1023rd 1024th 1025th
Seed7
$ include "seed7_05.s7i";
const func string: suffix (in integer: num) is func
result
var string: suffix is "";
begin
if num rem 10 = 1 and num rem 100 <> 11 then suffix := "st";
elsif num rem 10 = 2 and num rem 100 <> 12 then suffix := "nd";
elsif num rem 10 = 3 and num rem 100 <> 13 then suffix := "rd";
else suffix := "th";
end if;
end func;
const proc: printImages (in integer: start, in integer: stop) is func
local
var integer: num is 0;
begin
for num range start to stop do
write(num <& suffix(num) <& " ");
end for;
writeln;
end func;
const proc: main is func
begin
printImages( 0, 25);
printImages( 250, 265);
printImages(1000, 1025);
end func;
- Output:
0th 1st 2nd 3rd 4th 5th 6th 7th 8th 9th 10th 11th 12th 13th 14th 15th 16th 17th 18th 19th 20th 21st 22nd 23rd 24th 25th 250th 251st 252nd 253rd 254th 255th 256th 257th 258th 259th 260th 261st 262nd 263rd 264th 265th 1000th 1001st 1002nd 1003rd 1004th 1005th 1006th 1007th 1008th 1009th 1010th 1011th 1012th 1013th 1014th 1015th 1016th 1017th 1018th 1019th 1020th 1021st 1022nd 1023rd 1024th 1025th
SETL
program nth;
loop for r in [[0..25], [250..265], [1000..1025]] do
showrange(r);
end loop;
proc showrange(r);
i := 0;
loop for s in [nth(n) : n in r] do
putchar(rpad(s, 8));
if (i +:= 1) mod 6 = 0 then print(); end if;
end loop;
print();
end proc;
proc nth(n);
sfx := {[1,"st"], [2,"nd"], [3,"rd"]};
return str n +
if n div 10 mod 10 = 1
then "th"
else sfx(n mod 10) ? "th"
end if;
end proc;
end program;
- Output:
0th 1st 2nd 3rd 4th 5th 6th 7th 8th 9th 10th 11th 12th 13th 14th 15th 16th 17th 18th 19th 20th 21st 22nd 23rd 24th 25th 250th 251st 252nd 253rd 254th 255th 256th 257th 258th 259th 260th 261st 262nd 263rd 264th 265th 1000th 1001st 1002nd 1003rd 1004th 1005th 1006th 1007th 1008th 1009th 1010th 1011th 1012th 1013th 1014th 1015th 1016th 1017th 1018th 1019th 1020th 1021st 1022nd 1023rd 1024th 1025th
Set lang
Due to the language's specification, the input can only contain one character. Therefore, the following code only works with 0-9.
set o 49
set t 50
set h 51
set n !
set ! n
set ! 39
[n=o] set ? 13
[n=t] set ? 16
[n=h] set ? 19
set ! T
set ! H
set ? 21
set ! S
set ! T
set ? 21
set ! N
set ! D
set ? 12
set ! R
set ! D
> EOF
Input: I, Output: O
I: 1, O: 1'ST I: 2, O: 2'ND I: 3, O: 3'RD I: 4, O: 4'TH I: 5, O: 5'TH ...
Sidef
func nth(n) {
static irregulars = Hash(<1 ˢᵗ 2 ⁿᵈ 3 ʳᵈ 11 ᵗʰ 12 ᵗʰ 13 ᵗʰ>...)
n.to_s + (irregulars{n % 100} \\ irregulars{n % 10} \\ 'ᵗʰ')
}
for r in [0..25, 250..265, 1000..1025] {
say r.map {|n| nth(n) }.join(" ")
}
- Output:
0ᵗʰ 1ˢᵗ 2ⁿᵈ 3ʳᵈ 4ᵗʰ 5ᵗʰ 6ᵗʰ 7ᵗʰ 8ᵗʰ 9ᵗʰ 10ᵗʰ 11ᵗʰ 12ᵗʰ 13ᵗʰ 14ᵗʰ 15ᵗʰ 16ᵗʰ 17ᵗʰ 18ᵗʰ 19ᵗʰ 20ᵗʰ 21ˢᵗ 22ⁿᵈ 23ʳᵈ 24ᵗʰ 25ᵗʰ 250ᵗʰ 251ˢᵗ 252ⁿᵈ 253ʳᵈ 254ᵗʰ 255ᵗʰ 256ᵗʰ 257ᵗʰ 258ᵗʰ 259ᵗʰ 260ᵗʰ 261ˢᵗ 262ⁿᵈ 263ʳᵈ 264ᵗʰ 265ᵗʰ 1000ᵗʰ 1001ˢᵗ 1002ⁿᵈ 1003ʳᵈ 1004ᵗʰ 1005ᵗʰ 1006ᵗʰ 1007ᵗʰ 1008ᵗʰ 1009ᵗʰ 1010ᵗʰ 1011ᵗʰ 1012ᵗʰ 1013ᵗʰ 1014ᵗʰ 1015ᵗʰ 1016ᵗʰ 1017ᵗʰ 1018ᵗʰ 1019ᵗʰ 1020ᵗʰ 1021ˢᵗ 1022ⁿᵈ 1023ʳᵈ 1024ᵗʰ 1025ᵗʰ
SQL
Oracle
select level card,
to_char(to_date(level,'j'),'fmjth') ord
from dual
connect by level <= 15;
select to_char(to_date(5373485,'j'),'fmjth')
from dual;
CARD ORD ---------- ------------------------------ 1 1st 2 2nd 3 3rd 4 4th 5 5th 6 6th 7 7th 8 8th 9 9th 10 10th 11 11th 12 12th 13 13th 14 14th 15 15th 15 rows selected. select to_char(to_date(5373485,'j'),'fmjth') * ERROR at line 1: ORA-01854: julian date must be between 1 and 5373484
Standard ML
local
val v = Vector.tabulate (10, fn 1 => "st" | 2 => "nd" | 3 => "rd" | _ => "th")
fun getSuffix x =
if 3 < x andalso x < 21 then "th" else Vector.sub (v, x mod 10)
in
fun nth n =
Int.toString n ^ getSuffix (n mod 100)
end
(* some test ouput *)
val () = (print o concat o List.tabulate)
(26, fn i => String.concatWith "\t" (map nth [i, i + 250, i + 1000]) ^ "\n")
- Output:
0th 250th 1000th 1st 251st 1001st 2nd 252nd 1002nd 3rd 253rd 1003rd 4th 254th 1004th 5th 255th 1005th 6th 256th 1006th 7th 257th 1007th 8th 258th 1008th 9th 259th 1009th 10th 260th 1010th 11th 261st 1011th 12th 262nd 1012th 13th 263rd 1013th 14th 264th 1014th 15th 265th 1015th 16th 266th 1016th 17th 267th 1017th 18th 268th 1018th 19th 269th 1019th 20th 270th 1020th 21st 271st 1021st 22nd 272nd 1022nd 23rd 273rd 1023rd 24th 274th 1024th 25th 275th 1025th
Stata
We reuse here the maps function defined in the task Apply a callback to an array.
mata
function maps(f,a) {
nr = rows(a)
nc = cols(a)
b = J(nr,nc,"")
for (i=1;i<=nr;i++) {
for (j=1;j<=nc;j++) b[i,j] = (*f)(a[i,j])
}
return(b)
}
function nth(n) {
k = max((min((mod(n-1,10)+1,4)),4*(mod(n-10,100)<10)))
return(strofreal(n)+("st","nd","rd","th")[k])
}
maps(&nth(),((0::25),(250::275),(1000::1025)))
end
Output:
1 2 3 +----------------------------+ 1 | 0th 250th 1000th | 2 | 1st 251st 1001st | 3 | 2nd 252nd 1002nd | 4 | 3rd 253rd 1003rd | 5 | 4th 254th 1004th | 6 | 5th 255th 1005th | 7 | 6th 256th 1006th | 8 | 7th 257th 1007th | 9 | 8th 258th 1008th | 10 | 9th 259th 1009th | 11 | 10th 260th 1010th | 12 | 11th 261st 1011th | 13 | 12th 262nd 1012th | 14 | 13th 263rd 1013th | 15 | 14th 264th 1014th | 16 | 15th 265th 1015th | 17 | 16th 266th 1016th | 18 | 17th 267th 1017th | 19 | 18th 268th 1018th | 20 | 19th 269th 1019th | 21 | 20th 270th 1020th | 22 | 21st 271st 1021st | 23 | 22nd 272nd 1022nd | 24 | 23rd 273rd 1023rd | 25 | 24th 274th 1024th | 26 | 25th 275th 1025th | +----------------------------+
Stringle
#i
s "th"
.\#i "1" .:\#i !"1" s "st"
.\#i "2" .:\#i !"1" s "nd"
.\#i "3" .:\#i !"1" s "rd"
$ #i s
i i "."
#i +26 26 +#i #@i 250
#i +266 266 +#i #@i 1000
#i +1026 i ""
#i
Swift
func addSuffix(n:Int) -> String {
if n % 100 / 10 == 1 {
return "th"
}
switch n % 10 {
case 1:
return "st"
case 2:
return "nd"
case 3:
return "rd"
default:
return "th"
}
}
for i in 0...25 {
print("\(i)\(addSuffix(i)) ")
}
println()
for i in 250...265 {
print("\(i)\(addSuffix(i)) ")
}
println()
for i in 1000...1025 {
print("\(i)\(addSuffix(i)) ")
}
println()
- Output:
0th 1st 2nd 3rd 4th 5th 6th 7th 8th 9th 10th 11th 12th 13th 14th 15th 16th 17th 18th 19th 20th 21st 22nd 23rd 24th 25th 250th 251st 252nd 253rd 254th 255th 256th 257th 258th 259th 260th 261st 262nd 263rd 264th 265th 1000th 1001st 1002nd 1003rd 1004th 1005th 1006th 1007th 1008th 1009th 1010th 1011th 1012th 1013th 1014th 1015th 1016th 1017th 1018th 1019th 1020th 1021st 1022nd 1023rd 1024th 1025th
Tcl
proc ordinal {n} {
if {$n%100<10 || $n%100>20} {
set suff [lindex {th st nd rd th th th th th th} [expr {$n % 10}]]
} else {
set suff th
}
return "$n'$suff"
}
foreach start {0 250 1000} {
for {set n $start; set l {}} {$n<=$start+25} {incr n} {
lappend l [ordinal $n]
}
puts $l
}
- Output:
0'th 1'st 2'nd 3'rd 4'th 5'th 6'th 7'th 8'th 9'th 10'th 11'th 12'th 13'th 14'th 15'th 16'th 17'th 18'th 19'th 20'th 21'st 22'nd 23'rd 24'th 25'th 250'th 251'st 252'nd 253'rd 254'th 255'th 256'th 257'th 258'th 259'th 260'th 261'st 262'nd 263'rd 264'th 265'th 266'th 267'th 268'th 269'th 270'th 271'st 272'nd 273'rd 274'th 275'th 1000'th 1001'st 1002'nd 1003'rd 1004'th 1005'th 1006'th 1007'th 1008'th 1009'th 1010'th 1011'th 1012'th 1013'th 1014'th 1015'th 1016'th 1017'th 1018'th 1019'th 1020'th 1021'st 1022'nd 1023'rd 1024'th 1025'th
TypeScript
// N'th
function suffix(n: number): string {
var nMod10: number = n % 10;
var nMod100: number = n % 100;
if (nMod10 == 1 && nMod100 != 11)
return "st";
else if (nMod10 == 2 && nMod100 != 12)
return "nd";
else if (nMod10 == 3 && nMod100 != 13)
return "rd";
else
return "th";
}
function printImages(loLim: number, hiLim: number) {
for (i = loLim; i <= hiLim; i++)
process.stdout.write(`${i}` + suffix(i) + " ");
process.stdout.write("\n");
}
printImages( 0, 25);
printImages( 250, 265);
printImages(1000, 1025);
- Output:
0th 1st 2nd 3rd 4th 5th 6th 7th 8th 9th 10th 11th 12th 13th 14th 15th 16th 17th 18th 19th 20th 21st 22nd 23rd 24th 25th 250th 251st 252nd 253rd 254th 255th 256th 257th 258th 259th 260th 261st 262nd 263rd 264th 265th 1000th 1001st 1002nd 1003rd 1004th 1005th 1006th 1007th 1008th 1009th 1010th 1011th 1012th 1013th 1014th 1015th 1016th 1017th 1018th 1019th 1020th 1021st 1022nd 1023rd 1024th 1025th
UNIX Shell
nth() {
local ordinals=(th st nd rd)
local -i n=$1 i
if (( n < 0 )); then
printf '%s%s\n' - "$(nth $(( -n )) )"
return 0
fi
case $(( n % 100 )) in
11|12|13) i=0;;
*) (( i= n%10 < 4 ? n%10 : 0 ));;
esac
printf '%d%s\n' "$n" "${ordinals[i]}"
}
for n in {0..25} {250..265} {1000..1025}; do
nth $n
done | column
- Output:
0th 7th 14th 21st 252nd 259th 1000th 1007th 1014th 1021st 1st 8th 15th 22nd 253rd 260th 1001st 1008th 1015th 1022nd 2nd 9th 16th 23rd 254th 261st 1002nd 1009th 1016th 1023rd 3rd 10th 17th 24th 255th 262nd 1003rd 1010th 1017th 1024th 4th 11th 18th 25th 256th 263rd 1004th 1011th 1018th 1025th 5th 12th 19th 250th 257th 264th 1005th 1012th 1019th 6th 13th 20th 251st 258th 265th 1006th 1013th 1020th
V (Vlang)
fn ord(n int) string {
mut s := "th"
c := n % 10
if c in [1,2,3] {
if n%100/10 == 1 {
return "$n$s"
}
match c {
1 {
s = 'st'
}
2 {
s = 'nd'
}
3 {
s = 'rd'
}
else{}
}
}
return "$n$s"
}
fn main() {
for n := 0; n <= 25; n++ {
print("${ord(n)} ")
}
println('')
for n := 250; n <= 265; n++ {
print("${ord(n)} ")
}
println('')
for n := 1000; n <= 1025; n++ {
print("${ord(n)} ")
}
println('')
}
- Output:
0th 1st 2nd 3rd 4th 5th 6th 7th 8th 9th 10th 11th 12th 13th 14th 15th 16th 17th 18th 19th 20th 21st 22nd 23rd 24th 25th 250th 251st 252nd 253rd 254th 255th 256th 257th 258th 259th 260th 261st 262nd 263rd 264th 265th 1000th 1001st 1002nd 1003rd 1004th 1005th 1006th 1007th 1008th 1009th 1010th 1011th 1012th 1013th 1014th 1015th 1016th 1017th 1018th 1019th 1020th 1021st 1022nd 1023rd 1024th 1025th
VTL-2
The subroutine at 2000 sets S and T to the first and second letters of the suffix for N. The subroutine at 3000 prints N and the suffix.
1110 N=0
1120 #=3000
1130 N=N+1
1140 #=N<26*1120
1150 ?=" "
1210 N=250
1220 #=3000
1230 N=N+1
1240 #=N<266*1220
1250 ?=" "
1310 N=1000
1320 #=3000
1330 N=N+1
1340 #=N<1026*1320
1350 ?=" "
1400 #=9999
2000 R=!
2010 S=116
2020 T=104
2030 M=N/100*0+%
2040 #=M<20+(M>4)=2*R
2050 M=N/10*0+%
2060 #=M=1=0*2100
2070 S=115
2080 T=116
2090 #=R
2100 #=M=2=0*2140
2110 S=110
2120 T=100
2130 #=R
2140 #=M=3=0*R
2150 S=114
2160 T=100
2170 #=R
3000 Z=!
3010 $=32
3020 ?=N
3030 #=2000
3040 $=S
3050 $=T
3060 #=Z
- Output:
0th 1st 2nd 3rd 4th 5th 6th 7th 8th 9th 10th 11th 12th 13th 14th 15th 16th 17th 18th 19th 20th 21st 22nd 23rd 24th 25th 250th 251st 252nd 253rd 254th 255th 256th 257th 258th 259th 260th 261st 262nd 263rd 264th 265th 1000th 1001st 1002nd 1003rd 1004th 1005th 1006th 1007th 1008th 1009th 1010th 1011th 1012th 1013th 1014th 1015th 1016th 1017th 1018th 1019th 1020th 1021st 1022nd 1023rd 1024th 1025th
Wren
import "./fmt" for Fmt
var ranges = [ 0..25, 250..265, 1000..1025 ]
for (r in ranges) {
r.each { |i| Fmt.write("$r ", i) }
System.print("\n")
}
- Output:
0th 1st 2nd 3rd 4th 5th 6th 7th 8th 9th 10th 11th 12th 13th 14th 15th 16th 17th 18th 19th 20th 21st 22nd 23rd 24th 25th 250th 251st 252nd 253rd 254th 255th 256th 257th 258th 259th 260th 261st 262nd 263rd 264th 265th 1000th 1001st 1002nd 1003rd 1004th 1005th 1006th 1007th 1008th 1009th 1010th 1011th 1012th 1013th 1014th 1015th 1016th 1017th 1018th 1019th 1020th 1021st 1022nd 1023rd 1024th 1025th
XLISP
(DEFUN NTH (N)
(COND
((AND (> (MOD N 100) 3) (< (MOD N 100) 21)) `(,N TH))
((= (MOD N 10) 1) `(,N ST))
((= (MOD N 10) 2) `(,N ND))
((= (MOD N 10) 3) `(,N RD))
(T `(,N TH))))
(DEFUN RANGE (X Y)
(IF (<= X Y)
(CONS X (RANGE (+ X 1) Y))))
(DEFUN TEST-NTH ()
(DISPLAY (MAPCAR NTH (RANGE 1 25)))
(NEWLINE)
(DISPLAY (MAPCAR NTH (RANGE 250 265)))
(NEWLINE)
(DISPLAY (MAPCAR NTH (RANGE 1000 1025))))
(TEST-NTH)
- Output:
((1 ST) (2 ND) (3 RD) (4 TH) (5 TH) (6 TH) (7 TH) (8 TH) (9 TH) (10 TH) (11 TH) (12 TH) (13 TH) (14 TH) (15 TH) (16 TH) (17 TH) (18 TH) (19 TH) (20 TH) (21 ST) (22 ND) (23 RD) (24 TH) (25 TH)) ((250 TH) (251 ST) (252 ND) (253 RD) (254 TH) (255 TH) (256 TH) (257 TH) (258 TH) (259 TH) (260 TH) (261 ST) (262 ND) (263 RD) (264 TH) (265 TH)) ((1000 TH) (1001 ST) (1002 ND) (1003 RD) (1004 TH) (1005 TH) (1006 TH) (1007 TH) (1008 TH) (1009 TH) (1010 TH) (1011 TH) (1012 TH) (1013 TH) (1014 TH) (1015 TH) (1016 TH) (1017 TH) (1018 TH) (1019 TH) (1020 TH) (1021 ST) (1022 ND) (1023 RD) (1024 TH) (1025 TH))
XPL0
\N'th
code Rem=2, CrLf=9, IntIn=10, IntOut=11, Text=12;
procedure Suf(N, S);
integer N;
character S;
integer NMod10, NMod100;
begin
NMod10:= Rem(N/10);
NMod100:= Rem(N/100);
case of
NMod10 = 1 & NMod100 # 11: S(0):= "st";
NMod10 = 2 & NMod100 # 12: S(0):= "nd";
NMod10 = 3 & NMod100 # 13: S(0):= "rd"
other
S(0):= "th";
end;
procedure PrintImages(LoLim, HiLim);
integer LoLim, HiLim;
integer I;
character S;
begin
for I:= LoLim, HiLim do
begin
Suf(I, addr S);
IntOut(0, I); Text(0, S); Text(0, " ")
end;
CrLf(0)
end;
begin
PrintImages(0, 25);
PrintImages(250, 265);
PrintImages(1000, 1025)
end
- Output:
0th 1st 2nd 3rd 4th 5th 6th 7th 8th 9th 10th 11th 12th 13th 14th 15th 16th 17th 18th 19th 20th 21st 22nd 23rd 24th 25th 250th 251st 252nd 253rd 254th 255th 256th 257th 258th 259th 260th 261st 262nd 263rd 264th 265th 1000th 1001st 1002nd 1003rd 1004th 1005th 1006th 1007th 1008th 1009th 1010th 1011th 1012th 1013th 1014th 1015th 1016th 1017th 1018th 1019th 1020th 1021st 1022nd 1023rd 1024th 1025th
zkl
Two versions, your choice
#if 0
fcn addSuffix(n){
z:=n.abs()%100;
if(11<=z<=13) return(String(n,"th"));
z=z%10;
String(n,(z==1 and "st") or (z==2 and "nd") or (z==3 and "rd") or "th");
}
#else
fcn addSuffix(n){
var suffixes=T("th","st","nd","rd","th","th","th","th","th","th"); //0..10
z:=n.abs()%100;
String(n,(z<=10 or z>20) and suffixes[z%10] or "th");
}
#endif
[0..25] .apply(addSuffix).concat(",").println();
[250..265] .apply(addSuffix).concat(",").println();
[1000..1025].apply(addSuffix).concat(",").println();
- Output:
0th,1st,2nd,3rd,4th,5th,6th,7th,8th,9th,10th,11th,12th,13th,14th,15th,16th,17th,18th,19th,20th,21st,22nd,23rd,24th,25th 250th,251st,252nd,253rd,254th,255th,256th,257th,258th,259th,260th,261st,262nd,263rd,264th,265th 1000th,1001st,1002nd,1003rd,1004th,1005th,1006th,1007th,1008th,1009th,1010th,1011th,1012th,1013th,1014th,1015th,1016th,1017th,1018th,1019th,1020th,1021st,1022nd,1023rd,1024th,1025th
- Programming Tasks
- Ordinal numbers
- String manipulation
- 11l
- 68000 Assembly
- 8080 Assembly
- 8086 Assembly
- Action!
- Ada
- ALGOL 68
- ALGOL W
- APL
- AppleScript
- Arturo
- AutoHotkey
- AWK
- Babel
- BASIC
- ANSI BASIC
- Applesoft BASIC
- BaCon
- BASIC256
- BBC BASIC
- Chipmunk Basic
- Commodore BASIC
- FreeBASIC
- Gambas
- GW-BASIC
- Liberty BASIC
- Microsoft Small Basic
- Nascom BASIC
- PureBasic
- QBasic
- Run BASIC
- Sinclair ZX81 BASIC
- True BASIC
- UBasic/4tH
- VBA
- XBasic
- Yabasic
- ZX Spectrum Basic
- Batch File
- BCPL
- Befunge
- BQN
- C
- C sharp
- C++
- Clojure
- CLU
- COBOL
- Common Lisp
- Cowgol
- Crystal
- D
- Delphi
- Draco
- EasyLang
- Ed
- Elena
- Elixir
- ERRE
- F Sharp
- Factor
- Fennel
- Forth
- Fortran
- Go
- Haskell
- Icon
- Unicon
- Insitux
- J
- Java
- JavaScript
- Joy
- Jq
- Julia
- Kotlin
- Lambdatalk
- Lua
- Maple
- Mathematica
- Wolfram Language
- MATLAB
- MiniScript
- Modula-2
- Nanoquery
- Nim
- Objeck
- OCaml
- Oforth
- PARI/GP
- Pascal
- PascalABC.NET
- Perl
- Lingua::EN::Numbers::Ordinate
- Phix
- PHP
- Picat
- PicoLisp
- PL/I
- PL/M
- PowerShell
- Prolog
- Python
- Quackery
- R
- Racket
- Raku
- Red
- Refal
- REXX
- Ring
- RPL
- Ruby
- Rust
- Scala
- Sed
- Seed7
- SETL
- Set lang
- Sidef
- SQL
- Standard ML
- Stata
- Stringle
- Swift
- Tcl
- TypeScript
- UNIX Shell
- V (Vlang)
- VTL-2
- Wren
- Wren-fmt
- XLISP
- XPL0
- Zkl