Rebol3 Code Examplex


N'th

Write a function that when given an integer greater than or equal to zero returns a string of the number followed by an apostrophe then the ordinal suffix.

Rebol [
    title: "Rosetta code: N'th"
    file:  %N'th.r3
    url:   https://rosettacode.org/wiki/N'th
]

nth: function [
    "Returns a string with the ordinal suffix appended to an integer"
    n [integer!] "The integer to format as an ordinal"
][
    d:  n % 10
    dd: n % 100
    suffix: either any [
        all [ dd > 3 dd < 20 ]
        d < 1 d > 4
        1 = to integer! n / 10
    ] [4] [d]

    ajoin [n #"'" pick [st nd rd th] suffix]
]

;; test:

foreach [low high] [0 25  250 265  1000 1025][
    for i low high 1 [
        if zero? i % 10 [prin newline]
        prin pad nth i -8
    ]
    print ""
]

Output:


    0'th    1'st    2'nd    3'rd    4'th    5'th    6'th    7'th    8'th    9'th
   10'th   11'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