N'th: Difference between revisions

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

edits