N'th: Difference between revisions
Content added Content deleted
Puppydrum64 (talk | contribs) |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 20: | Line 20: | ||
{{trans|Python}} |
{{trans|Python}} |
||
< |
<syntaxhighlight lang="11l">V _suffix = [‘th’, ‘st’, ‘nd’, ‘rd’, ‘th’, ‘th’, ‘th’, ‘th’, ‘th’, ‘th’] |
||
F nth(n) |
F nth(n) |
||
Line 26: | Line 26: | ||
L(j) (0..1000).step(250) |
L(j) (0..1000).step(250) |
||
print_elements(Array(j.+25).map(i -> nth(i)))</ |
print_elements(Array(j.+25).map(i -> nth(i)))</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 39: | Line 39: | ||
=={{header|68000 Assembly}}== |
=={{header|68000 Assembly}}== |
||
The function itself: |
The function itself: |
||
< |
<syntaxhighlight lang="68000devpac">Nth: |
||
MOVEQ #1,D1 |
MOVEQ #1,D1 |
||
.loop: |
.loop: |
||
Line 97: | Line 97: | ||
rd: |
rd: |
||
dc.b "rd",255 |
dc.b "rd",255 |
||
even</ |
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) |
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 |
MOVE.W #25,D7 |
||
JSR Nth |
JSR Nth |
||
Line 112: | Line 112: | ||
JSR Nth |
JSR Nth |
||
jmp * ;stop the cpu - we're done.</ |
jmp * ;stop the cpu - we're done.</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 120: | Line 120: | ||
=={{header|8080 Assembly}}== |
=={{header|8080 Assembly}}== |
||
< |
<syntaxhighlight lang="asm"> org 100h |
||
jmp demo |
jmp demo |
||
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
||
Line 196: | Line 196: | ||
dcr b ; Decrement counter |
dcr b ; Decrement counter |
||
jnz printnums ; If not zero, print next number |
jnz printnums ; If not zero, print next number |
||
ret</ |
ret</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 212: | Line 212: | ||
{{trans|8080 Assembly}} |
{{trans|8080 Assembly}} |
||
< |
<syntaxhighlight lang="asm"> bits 16 |
||
cpu 8086 |
cpu 8086 |
||
segment .text |
segment .text |
||
Line 269: | Line 269: | ||
dec bl ; Are we done yet? |
dec bl ; Are we done yet? |
||
jnz printn |
jnz printn |
||
ret</ |
ret</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 283: | Line 283: | ||
=={{header|Action!}}== |
=={{header|Action!}}== |
||
< |
<syntaxhighlight lang="action!">PROC Nth(CARD val,CHAR ARRAY s) |
||
CHAR ARRAY sfx |
CHAR ARRAY sfx |
||
BYTE d |
BYTE d |
||
Line 321: | Line 321: | ||
PutE() PutE() |
PutE() PutE() |
||
OD |
OD |
||
RETURN</ |
RETURN</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/N'th.png Screenshot from Atari 8-bit computer] |
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/N'th.png Screenshot from Atari 8-bit computer] |
||
Line 334: | Line 334: | ||
=={{header|Ada}}== |
=={{header|Ada}}== |
||
< |
<syntaxhighlight lang="ada">with Ada.Text_IO; |
||
procedure Nth is |
procedure Nth is |
||
Line 359: | 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 368: | 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 416: | Line 416: | ||
test nth( 1000, 1025 ) |
test nth( 1000, 1025 ) |
||
)</ |
)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 437: | Line 437: | ||
{{works with|Dyalog APL}} |
{{works with|Dyalog APL}} |
||
< |
<syntaxhighlight lang="apl"> nth←{ |
||
sfx←4 2⍴'stndrdth' |
sfx←4 2⍴'stndrdth' |
||
tens←(10<100|⍵)∧20>100|⍵ |
tens←(10<100|⍵)∧20>100|⍵ |
||
(⍕⍵),sfx[(4×tens)⌈(⍳3)⍳10|⍵;] |
(⍕⍵),sfx[(4×tens)⌈(⍳3)⍳10|⍵;] |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 459: | Line 459: | ||
=={{header|AppleScript}}== |
=={{header|AppleScript}}== |
||
< |
<syntaxhighlight lang="applescript">-- ORDINAL STRINGS ----------------------------------------------------------- |
||
-- ordinalString :: Int -> String |
-- ordinalString :: Int -> String |
||
Line 540: | Line 540: | ||
end |λ| |
end |λ| |
||
end script |
end script |
||
end uncurry</ |
end uncurry</syntaxhighlight> |
||
{{Out}} |
{{Out}} |
||
<pre>{{"0th", "1st", "2nd", "3rd", "4th", "5th", "6th", "7th", "8th", "9th", |
<pre>{{"0th", "1st", "2nd", "3rd", "4th", "5th", "6th", "7th", "8th", "9th", |
||
Line 553: | Line 553: | ||
=={{header|Applesoft BASIC}}== |
=={{header|Applesoft BASIC}}== |
||
< |
<syntaxhighlight lang="applesoftbasic">0 OP = 1 |
||
10 FOR N = 0 TO 25 : GOSUB 100 : NEXT |
10 FOR N = 0 TO 25 : GOSUB 100 : NEXT |
||
20 FOR N = 250 TO 265 : GOSUB 100 : NEXT |
20 FOR N = 250 TO 265 : GOSUB 100 : NEXT |
||
Line 571: | Line 571: | ||
260 IF NOT OP THEN NTH$ = "'" + NTH$ |
260 IF NOT OP THEN NTH$ = "'" + NTH$ |
||
270 NTH$ = STR$(N) + NTH$ |
270 NTH$ = STR$(N) + NTH$ |
||
280 RETURN</ |
280 RETURN</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 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>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|Arturo}}== |
=={{header|Arturo}}== |
||
< |
<syntaxhighlight lang="rebol">suffixes: ["th" "st" "nd" "rd" "th" "th" "th" "th" "th" "th"] |
||
nth: function [n][ |
nth: function [n][ |
||
Line 589: | Line 589: | ||
prints (nth i)++" " |
prints (nth i)++" " |
||
print "" |
print "" |
||
]</ |
]</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 601: | Line 601: | ||
=={{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 616: | Line 616: | ||
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 623: | Line 623: | ||
=={{header|AWK}}== |
=={{header|AWK}}== |
||
<syntaxhighlight lang="awk"> |
|||
<lang AWK> |
|||
# syntax: GAWK -f NTH.AWK |
# syntax: GAWK -f NTH.AWK |
||
BEGIN { |
BEGIN { |
||
Line 656: | Line 656: | ||
return(nthday) |
return(nthday) |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 666: | Line 666: | ||
=={{header|Babel}}== |
=={{header|Babel}}== |
||
< |
<syntaxhighlight lang="babel">((irregular ("st" "nd" "rd")) |
||
(main |
(main |
||
Line 693: | Line 693: | ||
. . } |
. . } |
||
{ -> %d "'th" . } |
{ -> %d "'th" . } |
||
ifte }))</ |
ifte }))</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 707: | Line 707: | ||
=={{header|BaCon}}== |
=={{header|BaCon}}== |
||
< |
<syntaxhighlight lang="freebasic">' Nth (sans apostrophes) |
||
FUNCTION nth$(NUMBER n) TYPE STRING |
FUNCTION nth$(NUMBER n) TYPE STRING |
||
LOCAL suffix |
LOCAL suffix |
||
Line 734: | Line 734: | ||
NEXT |
NEXT |
||
NEXT |
NEXT |
||
DATA 0, 25, 250, 265, 1000, 1025, -20, -11</ |
DATA 0, 25, 250, 265, 1000, 1025, -20, -11</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 750: | Line 750: | ||
=={{header|BASIC}}== |
=={{header|BASIC}}== |
||
==={{header|BASIC256}}=== |
==={{header|BASIC256}}=== |
||
<syntaxhighlight lang="basic256"> |
|||
<lang BASIC256> |
|||
function ordinal(n) |
function ordinal(n) |
||
ns$ = string(n) |
ns$ = string(n) |
||
Line 779: | Line 779: | ||
call imprimeOrdinal (1000, 1025) |
call imprimeOrdinal (1000, 1025) |
||
end |
end |
||
</syntaxhighlight> |
|||
</lang> |
|||
==={{header|Commodore BASIC}}=== |
==={{header|Commodore BASIC}}=== |
||
{{trans|Applesoft BASIC}} |
{{trans|Applesoft BASIC}} |
||
{{works with|Commodore BASIC|2.0}} |
{{works with|Commodore BASIC|2.0}} |
||
< |
<syntaxhighlight lang="basic"> |
||
0 REM ROSETTACODE.ORG |
0 REM ROSETTACODE.ORG |
||
1 REM N'TH |
1 REM N'TH |
||
Line 813: | Line 813: | ||
270 NTH$ = STR$(N) + NTH$ |
270 NTH$ = STR$(N) + NTH$ |
||
280 RETURN |
280 RETURN |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
Commodore 64 (40 column text) |
Commodore 64 (40 column text) |
||
Line 837: | Line 837: | ||
{{trans|Ada}} |
{{trans|Ada}} |
||
{{works with|Nascom ROM BASIC|4.7}} |
{{works with|Nascom ROM BASIC|4.7}} |
||
< |
<syntaxhighlight lang="basic"> |
||
10 REM N'th |
10 REM N'th |
||
20 LOLIM=0 |
20 LOLIM=0 |
||
Line 865: | Line 865: | ||
1540 SUF$="th" |
1540 SUF$="th" |
||
1550 RETURN |
1550 RETURN |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 874: | Line 874: | ||
==={{header|QBasic}}=== |
==={{header|QBasic}}=== |
||
< |
<syntaxhighlight lang="basic"> |
||
DECLARE FUNCTION sufijo$ (n%) |
DECLARE FUNCTION sufijo$ (n%) |
||
DECLARE SUB imprimeOrdinal (loLim%, hiLim%) |
DECLARE SUB imprimeOrdinal (loLim%, hiLim%) |
||
Line 911: | Line 911: | ||
END IF |
END IF |
||
END FUNCTION |
END FUNCTION |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Batch File}}== |
=={{header|Batch File}}== |
||
< |
<syntaxhighlight lang="dos">@echo off |
||
::Main thing... |
::Main thing... |
||
call :Nth 0 25 |
call :Nth 0 25 |
||
Line 934: | Line 934: | ||
) |
) |
||
echo."!range_output:~1!" |
echo."!range_output:~1!" |
||
goto :EOF</ |
goto :EOF</syntaxhighlight> |
||
{{Out}} |
{{Out}} |
||
<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" |
<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" |
||
Line 943: | Line 943: | ||
=={{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 958: | Line 958: | ||
NEXT |
NEXT |
||
PRINT |
PRINT |
||
ENDPROC</ |
ENDPROC</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 967: | Line 967: | ||
=={{header|BCPL}}== |
=={{header|BCPL}}== |
||
< |
<syntaxhighlight lang="bcpl">get "libhdr" |
||
// Generate ASCII string of number n with ordinal suffix |
// Generate ASCII string of number n with ordinal suffix |
||
Line 1,006: | Line 1,006: | ||
for i = 250 to 265 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)) |
for i = 1000 to 1025 do writef("%S*N", nth(i, buf)) |
||
$)</ |
$)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre style="height:50ex">0th |
<pre style="height:50ex">0th |
||
Line 1,079: | Line 1,079: | ||
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 1,090: | Line 1,090: | ||
htdd >$>:#,_$:vg |
htdd >$>:#,_$:vg |
||
v"d"\*!`3:%+55<9 |
v"d"\*!`3:%+55<9 |
||
>%55+/1-!!*:8g,^</ |
>%55+/1-!!*:8g,^</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,096: | Line 1,096: | ||
=={{header|BQN}}== |
=={{header|BQN}}== |
||
< |
<syntaxhighlight lang="bqn">Nth ← { |
||
sfx ← ⟨"th","st","nd","rd"⟩ |
sfx ← ⟨"th","st","nd","rd"⟩ |
||
(•Fmt 𝕩)∾sfx⊑˜ (1≠10|⌊𝕩÷10)×(3≥10|𝕩)×10|𝕩 |
(•Fmt 𝕩)∾sfx⊑˜ (1≠10|⌊𝕩÷10)×(3≥10|𝕩)×10|𝕩 |
||
} |
} |
||
∘‿4⥊ Nth¨ (↕26) ∾ (250+↕16) ∾ (1000+↕26)</ |
∘‿4⥊ Nth¨ (↕26) ∾ (250+↕16) ∾ (1000+↕26)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>┌─ |
<pre>┌─ |
||
Line 1,123: | Line 1,123: | ||
┘</pre> |
┘</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 1,174: | Line 1,174: | ||
return 0; |
return 0; |
||
}</ |
}</syntaxhighlight> |
||
Another method with dynamic memory allocation |
Another method with dynamic memory allocation |
||
< |
<syntaxhighlight lang="c">#include <stdlib.h> |
||
#include <stdio.h> |
#include <stdio.h> |
||
Line 1,208: | Line 1,208: | ||
print_range(250, 265); |
print_range(250, 265); |
||
print_range(1000, 1025); |
print_range(1000, 1025); |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>Set [0,25] : |
<pre>Set [0,25] : |
||
Line 1,219: | Line 1,219: | ||
=={{header|C sharp|C#}}== |
=={{header|C sharp|C#}}== |
||
{{trans|Ruby}} |
{{trans|Ruby}} |
||
< |
<syntaxhighlight lang="csharp">using System; |
||
using System.Linq; |
using System.Linq; |
||
Line 1,250: | Line 1,250: | ||
Console.WriteLine(string.Join(" ", Enumerable.Range(1000, 26).Select(Ordinalize))); |
Console.WriteLine(string.Join(" ", Enumerable.Range(1000, 26).Select(Ordinalize))); |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
Dotnet 5 version without LINQ |
Dotnet 5 version without LINQ |
||
< |
<syntaxhighlight lang="csharp">using System; |
||
static string Ordinalize(int i) |
static string Ordinalize(int i) |
||
Line 1,273: | Line 1,273: | ||
PrintRange(0, 25); |
PrintRange(0, 25); |
||
PrintRange(250, 265); |
PrintRange(250, 265); |
||
PrintRange(1000, 1025);</ |
PrintRange(1000, 1025);</syntaxhighlight> |
||
{{Out}} |
{{Out}} |
||
Line 1,281: | Line 1,281: | ||
=={{header|C++}}== |
=={{header|C++}}== |
||
< |
<syntaxhighlight lang="cpp">#include <string> |
||
#include <iostream> |
#include <iostream> |
||
Line 1,321: | Line 1,321: | ||
return 0; |
return 0; |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>Set [0,25] : |
<pre>Set [0,25] : |
||
Line 1,331: | Line 1,331: | ||
=={{header|Clojure}}== |
=={{header|Clojure}}== |
||
< |
<syntaxhighlight lang="clojure"> |
||
(defn n-th [n] |
(defn n-th [n] |
||
(str n |
(str n |
||
Line 1,346: | Line 1,346: | ||
(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 1,357: | Line 1,357: | ||
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> |
||
Line 1,366: | Line 1,366: | ||
=={{header|CLU}}== |
=={{header|CLU}}== |
||
< |
<syntaxhighlight lang="clu">nth = proc (n: int) returns (string) |
||
num: string := int$unparse(n) |
num: string := int$unparse(n) |
||
sfx: array[string] := array[string]$[0: "th", "st", "nd", "rd"] |
sfx: array[string] := array[string]$[0: "th", "st", "nd", "rd"] |
||
Line 1,396: | Line 1,396: | ||
do_range(250,265) |
do_range(250,265) |
||
do_range(1000,1025) |
do_range(1000,1025) |
||
end start_up</ |
end start_up</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>0th 1st 2nd 3rd 4th 5th 6th 7th 8th 9th |
<pre>0th 1st 2nd 3rd 4th 5th 6th 7th 8th 9th |
||
Line 1,411: | Line 1,411: | ||
=={{header|COBOL}}== |
=={{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. |
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. |
PROGRAM-ID. NTH-PROGRAM. |
||
DATA DIVISION. |
DATA DIVISION. |
||
Line 1,441: | Line 1,441: | ||
IF LAST-DIGIT IS EQUAL TO 1 THEN MOVE 'ST' TO SUFFIX. |
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 2 THEN MOVE 'ND' TO SUFFIX. |
||
IF LAST-DIGIT IS EQUAL TO 3 THEN MOVE 'RD' TO SUFFIX.</ |
IF LAST-DIGIT IS EQUAL TO 3 THEN MOVE 'RD' TO SUFFIX.</syntaxhighlight> |
||
Output: |
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> |
<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 1,454: | Line 1,454: | ||
(svref suffixes last-digit)))) |
(svref suffixes last-digit)))) |
||
(format nil "~a~a" number suffix))) |
(format nil "~a~a" number suffix))) |
||
</syntaxhighlight> |
|||
</lang> |
|||
A more concise, albeit less readable version: |
A more concise, albeit less readable version: |
||
< |
<syntaxhighlight lang="lisp">(defun add-suffix (n) |
||
(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> |
|||
</lang> |
|||
Display the results: |
Display the results: |
||
< |
<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 1,477: | Line 1,477: | ||
=={{header|Cowgol}}== |
=={{header|Cowgol}}== |
||
< |
<syntaxhighlight lang="cowgol">include "cowgol.coh"; |
||
include "strings.coh"; |
include "strings.coh"; |
||
Line 1,516: | Line 1,516: | ||
test(0, 25); |
test(0, 25); |
||
test(250,265); |
test(250,265); |
||
test(1000,1025);</ |
test(1000,1025);</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,531: | Line 1,531: | ||
=={{header|Crystal}}== |
=={{header|Crystal}}== |
||
{{trans|Ruby}} |
{{trans|Ruby}} |
||
< |
<syntaxhighlight lang="ruby">struct Int |
||
def ordinalize |
def ordinalize |
||
num = self.abs |
num = self.abs |
||
Line 1,549: | Line 1,549: | ||
[(0..25),(250..265),(1000..1025)].each{|r| puts r.map{ |n| n.ordinalize }.join(", "); puts} |
[(0..25),(250..265),(1000..1025)].each{|r| puts r.map{ |n| n.ordinalize }.join(", "); puts} |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,561: | Line 1,561: | ||
=={{header|D}}== |
=={{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 1,572: | Line 1,572: | ||
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 |
||
Line 1,582: | Line 1,582: | ||
=={{header|Draco}}== |
=={{header|Draco}}== |
||
< |
<syntaxhighlight lang="draco">proc nonrec nth(word n; *char buf) *char: |
||
channel output text ch; |
channel output text ch; |
||
open(ch, buf); |
open(ch, buf); |
||
Line 1,613: | Line 1,613: | ||
print_range(250, 265); |
print_range(250, 265); |
||
print_range(1000, 1025) |
print_range(1000, 1025) |
||
corp</ |
corp</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>0th 1st 2nd 3rd 4th 5th 6th 7th 8th 9th |
<pre>0th 1st 2nd 3rd 4th 5th 6th 7th 8th 9th |
||
Line 1,627: | Line 1,627: | ||
{{trans|C#}} |
{{trans|C#}} |
||
ELENA 5.0 : |
ELENA 5.0 : |
||
< |
<syntaxhighlight lang="elena">import extensions; |
||
import system'math; |
import system'math; |
||
import system'routines; |
import system'routines; |
||
Line 1,655: | Line 1,655: | ||
console.printLine(new Range(250,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])) |
console.printLine(new Range(1000,26).selectBy(mssgconst ordinalize<op>[1])) |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,664: | Line 1,664: | ||
=={{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 1,683: | Line 1,683: | ||
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 1,693: | Line 1,693: | ||
=={{header|ERRE}}== |
=={{header|ERRE}}== |
||
<syntaxhighlight lang="erre"> |
|||
<lang ERRE> |
|||
PROGRAM NTH_SOLVE |
PROGRAM NTH_SOLVE |
||
Line 1,717: | Line 1,717: | ||
NTH(1000,1025) |
NTH(1000,1025) |
||
END PROGRAM |
END PROGRAM |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,730: | Line 1,730: | ||
=={{header|F_Sharp|F#}}== |
=={{header|F_Sharp|F#}}== |
||
< |
<syntaxhighlight lang="fsharp">open System |
||
let ordinalsuffix n = |
let ordinalsuffix n = |
||
Line 1,745: | Line 1,745: | ||
[1000..1025] |> show |
[1000..1025] |> show |
||
0 |
0 |
||
</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,752: | Line 1,752: | ||
=={{header|Factor}}== |
=={{header|Factor}}== |
||
< |
<syntaxhighlight lang="factor">USING: io kernel math math.order math.parser math.ranges qw |
||
sequences ; |
sequences ; |
||
IN: rosetta-code.nth |
IN: rosetta-code.nth |
||
Line 1,765: | Line 1,765: | ||
[ [ n'th write bl ] each nl ] tri@ ; |
[ [ n'th write bl ] each nl ] tri@ ; |
||
MAIN: n'th-demo</ |
MAIN: n'th-demo</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,774: | Line 1,774: | ||
=={{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 10 20 within if 0 .r ." th " exit then |
||
Line 1,783: | Line 1,783: | ||
26 0 test 266 250 test 1026 1000 test ; |
26 0 test 266 250 test 1026 1000 test ; |
||
tests</ |
tests</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,814: | Line 1,814: | ||
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 1,904: | Line 1,904: | ||
end function ordinate |
end function ordinate |
||
end program nth</ |
end program nth</syntaxhighlight> |
||
=={{header|FreeBASIC}}== |
=={{header|FreeBASIC}}== |
||
< |
<syntaxhighlight lang="freebasic">' FB 1.05.0 Win64 |
||
' Apostrophes NOT used as incorrect English |
' Apostrophes NOT used as incorrect English |
||
Line 1,945: | Line 1,945: | ||
Print "Press any key to quit" |
Print "Press any key to quit" |
||
Sleep</ |
Sleep</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,956: | Line 1,956: | ||
=={{header|Gambas}}== |
=={{header|Gambas}}== |
||
'''[https://gambas-playground.proko.eu/?gist=6d60749ae886a37f128e75cffc6c7118 Click this link to run this code]''' |
'''[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 siNums As Short[] = [0, 25, 250, 265, 1000, 1025] |
||
Dim siCount, siNumbers As Short |
Dim siCount, siNumbers As Short |
||
Line 1,972: | Line 1,972: | ||
Next |
Next |
||
End </ |
End </syntaxhighlight> |
||
Output: |
Output: |
||
<pre> |
<pre> |
||
Line 1,984: | Line 1,984: | ||
=={{header|Go}}== |
=={{header|Go}}== |
||
{{trans|Raku}} |
{{trans|Raku}} |
||
< |
<syntaxhighlight lang="go">package main |
||
import "fmt" |
import "fmt" |
||
Line 2,020: | Line 2,020: | ||
} |
} |
||
fmt.Println() |
fmt.Println() |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 2,031: | Line 2,031: | ||
{{trans|Ada}} |
{{trans|Ada}} |
||
{{works with|PC-BASIC|any}} |
{{works with|PC-BASIC|any}} |
||
< |
<syntaxhighlight lang="qbasic"> |
||
10 ' N'th |
10 ' N'th |
||
20 LET LOLIM% = 0 |
20 LET LOLIM% = 0 |
||
Line 2,060: | Line 2,060: | ||
1800 LET SUF$ = "th" |
1800 LET SUF$ = "th" |
||
2000 RETURN |
2000 RETURN |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 2,069: | Line 2,069: | ||
=={{header|Haskell}}== |
=={{header|Haskell}}== |
||
< |
<syntaxhighlight lang="haskell">import Data.Array |
||
ordSuffs :: Array Integer String |
ordSuffs :: Array Integer String |
||
Line 2,087: | Line 2,087: | ||
printOrdSuffs [ 0.. 25] |
printOrdSuffs [ 0.. 25] |
||
printOrdSuffs [ 250.. 265] |
printOrdSuffs [ 250.. 265] |
||
printOrdSuffs [1000..1025]</ |
printOrdSuffs [1000..1025]</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 2,098: | Line 2,098: | ||
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 2,108: | Line 2,108: | ||
(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 2,123: | Line 2,123: | ||
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 2,134: | Line 2,134: | ||
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 2,162: | Line 2,162: | ||
} |
} |
||
} |
} |
||
}</ |
}</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 2,211: | Line 2,211: | ||
; |
; |
||
} |
} |
||
}</ |
}</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 2,221: | Line 2,221: | ||
===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 2,244: | Line 2,244: | ||
}).join('\n\n'); |
}).join('\n\n'); |
||
}());</ |
}());</syntaxhighlight> |
||
{{Out}} |
{{Out}} |
||
< |
<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=== |
===ES6=== |
||
< |
<syntaxhighlight lang="javascript">(function (lstTestRanges) { |
||
'use strict' |
'use strict' |
||
Line 2,283: | Line 2,283: | ||
); |
); |
||
})([[0, 25], [250, 265], [1000, 1025]]);</ |
})([[0, 25], [250, 265], [1000, 1025]]);</syntaxhighlight> |
||
Line 2,298: | Line 2,298: | ||
=={{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 2,316: | Line 2,316: | ||
([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 2,326: | Line 2,326: | ||
=={{header|Julia}}== |
=={{header|Julia}}== |
||
<lang |
<syntaxhighlight lang="julia">using Printf</syntaxhighlight> |
||
'''Function''': |
'''Function''': |
||
< |
<syntaxhighlight lang="julia">function ordinal(n::Integer) |
||
n < 0 && throw(DomainError()) |
n < 0 && throw(DomainError()) |
||
suffixes = ("st", "nd", "rd") |
suffixes = ("st", "nd", "rd") |
||
Line 2,339: | Line 2,339: | ||
end |
end |
||
return string(n, suf) |
return string(n, suf) |
||
end</ |
end</syntaxhighlight> |
||
'''Main''': |
'''Main''': |
||
< |
<syntaxhighlight 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() |
(i - 1) % 10 == 0 && println() |
||
Line 2,358: | Line 2,358: | ||
(i - 1) % 10 == 0 && println() |
(i - 1) % 10 == 0 && println() |
||
@printf("%7s", ordinal(n)) |
@printf("%7s", ordinal(n)) |
||
end</ |
end</syntaxhighlight> |
||
'''Main2''': |
'''Main2''': |
||
< |
<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)) " |
NthA(x::Integer) = "$(x)'$(Nth(x)) " |
||
[0:25..., 250:265..., 1000:1025...] .|> NthA .|> print;</ |
[0:25..., 250:265..., 1000:1025...] .|> NthA .|> print;</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 2,379: | Line 2,379: | ||
=={{header|Kotlin}}== |
=={{header|Kotlin}}== |
||
< |
<syntaxhighlight lang="scala">fun Int.ordinalAbbrev() = |
||
if (this % 100 / 10 == 1) "th" |
if (this % 100 / 10 == 1) "th" |
||
else when (this % 10) { 1 -> "st" 2 -> "nd" 3 -> "rd" else -> "th" } |
else when (this % 10) { 1 -> "st" 2 -> "nd" 3 -> "rd" else -> "th" } |
||
Line 2,387: | Line 2,387: | ||
fun main(args: Array<String>) { |
fun main(args: Array<String>) { |
||
listOf((0..25), (250..265), (1000..1025)).forEach { println(it.ordinalAbbrev()) } |
listOf((0..25), (250..265), (1000..1025)).forEach { println(it.ordinalAbbrev()) } |
||
}</ |
}</syntaxhighlight> |
||
=={{header|Lambdatalk}}== |
=={{header|Lambdatalk}}== |
||
Translation from the javascript entry |
Translation from the javascript entry |
||
< |
<syntaxhighlight lang="scheme"> |
||
{def fnOrdinalForm |
{def fnOrdinalForm |
||
{lambda {:n} |
{lambda {:n} |
||
Line 2,411: | Line 2,411: | ||
1011th 1012th 1013th 1014th 1015th 1016th 1017th 1018th 1019th 1020th |
1011th 1012th 1013th 1014th 1015th 1016th 1017th 1018th 1019th 1020th |
||
1021st 1022nd 1023rd 1024th 1025th |
1021st 1022nd 1023rd 1024th 1025th |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Liberty BASIC}}== |
=={{header|Liberty BASIC}}== |
||
{{trans|Ada}} |
{{trans|Ada}} |
||
{{works with|Just BASIC|any}} |
{{works with|Just BASIC|any}} |
||
<syntaxhighlight lang="lb"> |
|||
<lang lb> |
|||
call printImages 0, 25 |
call printImages 0, 25 |
||
call printImages 250, 265 |
call printImages 250, 265 |
||
Line 2,449: | Line 2,449: | ||
end if |
end if |
||
end function |
end function |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 2,459: | Line 2,459: | ||
=={{header|Lua}}== |
=={{header|Lua}}== |
||
The apostrophe just looks weird if you ask me. No one did, obviously. |
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 |
local lastTwo, lastOne = n % 100, n % 10 |
||
if lastTwo > 3 and lastTwo < 21 then return "th" end |
if lastTwo > 3 and lastTwo < 21 then return "th" end |
||
Line 2,470: | Line 2,470: | ||
function Nth (n) return n .. "'" .. getSuffix(n) end |
function Nth (n) return n .. "'" .. getSuffix(n) end |
||
for i = 0, 25 do print(Nth(i), Nth(i + 250), Nth(i + 1000)) end</ |
for i = 0, 25 do print(Nth(i), Nth(i + 250), Nth(i + 1000)) end</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 2,503: | Line 2,503: | ||
=={{header|Maple}}== |
=={{header|Maple}}== |
||
< |
<syntaxhighlight lang="maple">toOrdinal := proc(n:: nonnegint) |
||
if 1 <= n and n <= 10 then |
if 1 <= n and n <= 10 then |
||
if n >= 4 then |
if n >= 4 then |
||
Line 2,527: | Line 2,527: | ||
end do; |
end do; |
||
printf("\n\n"); |
printf("\n\n"); |
||
end do;</ |
end do;</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 2,539: | Line 2,539: | ||
=={{header|Mathematica}}/{{header|Wolfram Language}}== |
=={{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 2,550: | Line 2,550: | ||
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> |
||
{{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 2,559: | Line 2,559: | ||
=={{header|MATLAB}}== |
=={{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 2,576: | Line 2,576: | ||
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 2,585: | Line 2,585: | ||
=={{header|Microsoft Small Basic}}== |
=={{header|Microsoft Small Basic}}== |
||
{{trans|Ada}} |
{{trans|Ada}} |
||
< |
<syntaxhighlight lang="microsoftsmallbasic"> |
||
loLim = 0 |
loLim = 0 |
||
hiLim = 25 |
hiLim = 25 |
||
Line 2,618: | Line 2,618: | ||
EndIf |
EndIf |
||
EndSub |
EndSub |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|MiniScript}}== |
=={{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. |
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 > 3 and n < 20 then return n + "th" |
if n > 3 and n < 20 then return n + "th" |
||
if n % 10 == 1 then return n + "st" |
if n % 10 == 1 then return n + "st" |
||
Line 2,641: | Line 2,641: | ||
test 250, 265 |
test 250, 265 |
||
test 1000, 1025 |
test 1000, 1025 |
||
print out.join</ |
print out.join</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 2,649: | Line 2,649: | ||
{{trans|Ada}} |
{{trans|Ada}} |
||
{{works with|ADW Modula-2|any (Compile with the linker option ''Console Application'').}} |
{{works with|ADW Modula-2|any (Compile with the linker option ''Console Application'').}} |
||
< |
<syntaxhighlight lang="modula2"> |
||
MODULE Nth; |
MODULE Nth; |
||
Line 2,695: | Line 2,695: | ||
PrintImages(1000, 1025); |
PrintImages(1000, 1025); |
||
END Nth. |
END Nth. |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Nanoquery}}== |
=={{header|Nanoquery}}== |
||
{{trans|Java}} |
{{trans|Java}} |
||
< |
<syntaxhighlight lang="nanoquery">def ordinalAbbrev(n) |
||
if int(n % 100 / 10) = 1 |
if int(n % 100 / 10) = 1 |
||
return "th" |
return "th" |
||
Line 2,728: | Line 2,728: | ||
print (i + "'" + ordinalAbbrev(i) + " ") |
print (i + "'" + ordinalAbbrev(i) + " ") |
||
end |
end |
||
println</ |
println</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 |
||
Line 2,736: | Line 2,736: | ||
=={{header|Nim}}== |
=={{header|Nim}}== |
||
{{trans|Python}} |
{{trans|Python}} |
||
< |
<syntaxhighlight lang="nim">const Suffix = ["th", "st", "nd", "rd", "th", "th", "th", "th", "th", "th"] |
||
proc nth(n: Natural): string = |
proc nth(n: Natural): string = |
||
Line 2,744: | Line 2,744: | ||
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 2,754: | Line 2,754: | ||
=={{header|Objeck}}== |
=={{header|Objeck}}== |
||
{{trans|Java}} |
{{trans|Java}} |
||
< |
<syntaxhighlight lang="objeck">class Nth { |
||
function : OrdinalAbbrev(n : Int ) ~ String { |
function : OrdinalAbbrev(n : Int ) ~ String { |
||
ans := "th"; # most of the time it should be "th" |
ans := "th"; # most of the time it should be "th" |
||
Line 2,788: | Line 2,788: | ||
}; |
}; |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{output}} |
{{output}} |
||
Line 2,799: | Line 2,799: | ||
=={{header|OCaml}}== |
=={{header|OCaml}}== |
||
<syntaxhighlight lang="ocaml"> |
|||
<lang Ocaml> |
|||
let show_nth n = |
let show_nth n = |
||
if (n mod 10 = 1) && (n mod 100 <> 11) then "st" |
if (n mod 10 = 1) && (n mod 100 <> 11) then "st" |
||
Line 2,815: | Line 2,815: | ||
List.iter show_ordinals [ (0,25); (250,265); (1000,1025) ] |
List.iter show_ordinals [ (0,25); (250,265); (1000,1025) ] |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
Line 2,826: | Line 2,826: | ||
=={{header|Oforth}}== |
=={{header|Oforth}}== |
||
< |
<syntaxhighlight lang="oforth">: nth(n) |
||
| r | |
| r | |
||
n "th" over 10 mod ->r |
n "th" over 10 mod ->r |
||
Line 2,832: | Line 2,832: | ||
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> |
||
{{out}} |
{{out}} |
||
Line 2,853: | Line 2,853: | ||
(Spurious apostrophes intentionally omitted, following Raku.) |
(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 2,866: | Line 2,866: | ||
=={{header|Pascal}}== |
=={{header|Pascal}}== |
||
nearly copy of [[N'th#Ada|Ada]] |
nearly copy of [[N'th#Ada|Ada]] |
||
< |
<syntaxhighlight lang="pascal">Program n_th; |
||
function Suffix(N: NativeInt):AnsiString; |
function Suffix(N: NativeInt):AnsiString; |
||
Line 2,898: | Line 2,898: | ||
Print_Images( 250, 265); |
Print_Images( 250, 265); |
||
Print_Images(1000, 1025); |
Print_Images(1000, 1025); |
||
end.</ |
end.</syntaxhighlight> |
||
{{Out}} shortened |
{{Out}} shortened |
||
<pre> |
<pre> |
||
Line 2,909: | Line 2,909: | ||
{{Trans|Raku}} |
{{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; |
||
my %irregulars = ( 1 => 'st', |
my %irregulars = ( 1 => 'st', |
||
2 => 'nd', |
2 => 'nd', |
||
Line 2,924: | Line 2,924: | ||
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> |
||
{{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 2,932: | Line 2,932: | ||
Alternatively, can use a library. |
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|Phix}}== |
=={{header|Phix}}== |
||
<!--< |
<!--<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;">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> |
||
Line 2,955: | Line 2,955: | ||
<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: #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> |
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span> |
||
<!--</ |
<!--</syntaxhighlight>--> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 2,970: | Line 2,970: | ||
</pre> |
</pre> |
||
Alternatively you can use the builtin ord(), and a more functional style, as follows (same output) |
Alternatively you can use the builtin ord(), and a more functional style, as follows (same output) |
||
<!--< |
<!--<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;">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;">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> |
||
Line 2,979: | Line 2,979: | ||
<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: #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> |
<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}}== |
=={{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 3,006: | Line 3,006: | ||
} |
} |
||
echo "\n"; |
echo "\n"; |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 3,018: | Line 3,018: | ||
===Prolog style=== |
===Prolog style=== |
||
{{trans|Prolog}} |
{{trans|Prolog}} |
||
< |
<syntaxhighlight lang="picat">nth2(N) = N.to_string() ++ Th => |
||
( tween(N) -> Th = "th" |
( tween(N) -> Th = "th" |
||
; 1 = N mod 10 -> Th = "st" |
; 1 = N mod 10 -> Th = "st" |
||
Line 3,024: | Line 3,024: | ||
; 3 = N mod 10 -> Th = "rd" |
; 3 = N mod 10 -> Th = "rd" |
||
; Th = "th" ). |
; Th = "th" ). |
||
tween(N) => Tween = N mod 100, between(11, 13, Tween).</ |
tween(N) => Tween = N mod 100, between(11, 13, Tween).</syntaxhighlight> |
||
===Function with explicit conditions=== |
===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,"st"), N mod 10 = 1 => true. |
||
nth3(N) = cc(N,"nd"), N mod 10 = 2 => true. |
nth3(N) = cc(N,"nd"), N mod 10 = 2 => true. |
||
Line 3,033: | Line 3,033: | ||
nth3(N) = cc(N,"th") => true. |
nth3(N) = cc(N,"th") => true. |
||
% helper function |
% helper function |
||
cc(N,Th) = N.to_string() ++ Th.</ |
cc(N,Th) = N.to_string() ++ Th.</syntaxhighlight> |
||
===List of suffixes=== |
===List of suffixes=== |
||
{{trans|Python}} |
{{trans|Python}} |
||
< |
<syntaxhighlight lang="picat">nth4(N) = Nth => |
||
Suffix = ["th","st","nd","rd","th","th","th","th","th","th"], |
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").</ |
Nth = N.to_string() ++ cond((N mod 100 <= 10; N mod 100 > 20), Suffix[1 + N mod 10], "th").</syntaxhighlight> |
||
===Test=== |
===Test=== |
||
< |
<syntaxhighlight lang="picat">go => |
||
Ranges = [ 0..25, 250..265, 1000..1025], |
Ranges = [ 0..25, 250..265, 1000..1025], |
||
foreach(Range in Ranges) println([nth2(I) : I in Range]) |
foreach(Range in Ranges) println([nth2(I) : I in Range]) |
||
end, |
end, |
||
nl.</ |
nl.</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 3,054: | Line 3,054: | ||
=={{header|PicoLisp}}== |
=={{header|PicoLisp}}== |
||
< |
<syntaxhighlight lang="picolisp">(de rangeth (A B) |
||
(mapcar |
(mapcar |
||
'((I) |
'((I) |
||
Line 3,071: | Line 3,071: | ||
(prinl (glue " " (rangeth 1000 1025))) |
(prinl (glue " " (rangeth 1000 1025))) |
||
(bye)</ |
(bye)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 3,080: | Line 3,080: | ||
=={{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 3,105: | Line 3,105: | ||
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 3,118: | Line 3,118: | ||
=={{header|PowerShell}}== |
=={{header|PowerShell}}== |
||
< |
<syntaxhighlight lang="powershell">function nth($inp){ |
||
$suffix = "th" |
$suffix = "th" |
||
Line 3,131: | Line 3,131: | ||
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>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 |
<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 |
||
Line 3,139: | Line 3,139: | ||
===An Alternate Version=== |
===An Alternate Version=== |
||
This is, I think, is a more "PowerShelly" way: |
This is, I think, is a more "PowerShelly" way: |
||
<syntaxhighlight lang="powershell"> |
|||
<lang PowerShell> |
|||
function Get-Nth ([int]$Number) |
function Get-Nth ([int]$Number) |
||
{ |
{ |
||
Line 3,157: | Line 3,157: | ||
251..265 | 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 |
1001..1025 | ForEach-Object {Get-Nth $_} | Format-Wide {$_} -Column 5 -Force |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{Out}} |
{{Out}} |
||
<pre> |
<pre> |
||
Line 3,184: | Line 3,184: | ||
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 3,200: | Line 3,200: | ||
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 3,214: | Line 3,214: | ||
=={{header|PureBasic}}== |
=={{header|PureBasic}}== |
||
< |
<syntaxhighlight lang="purebasic">Procedure.s Suffix(n.i) |
||
Select n%10 |
Select n%10 |
||
Case 1 : If n%100<>11 : ProcedureReturn "st" : EndIf |
Case 1 : If n%100<>11 : ProcedureReturn "st" : EndIf |
||
Line 3,232: | Line 3,232: | ||
put(250,265) |
put(250,265) |
||
put(1000,1025) |
put(1000,1025) |
||
Input()</ |
Input()</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 3,239: | Line 3,239: | ||
=={{header|Python}}== |
=={{header|Python}}== |
||
< |
<syntaxhighlight lang="python">_suffix = ['th', 'st', 'nd', 'rd', 'th', 'th', 'th', 'th', 'th', 'th'] |
||
def nth(n): |
def nth(n): |
||
Line 3,246: | Line 3,246: | ||
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 3,254: | Line 3,254: | ||
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 3,268: | Line 3,268: | ||
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 3,282: | Line 3,282: | ||
=={{header|Quackery}}== |
=={{header|Quackery}}== |
||
< |
<syntaxhighlight lang="quackery"> [ table ] is suffix ( n --> $ ) |
||
$ "th st nd rd th th th th th th" |
$ "th st nd rd th th th th th th" |
||
Line 3,305: | Line 3,305: | ||
250 265 test |
250 265 test |
||
cr |
cr |
||
1000 1025 test</ |
1000 1025 test</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 3,324: | Line 3,324: | ||
{{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 3,341: | Line 3,341: | ||
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 3,374: | Line 3,374: | ||
=={{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 3,383: | Line 3,383: | ||
(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 |
||
Line 3,392: | Line 3,392: | ||
(formerly Perl 6) |
(formerly Perl 6) |
||
(Spurious apostrophes intentionally omitted.) |
(Spurious apostrophes intentionally omitted.) |
||
<lang |
<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' ) } |
sub nth ($n) { $n ~ ( %irregulars{$n % 100} // %irregulars{$n % 10} // 'th' ) } |
||
say .list».&nth for [^26], [250..265], [1000..1025];</ |
say .list».&nth for [^26], [250..265], [1000..1025];</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 3,402: | Line 3,402: | ||
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> |
||
If you want to get Unicodally fancy, use this version instead: |
If you want to get Unicodally fancy, use this version instead: |
||
<lang |
<syntaxhighlight lang="raku" line>my %irregulars = <1 ˢᵗ 2 ⁿᵈ 3 ʳᵈ>, (11..13 X=> 'ᵗʰ'); |
||
sub nth ($n) { $n ~ ( %irregulars{$n % 100} // %irregulars{$n % 10} // 'ᵗʰ' ) } |
sub nth ($n) { $n ~ ( %irregulars{$n % 100} // %irregulars{$n % 10} // 'ᵗʰ' ) } |
||
say .list».&nth for [^26], [250..265], [1000..1025];</ |
say .list».&nth for [^26], [250..265], [1000..1025];</syntaxhighlight> |
||
{{out}} |
{{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> |
<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> |
||
Line 3,413: | Line 3,413: | ||
=={{header|Red}}== |
=={{header|Red}}== |
||
< |
<syntaxhighlight lang="rebol">Red[] |
||
nth: function [n][ |
nth: function [n][ |
||
Line 3,430: | Line 3,430: | ||
test 0 25 |
test 0 25 |
||
test 250 265 |
test 250 265 |
||
test 1000 1025</ |
test 1000 1025</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 3,442: | Line 3,442: | ||
Negative numbers and fractions are also handled. |
Negative numbers and fractions are also handled. |
||
< |
<syntaxhighlight lang="rexx">/*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 0, 25 /*display the 1st range of numbers. */ |
||
call tell 250, 265 /* " " 2nd " " " */ |
call tell 250, 265 /* " " 2nd " " " */ |
||
Line 3,454: | Line 3,454: | ||
return /*return to invoker. */ |
return /*return to invoker. */ |
||
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
||
th: parse arg z; x=abs(z); return z||word('th st nd rd',1+x//10*(x//100%10\==1)*(x//10<4))</ |
th: parse arg z; x=abs(z); return z||word('th st nd rd',1+x//10*(x//100%10\==1)*(x//10<4))</syntaxhighlight> |
||
'''output''' using the default inputs: |
'''output''' using the default inputs: |
||
<pre> |
<pre> |
||
Line 3,470: | Line 3,470: | ||
=={{header|Ring}}== |
=={{header|Ring}}== |
||
< |
<syntaxhighlight lang="ring"> |
||
for nr = 0 to 25 |
for nr = 0 to 25 |
||
see Nth(nr) + Nth(nr + 250) + Nth(nr + 1000) + nl |
see Nth(nr) + Nth(nr + 250) + Nth(nr + 1000) + nl |
||
Line 3,486: | Line 3,486: | ||
func Nth n |
func Nth n |
||
return "" + n + "'" + getSuffix(n) + " " |
return "" + n + "'" + getSuffix(n) + " " |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{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 3,508: | Line 3,508: | ||
[(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 3,519: | Line 3,519: | ||
=={{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) | (2, 12) | (3, 13) => "th", |
(1, 11) | (2, 12) | (3, 13) => "th", |
||
Line 3,538: | Line 3,538: | ||
println!(); |
println!(); |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 3,550: | Line 3,550: | ||
=={{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 3,569: | Line 3,569: | ||
println(); |
println(); |
||
(1000 to 1025).foreach(abbrevNumber) |
(1000 to 1025).foreach(abbrevNumber) |
||
}</ |
}</syntaxhighlight> |
||
=={{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 3,600: | Line 3,600: | ||
printImages( 250, 265); |
printImages( 250, 265); |
||
printImages(1000, 1025); |
printImages(1000, 1025); |
||
end func;</ |
end func;</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 3,611: | Line 3,611: | ||
=={{header|Set lang}}== |
=={{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. |
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 t 50 |
||
set h 51 |
set h 51 |
||
Line 3,631: | Line 3,631: | ||
set ! R |
set ! R |
||
set ! D |
set ! D |
||
> EOF</ |
> EOF</syntaxhighlight> |
||
Input: I, Output: O |
Input: I, Output: O |
||
<pre>I: 1, O: 1'ST |
<pre>I: 1, O: 1'ST |
||
Line 3,642: | Line 3,642: | ||
=={{header|Sidef}}== |
=={{header|Sidef}}== |
||
{{trans|Raku}} |
{{trans|Raku}} |
||
< |
<syntaxhighlight lang="ruby">func nth(n) { |
||
static irregulars = Hash(<1 ˢᵗ 2 ⁿᵈ 3 ʳᵈ 11 ᵗʰ 12 ᵗʰ 13 ᵗʰ>...) |
static irregulars = Hash(<1 ˢᵗ 2 ⁿᵈ 3 ʳᵈ 11 ᵗʰ 12 ᵗʰ 13 ᵗʰ>...) |
||
n.to_s + (irregulars{n % 100} \\ irregulars{n % 10} \\ 'ᵗʰ') |
n.to_s + (irregulars{n % 100} \\ irregulars{n % 10} \\ 'ᵗʰ') |
||
Line 3,649: | Line 3,649: | ||
for r in [0..25, 250..265, 1000..1025] { |
for r in [0..25, 250..265, 1000..1025] { |
||
say r.map {|n| nth(n) }.join(" ") |
say r.map {|n| nth(n) }.join(" ") |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 3,659: | Line 3,659: | ||
=={{header|Sinclair ZX81 BASIC}}== |
=={{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.) |
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 |
20 GOSUB 160 |
||
30 PRINT N$;" "; |
30 PRINT N$;" "; |
||
Line 3,682: | Line 3,682: | ||
220 IF N$(LEN N$)="3" THEN LET S$="RD" |
220 IF N$(LEN N$)="3" THEN LET S$="RD" |
||
230 LET N$=N$+S$ |
230 LET N$=N$+S$ |
||
240 RETURN</ |
240 RETURN</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 3,691: | Line 3,691: | ||
=={{header|SQL}}== |
=={{header|SQL}}== |
||
Oracle |
Oracle |
||
<syntaxhighlight lang="sql"> |
|||
<lang SQL> |
|||
select level card, |
select level card, |
||
to_char(to_date(level,'j'),'fmjth') ord |
to_char(to_date(level,'j'),'fmjth') ord |
||
Line 3,699: | Line 3,699: | ||
select to_char(to_date(5373485,'j'),'fmjth') |
select to_char(to_date(5373485,'j'),'fmjth') |
||
from dual; |
from dual; |
||
</syntaxhighlight> |
|||
</lang> |
|||
<pre> |
<pre> |
||
CARD ORD |
CARD ORD |
||
Line 3,728: | Line 3,728: | ||
=={{header|Standard ML}}== |
=={{header|Standard ML}}== |
||
< |
<syntaxhighlight lang="sml">local |
||
val v = Vector.tabulate (10, fn 1 => "st" | 2 => "nd" | 3 => "rd" | _ => "th") |
val v = Vector.tabulate (10, fn 1 => "st" | 2 => "nd" | 3 => "rd" | _ => "th") |
||
fun getSuffix x = |
fun getSuffix x = |
||
Line 3,739: | Line 3,739: | ||
(* some test ouput *) |
(* some test ouput *) |
||
val () = (print o concat o List.tabulate) |
val () = (print o concat o List.tabulate) |
||
(26, fn i => String.concatWith "\t" (map nth [i, i + 250, i + 1000]) ^ "\n")</ |
(26, fn i => String.concatWith "\t" (map nth [i, i + 250, i + 1000]) ^ "\n")</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>0th 250th 1000th |
<pre>0th 250th 1000th |
||
Line 3,771: | Line 3,771: | ||
We reuse here the '''maps''' function defined in the task [[Apply a callback to an array]]. |
We reuse here the '''maps''' function defined in the task [[Apply a callback to an array]]. |
||
< |
<syntaxhighlight lang="stata">mata |
||
function maps(f,a) { |
function maps(f,a) { |
||
nr = rows(a) |
nr = rows(a) |
||
Line 3,788: | Line 3,788: | ||
maps(&nth(),((0::25),(250::275),(1000::1025))) |
maps(&nth(),((0::25),(250::275),(1000::1025))) |
||
end</ |
end</syntaxhighlight> |
||
'''Output:''' |
'''Output:''' |
||
Line 3,823: | Line 3,823: | ||
=={{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 3,851: | Line 3,851: | ||
print("\(i)\(addSuffix(i)) ") |
print("\(i)\(addSuffix(i)) ") |
||
} |
} |
||
println()</ |
println()</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 3,860: | Line 3,860: | ||
=={{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 3,874: | Line 3,874: | ||
} |
} |
||
puts $l |
puts $l |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 3,884: | Line 3,884: | ||
=={{header|True BASIC}}== |
=={{header|True BASIC}}== |
||
< |
<syntaxhighlight lang="basic"> |
||
SUB sufijo (n) |
SUB sufijo (n) |
||
LET n = INT(n) |
LET n = INT(n) |
||
Line 3,920: | Line 3,920: | ||
CALL imprimeOrdinal (1000, 1025) |
CALL imprimeOrdinal (1000, 1025) |
||
END |
END |
||
</syntaxhighlight> |
|||
</lang> |
|||
== {{header|TypeScript}} == |
== {{header|TypeScript}} == |
||
{{trans|Ada}} |
{{trans|Ada}} |
||
< |
<syntaxhighlight lang="javascript"> |
||
// N'th |
// N'th |
||
function suffix(n: number): string { |
function suffix(n: number): string { |
||
Line 3,948: | Line 3,948: | ||
printImages( 250, 265); |
printImages( 250, 265); |
||
printImages(1000, 1025); |
printImages(1000, 1025); |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 3,957: | Line 3,957: | ||
=={{header|uBasic/4tH}}== |
=={{header|uBasic/4tH}}== |
||
<lang>For x = 0 to 25 ' Test range 0..25 |
<syntaxhighlight lang="text">For x = 0 to 25 ' Test range 0..25 |
||
Push x : GoSub _PrintOrdinal |
Push x : GoSub _PrintOrdinal |
||
Next x : Print |
Next x : Print |
||
Line 3,991: | Line 3,991: | ||
110 Print "st"; : Return |
110 Print "st"; : Return |
||
120 Print "nd"; : Return |
120 Print "nd"; : Return |
||
130 Print "rd"; : Return</ |
130 Print "rd"; : Return</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 4,001: | Line 4,001: | ||
=={{header|UNIX Shell}}== |
=={{header|UNIX Shell}}== |
||
{{works with|Bourne Again Shell}} |
{{works with|Bourne Again Shell}} |
||
< |
<syntaxhighlight lang="sh">nth() { |
||
local ordinals=(th st nd rd) |
local ordinals=(th st nd rd) |
||
local -i n=$1 i |
local -i n=$1 i |
||
Line 4,016: | Line 4,016: | ||
for n in {0..25} {250..265} {1000..1025}; do |
for n in {0..25} {250..265} {1000..1025}; do |
||
nth $n |
nth $n |
||
done | column</ |
done | column</syntaxhighlight> |
||
{{Out}} |
{{Out}} |
||
Line 4,028: | Line 4,028: | ||
=={{header|VBA}}== |
=={{header|VBA}}== |
||
{{trans|Phix}}< |
{{trans|Phix}}<syntaxhighlight lang="vb">Private Function ordinals() As Variant |
||
ordinals = [{"th","st","nd","rd"}] |
ordinals = [{"th","st","nd","rd"}] |
||
End Function |
End Function |
||
Line 4,047: | Line 4,047: | ||
Debug.Print |
Debug.Print |
||
Next i |
Next i |
||
End Sub</ |
End Sub</syntaxhighlight>{{out}} |
||
<pre> 0th 1st 2nd 3rd 4th 5th 6th 7th 8th 9th |
<pre> 0th 1st 2nd 3rd 4th 5th 6th 7th 8th 9th |
||
10th 11th 12th 13th 14th 15th 16th 17th 18th 19th |
10th 11th 12th 13th 14th 15th 16th 17th 18th 19th |
||
Line 4,061: | Line 4,061: | ||
=={{header|Vlang}}== |
=={{header|Vlang}}== |
||
{{trans|go}} |
{{trans|go}} |
||
< |
<syntaxhighlight lang="vlang">fn ord(n int) string { |
||
mut s := "th" |
mut s := "th" |
||
c := n % 10 |
c := n % 10 |
||
Line 4,097: | Line 4,097: | ||
} |
} |
||
println('') |
println('') |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 4,108: | Line 4,108: | ||
=={{header|Wren}}== |
=={{header|Wren}}== |
||
{{libheader|Wren-fmt}} |
{{libheader|Wren-fmt}} |
||
< |
<syntaxhighlight lang="ecmascript">import "/fmt" for Conv |
||
var ranges = [ 0..25, 250..265, 1000..1025 ] |
var ranges = [ 0..25, 250..265, 1000..1025 ] |
||
Line 4,114: | Line 4,114: | ||
r.each { |i| System.write("%(Conv.ord(i)) ") } |
r.each { |i| System.write("%(Conv.ord(i)) ") } |
||
System.print("\n") |
System.print("\n") |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 4,128: | Line 4,128: | ||
{{trans|Ada}} |
{{trans|Ada}} |
||
{{works with|Windows XBasic}} |
{{works with|Windows XBasic}} |
||
< |
<syntaxhighlight lang="xbasic"> |
||
PROGRAM "nth" |
PROGRAM "nth" |
||
VERSION "0.0002" |
VERSION "0.0002" |
||
Line 4,164: | Line 4,164: | ||
END FUNCTION |
END FUNCTION |
||
END PROGRAM |
END PROGRAM |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 4,173: | Line 4,173: | ||
=={{header|XLISP}}== |
=={{header|XLISP}}== |
||
< |
<syntaxhighlight lang="xlisp">(DEFUN NTH (N) |
||
(COND |
(COND |
||
((AND (> (MOD N 100) 3) (< (MOD N 100) 21)) `(,N TH)) |
((AND (> (MOD N 100) 3) (< (MOD N 100) 21)) `(,N TH)) |
||
Line 4,192: | Line 4,192: | ||
(DISPLAY (MAPCAR NTH (RANGE 1000 1025)))) |
(DISPLAY (MAPCAR NTH (RANGE 1000 1025)))) |
||
(TEST-NTH)</ |
(TEST-NTH)</syntaxhighlight> |
||
{{out}} |
{{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)) |
<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)) |
||
Line 4,201: | Line 4,201: | ||
{{trans|Ada}} |
{{trans|Ada}} |
||
{{works with|EXPL-32}} |
{{works with|EXPL-32}} |
||
< |
<syntaxhighlight lang="xpl0"> |
||
\N'th |
\N'th |
||
code Rem=2, CrLf=9, IntIn=10, IntOut=11, Text=12; |
code Rem=2, CrLf=9, IntIn=10, IntOut=11, Text=12; |
||
Line 4,238: | Line 4,238: | ||
PrintImages(1000, 1025) |
PrintImages(1000, 1025) |
||
end |
end |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 4,248: | Line 4,248: | ||
=={{header|Yabasic}}== |
=={{header|Yabasic}}== |
||
{{trans|Liberty BASIC}} |
{{trans|Liberty BASIC}} |
||
< |
<syntaxhighlight lang="xbasic"> |
||
sub ordinal$ (n) |
sub ordinal$ (n) |
||
NMod10 = mod(n, 10) |
NMod10 = mod(n, 10) |
||
Line 4,278: | Line 4,278: | ||
imprimeOrdinal (1000, 1025) |
imprimeOrdinal (1000, 1025) |
||
end |
end |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{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 4,296: | Line 4,296: | ||
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> |
||
Line 4,308: | Line 4,308: | ||
=={{header|ZX Spectrum Basic}}== |
=={{header|ZX Spectrum Basic}}== |
||
< |
<syntaxhighlight lang="basic"> 10 FOR n=0 TO 25 |
||
20 GO SUB 140 |
20 GO SUB 140 |
||
30 PRINT n$;" "; |
30 PRINT n$;" "; |
||
Line 4,329: | Line 4,329: | ||
200 IF n$(LEN n$)="3" THEN LET s$="rd" |
200 IF n$(LEN n$)="3" THEN LET s$="rd" |
||
210 LET n$=n$+s$ |
210 LET n$=n$+s$ |
||
220 RETURN</ |
220 RETURN</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> |