I would like to generate (as a return result of a function, or simply as the output of a program) the ordinal suffix of a positive integer concatenated to the number.



And so on, with the suffix repeating the initial 1-10 subpattern every 10 until 100, where the pattern ultimately starts over.

The input would be the number and the output the ordinal string as shown above.

What is the smallest algorithm for this?

  • \$\begingroup\$ Hi, NickC, and welcome to codegolf.SE! Just to clarify, do you mean that we should read a number like 11 as input, and output e.g. 11th? Is each number in the input on a separate line, and should the output numbers be on separate lines too? And do we need to handle more than one line of input? \$\endgroup\$ Jan 20, 2012 at 18:26
  • 1
    \$\begingroup\$ Are you looking for smallest algorithm or smallest code? \$\endgroup\$
    – Toto
    Jan 20, 2012 at 18:31
  • \$\begingroup\$ @Ilmari I am looking for 11 as input and 11th as output. I don't mind if it processes multiple lines but what I had in mind was processing just a single number. \$\endgroup\$
    – Nicole
    Jan 20, 2012 at 21:00
  • \$\begingroup\$ @M42 You know, I'm not really sure. I don't have a strict requirement - but I was probably thinking smallest algorithm. \$\endgroup\$
    – Nicole
    Jan 20, 2012 at 21:02

47 Answers 47


Python 2, 49 bytes

lambda n:`n`+'tsnrhtdd'[n%5*(n%100^15>4>n%10)::4]

An anonymous function. A full program would be counted at 55 bytes.

'tsnrhtdd'[i::4] encodes the suffixes th st nd rd for values of i from 0 to 3. Given this, all we need is a way to map the values of n to the index of the corresponding suffix, i. A straightforward expression that works is (n%10)*(n%10<4 and 10<n%100<14). We can easily shorten this by dropping the first set of parentheses and observing that n%5 gives the same results as n%10 for the values of n with the special suffixes. With a bit of trial and error, one may also shorten 10<n%100<14 to n%100^15>4, which can be chained with the other conditional to save even more bytes.

  • 3
    \$\begingroup\$ this is some black magic right here c: \$\endgroup\$
    – cat
    Feb 25, 2016 at 23:33
  • \$\begingroup\$ terminally-incoherent.com/blog/wp-content/uploads/2012/08/… \$\endgroup\$ Feb 26, 2016 at 17:10
  • \$\begingroup\$ I don't get it. Can anyone explain what's happening here? \$\endgroup\$
    – Pavel
    Sep 27, 2016 at 17:11
  • \$\begingroup\$ @Pavel I have updated my answer with an explanation. \$\endgroup\$
    – xsot
    Sep 28, 2016 at 8:55
  • \$\begingroup\$ Awesome! Brilliant way to think... \$\endgroup\$ Dec 5, 2017 at 0:45

Perl, 37 + 1 characters


This is a regexp substitution that appends the appropriate ordinal suffix to any numbers in $_ that are not already followed by a letter. To apply it to file input, use the p command line switch, like this:

perl -pe 's/1?\d\b/$&.((0,st,nd,rd)[$&]||th)/eg'

This is a complete Perl program that reads input from stdin and writes the processed output to stdout. The actual code is 37 chars long, but the p switch counts as one extra character.

Sample input:

This is the 1 line of the sample input...
...and this is the 2.
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 26 27 28 29 30
101 102 103 104 105 106 107 108 109 110


This is the 1st line of the sample input...
...and this is the 2nd.
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 26th 27th 28th 29th 30th
101st 102nd 103rd 104th 105th 106th 107th 108th 109th 110th

Numbers already followed by letters will be ignored, so feeding the output again through the filter won't change it. Spaces, commas and periods between numbers are not treated specially, so they're assumed to separate numbers like any other punctuation. Thus, e.g. 3.14159 becomes 3rd.14159th.

How does it work?

  • First, this is a global regexp replacement (s///g). The regexp being matched is 1?\d\b, where \d matches any digit and \b is a zero-width assertion matching the boundary between an alphanumeric and a non-alphanumeric character. Thus, 1?\d\b matches the last digit of any number, plus the previous digit if it happens to be 1.

  • In the substitution, which is evaluated as Perl code due to the /e switch, we take the matched string segment ($&) and append (.) to it the suffix obtained by using $& itself as an integer index to the list (0,st,nd,rd); if this suffix is zero or undefined (i.e. when $& is zero or greater than three), the || operator replaces it with th.

Edit: If the input is restricted to a single integer, then this 35 character solution will suffice:

  • 1
    \$\begingroup\$ Should be able to drop the g off the substitution if you specify that each number must be on it's own line. Also, that'd let you change the word boundary to be $. But overall, +1, damn clever solution. \$\endgroup\$
    – Mr. Llama
    Jan 20, 2012 at 21:05
  • \$\begingroup\$ @GigaWatt: Indeed, I wrote the code before NickC answered my question about the input format, so I made it as generic as possible. \$\endgroup\$ Jan 20, 2012 at 21:42
  • \$\begingroup\$ I know this is pretty old and rules were different, but using $\ instead of s/// shaves a few bytes off: Try it online! \$\endgroup\$ Jul 5, 2020 at 7:48
  • \$\begingroup\$ In fact, even shorter using /.../g syntax: Try it online! \$\endgroup\$ Jul 6, 2020 at 12:41

Python, 68 characters

  • 4
    \$\begingroup\$ This is really late, but you can take off 7 bytes by doing `i`+"tsnrhtdd". Otherwise, this is the exact solution I just got. \$\endgroup\$
    – DLosc
    Sep 5, 2015 at 20:27

Mathematica 39 45 bytes

Note: In recent versions of Mathematica, asking for the nth part of p, where p is undefined, generates an error message, but returns the correct answer anyway. I've added Quiet to prevent the error message from printing.



Quiet@StringSplit[SpokenString[p[[#]]]][[2]] &[31]


Quiet@StringSplit[SpokenString[p[[#]]]][[2]] &/@Range[21]

{"1st", "2nd", "3rd", "4th", "5th", "6th", "7th", "8th", "9th", "10th", "11th", "12th", "13th", "14th", "15th", "16th", "17th", "18th", "19th", "20th", "21st"}

How it works

SpokenString writes out an any valid Mathematica expression as it might be spoken. Below are two examples from the documentation for SpokenString,

SpokenString[Sqrt[x/(y + z)]]

"square root of the quantity x over the quantity y plus z" *)

SpokenString[Graphics3D[Sphere[{{0, 0, 0}, {1, 1, 1}}]], "DetailedGraphics" -> True]

"a three-dimensional graphic consisting of unit spheres centered at 0, 0, 0 and 1, 1, 1"

Now, for the example at hand,

Quiet@SpokenString[p[[#]]] &[31]

"the 31st element of p"

Let's represent the above string as a list of words:


{"the", "31st", "element", "of", "p"}

and take the second element...



  • \$\begingroup\$ I'm confused; where is p defined? EDIT: never mind, I see how you're using this; unfortunately it does not work on my system. :-/ \$\endgroup\$
    – Mr.Wizard
    Apr 6, 2013 at 13:13
  • \$\begingroup\$ It works on v.9.0.1. (I seem to recall that you are using 7.0.) Yes, p does not need to be defined. \$\endgroup\$
    – DavidC
    Apr 6, 2013 at 13:29
  • \$\begingroup\$ I understand that now. However, in v7 I get from SpokenString @ p[[117]] the output " part 117 of p". \$\endgroup\$
    – Mr.Wizard
    Apr 6, 2013 at 13:31
  • \$\begingroup\$ So SpokenString gets revised from time to time. I wouldn't be surprised it this code (codegolf.stackexchange.com/questions/8859/…) also doesn't work on v. 7. BTW, it wasn't meant to be an enduring solution. \$\endgroup\$
    – DavidC
    Apr 6, 2013 at 13:37
  • \$\begingroup\$ Hah, I hadn't seen that answer before. \$\endgroup\$
    – Mr.Wizard
    Apr 6, 2013 at 13:44

Javascript (ES6) 50 44 Bytes (non-competing)



  • takes imput as a string
  • Removed 6 bytes, thanks @user81655
  • 3
    \$\begingroup\$ a+ -> a+=, remove parentheses, \d -> ., remove [0], and if you take the number as a string: a.match`1?.$` instead of /1?.$/.exec(a). \$\endgroup\$
    – user81655
    Feb 26, 2016 at 21:49
  • \$\begingroup\$ You should probably add a notice this answer is non-competing, as ES6 didn't exist when the challenge was posted. \$\endgroup\$ Sep 28, 2016 at 12:19

Ruby, 60

It's not as good as the Perl entry, but I figured I'd work on my Ruby skills.

def o(n)n.to_s+%w{th st nd rd}[n/10%10==1||n%10>3?0:n%10]end

Function takes one integer argument, n, and returns a string as the ordinal form.

Works according to the following logic:
If the tens digit is a 1 or the ones digit is greater than 3 use the suffix 'th'; otherwise find the suffix from the array ['th', 'st', 'nd', 'rd'] using the final digit as the index.

  • \$\begingroup\$ o(113) is "113rd", should be "113th". The tens digit check doesn't account for numbers with more than two digits. \$\endgroup\$
    – hammar
    Jan 20, 2012 at 23:07
  • \$\begingroup\$ Ok, pitched in another %10 to compensate. Added 3 characters. (I feel like %10 appears enough where it should be shortened somehow, but I can't think of a solution) \$\endgroup\$
    – Mr. Llama
    Jan 20, 2012 at 23:12
  • 1
    \$\begingroup\$ You can never beat Perl in writing horrible incomprehensible code that's as short as possible :) \$\endgroup\$
    – jamylak
    Apr 6, 2013 at 10:25
  • \$\begingroup\$ Set a variable to 10? \$\endgroup\$
    – wizzwizz4
    Feb 25, 2016 at 17:57
  • \$\begingroup\$ I think setting a variable to n%10 is better. \$\endgroup\$ Feb 27, 2016 at 20:53

Javascript, 68 71

function o(n){return n+([,'st','nd','rd'][~~(n/10%10)-1?n%10:0]||'th')}

Joint effort with ItsCosmo.

EDIT: Wasn't working properly with numbers > 100

  • \$\begingroup\$ You could bring it down to 62 with: function o(n)n+([,'st','nd','rd'][~~(n/10%10)-1?n%10:0]||'th') and you can bring it down further to 54 if you are happy to use fat arrow notation: o=n=>n+([,'st','nd','rd'][~~(n/10%10)-1?n%10:0]||'th') \$\endgroup\$ Mar 3, 2014 at 12:04
  • \$\begingroup\$ @WallyWest For some reason, I really like that my solution works in modern browsers. Feel free to post your own answer; I think its sufficiently different. \$\endgroup\$
    – aebabis
    Mar 3, 2014 at 16:33
  • \$\begingroup\$ No, that's fine! Just providing a few alternatives! \$\endgroup\$ Mar 3, 2014 at 23:20
  • \$\begingroup\$ I made it work with ARGV: for(n of arguments)print(n+([,'st','nd','rd'][~~(n/10%10)-1?n%10:0]||'th')) \$\endgroup\$ May 6 at 1:57

JavaScript, 64 characters (ES3) or 47 characters (ES6)

ES3 (64 characters):

function(n){return n+=[,'st','nd','rd'][n%100>>3^1&&n%10]||'th'}

ES6 (47 characters):



The expression n % 100 >> 3 ^ 1 evaluates to 0 for any positive n ending with digits 0815. Thus, for any n mod 100 ending in 11, 12, or 13, the array lookup returns undefined, leading to a suffix of th.

For any positive n ending in other digits than 0815, the expression n % 100 >> 3 ^ 1 evaluates to a positive integer, invoking the expression n % 10 for array lookup, returning st,nd, or rd for n which ends with 1, 2, or 3. Otherwise, th.


Haskell, 95 100 chars

h=foldr g"th".show
g '1'"th"="1st"
g '2'"th"="2nd"
g '3'"th"="3rd"
g '1'[x,_,_]='1':x:"th"
g a s=a:s


*Main> map h [1..40]

Must be loaded with -XNoMonomorphismRestriction.

  • \$\begingroup\$ I'm seeing 100 here, but 95 using an infix operator. \$\endgroup\$ Jul 5, 2020 at 15:48
  • \$\begingroup\$ thanks, fixed. probably didn't know we have to count the newlines as well, back then. \$\endgroup\$
    – Will Ness
    Jul 6, 2020 at 10:29

Golfscript, 34 characters


J - 44 char

Nothing in J? This is an outrage!

(":,th`st`nd`rd{::~10 10(]*[(~:*])4>])/@#:])

Explained (note that 1 is boolean true in J and 0 is false):

  • 10 10(...)/@#:] - First we take the argument (]) and find the tens and ones digit (10 10 #:). Then, we will insert (...) between the two.
  • (]*[(~:*])4>]) - In this subexpression, but not the innermost one, ] will point to the ones digit and [ the tens digit.
  • [(~:*])4>] - ~: is J for "not-equals", so this takes the result of 4>] (i.e. whether or not the one digit is less than 4) and multiplies it by the result of tens ~: (4>]). Why would anyone do this? Consider the following:
    • If we are checking 10, 11, 12, 13, then tens is 1 (we are in the teens) and ones is less than 4, so tens ~: (4>]) is false and the result is 0*1 = 0.
    • If we are any other one of {X0, X1, X2, X3}, then clearly tens ~: (4>]) is true and we get out 1*1 = 1.
    • If ones is greater than four, then 4>] was 0 and it doesn't matter what happens to the test anymore, we will get 0 out regardless.
    • So to summarize, [(~:*])4>] is 1 if we are in {X0, X1, X2, X3} but not in the teens, and 0 otherwise.
  • ]* - Finally we multiply that result by the ones digit. So this product will be 0 if the number deserves a 'th' suffix, else its value.
  • th`st`nd`rd{::~ - We use the modified ones-digit from above to index the list of suffixes. 0 gets 'th', 1 gets 'st', and so on.
  • ":, - Finally, take the original number, convert it to a string (":), and then prepend it to the suffix.

Usage is obvious, though as-is the verb can only take one ordinal, not a list.

   (":,th`st`nd`rd{::~10 10(]*[(~:*])4>])/@#:]) 112         NB. single use
   (":,th`st`nd`rd{::~10 10(]*[(~:*])4>])/@#:]) 1 2 3 4 5   NB. doing it wrong
|length error
|       (":,th`st`nd`rd{::~10 10(]*[(~:*])4>])/@#:])1 2 3 4 5
   NB. i.5 10   makes a 5x10 grid of increasing integers
   NB. &.>      to operate on each integer separately, and box the result after
   (":,th`st`nd`rd{::~10 10(]*[(~:*])4>])/@#:])&.> i.5 10   NB. all better
|0th |1st |2nd |3rd |4th |5th |6th |7th |8th |9th |

APL (Dyalog Unicode), 38 36 bytes

Thanks to ngn for fixing a bug while maintaining byte count.

Anonymous tacit prefix function. Requires ⎕IO (Index Origin) set to 0, which is default on many systems. Even works for 0!

⍕,{2↑'thstndrd'↓⍨2×⊃⍵⌽∊1 0 8\⊂10↑⍳4}

Try it online!

{} anonymous lambda; is argument:

⍳4 first four ɩndices; [0,1,2,3]

10↑ take first ten elements from that, padding with zeros: [0,1,2,3,0,0,0,0,0,0]

 enclose to treat as single element; [[0,1,2,3,0,0,0,0,0,0]]

1 0 8\ expand to one copy, a prototypical copy (all-zero), eight copies;
   ⋮ (5 more)

ϵnlist (flatten);
   ⋮ (50 more)

⍵⌽ cyclically rotate left as many steps as indicated by the argument

 pick the first number (i.e. the argument-mod-100'th number)

 multiply two by that (gives 0, 2, 4, or 6)

'thstndrd'↓⍨drop that many characters from this string

2↑ take the first two of the remaining characters

⍕, concatenate the stringified argument to that

  • \$\begingroup\$ "31th"?­­­­­­­­ \$\endgroup\$
    – ngn
    May 3, 2018 at 17:49
  • \$\begingroup\$ ⍕,{2↑'thstndrd'↓⍨2×⊃⍵⌽∊1 0 8\⊂10↑⍳4} \$\endgroup\$
    – ngn
    May 3, 2018 at 18:23
  • \$\begingroup\$ sorry, I forgot to mention ⎕io←0. I can see you guessed that, but there are a few 1,2,3,4,0,0... that should be 0,1,2,3,0,0... \$\endgroup\$
    – ngn
    May 3, 2018 at 19:35
  • \$\begingroup\$ @ngn Fixed. And happens to work for 0 too! \$\endgroup\$
    – Adám
    May 3, 2018 at 20:26

Javascript, 75 60

With the new arrow notation:


Old version, without arrow notation (75 chars):

function o(s){return s+((0|s/10%10)==1?"th":[,"st","nd","rd"][s%10]||"th")}
  • \$\begingroup\$ 52 bytes (V8) \$\endgroup\$
    – user100411
    Jun 18, 2021 at 12:23

Bash (Cardinal to ordinal) 91 Bytes:

function c(){ case "$1" in *1[0-9]|*[04-9])"$1"th;;*1)"$1"st;;*2)"$1"nd;;*3)"$1"rd;;esac }
  • 1
    \$\begingroup\$ You can save at least 2 bytes in your solution, just rename your c2o function something with a 1-byte name. \$\endgroup\$
    – RGS
    Jul 5, 2020 at 9:45
  • \$\begingroup\$ Thank you @RGS. \$\endgroup\$
    – stephanmg
    Jul 6, 2020 at 12:43
  • 1
    \$\begingroup\$ 58 bytes. \$\endgroup\$
    – user100411
    Jun 18, 2021 at 11:53
  • \$\begingroup\$ Wow @tailsparkrabbitear I think you should write an answer. Or do you want me to edit my answer? \$\endgroup\$
    – stephanmg
    Jun 18, 2021 at 12:06
  • 1
    \$\begingroup\$ If you say so; posted mine. \$\endgroup\$
    – user100411
    Jun 18, 2021 at 12:18

PowerShell, 92

process{"$_$(switch -r($_){"(?<!1)1$"{'st'}"(?<!1)2$"{'nd'}"(?<!1)3$"{'rd'}default{'th'}})"}

Works with one number per line of input. Input is given through the pipeline. Making it work for only a single number doesn't reduce the size.


PHP, 151

I know that this program is not comparable to the others. Just felt like giving a solution.

<?$s=explode(' ',trim(fgets(STDIN)));foreach($s as$n){echo$n;echo(int)(($n%100)/10)==1?'th':($n%10==1?'st':($n%10==2?'nd':($n%10==3?'rd':'th')))."\n";}
  • \$\begingroup\$ you can save a few characters by using foreach($s as $n){echo$n; \$\endgroup\$
    – karth
    Apr 29, 2012 at 11:24

Javascript ES6, 52 chars


C#, 62 bytes


Full program and verification:

using System;

namespace OutputOrdinalNumbers
    class Program
        static void Main(string[] args)
            Func<int,string>f= n=>n+(n/10%10==1||(n%=10)<1||n>3?"th":n<2?"st":n<3?"nd":"rd");
            for (int i=1; i<=124; i++)
  • \$\begingroup\$ You can golf it by two bytes by changing the || to |. \$\endgroup\$ Nov 8, 2016 at 12:39

Mathematica 29 + 5 = 34 bytes


+5 bytes because Speak function must be called before using this built-in.



"1st "


"4,707th "


R, 79 76 bytes

Since there is no R solution yet... no tricks here, basic vector indexing, golfed down 3 chars thanks to Giuseppe. Previously tried index: [1+(x%%10)-(x%%100==11)] and [1+(x%%10)*(x%%100!=11)].


Try it online!

With substr, 79 bytes:


Try it online!

  • 1
    \$\begingroup\$ 1+x%%10*!x%%100==11 for the index? \$\endgroup\$
    – Giuseppe
    May 3, 2018 at 17:58
  • \$\begingroup\$ @Giuseppe I need to calm down on the parenthesis :). Clever use of ! in front of expression instead of !=. \$\endgroup\$
    – JayCe
    May 3, 2018 at 18:13
  • 1
    \$\begingroup\$ Yeah, the operators have weird precedence; ^ is really high, then %%-type operators, then */ and +- and I think == and &| comes next. ! has quite low precedence so you can use it as a separator between operations. \$\endgroup\$
    – Giuseppe
    May 3, 2018 at 18:17

Python 2.7, 137 chars

def b(n):
 for e,o in[[i,'th']for i in['11','12','13']+list('4567890')]+[['2','nd'],['1','st'],['3','rd']]:
  if n.endswith(e):return n+o

n should be a string

I know I'm beaten by the competition here already, but I thought I'd provide my idea anyways

this just basically generates a list of key, value pairs with number (as a string) ending e and the ordinal o. It attempts to match 'th' first (hence why I didn't use a dictionary), so that it won't accidentally return 'st', for example, when it should be 'th'. This will work for any positive integer

  • \$\begingroup\$ n[-1]==e is 5 chars shorter than n.endswith(e) \$\endgroup\$
    – hlt
    Feb 14, 2014 at 11:30

Scala 86

def x(n:Int)=n+{if(n%100/10==1)"th"else(("thstndrd"+"th"*6).sliding(2,2).toSeq(n%10))}

Scala 102:

def x(n:Int)=if((n%100)/10==1)n+"th"else if(n%10<4)n+("thstndrd".take(n+1)%5*2.drop(n%5*2))else n+"th"

102 as well:

def x(n:Int)=if((n%100)/10==1)n+"th"else if(n%10<4)n+("thstndrd".sliding(2,2).toSeq(n%10))else n+"th"


def x (n: Int) =
  n + { if (((n % 100) / 10) == 1) "th" 
        else (("thstndrd"  + ("th"  * 6)).sliding (2, 2).toSeq (n % 10))

C: 95 characters

A ridiculously long solution:


It needs to be mangled more.

  • \$\begingroup\$ A user flagged this saying "is incorrect: doesn't loop at all, i.e. it works just for a given number, not _all_ numbers below it. see ideone.com/pO6UwV ." That's not a reason to flag as moderators do not judge correctness, but it may be an issue. \$\endgroup\$ Apr 11, 2013 at 0:00
  • \$\begingroup\$ Based upon this comment from the question creator: "@Ilmari I am looking for 11 as input and 11th as output. I don't mind if it processes multiple lines but what I had in mind was processing just a single number. – NickC Jan 20 '12 at 21:00" it is doing what it should. I.e. it only should process a single number. \$\endgroup\$
    – Fors
    Apr 11, 2013 at 4:42
  • \$\begingroup\$ sorry for that flag; I misread the requirement, and didn't have enough rep at the time, to comment directly. Sorry again. :) \$\endgroup\$
    – Will Ness
    Apr 13, 2013 at 19:48


I'm pretty new to OCaml, but this is the shortest i could get.

let n x =
   let v = x mod 100 and string_v = string_of_int x in
   let z = v mod 10 in
   if v=11 || v=12 || v=13 then string_v^"th" 
   else if v = 1 || z = 1 then string_v^"st" else if v = 2 || z = 2 then string_v^"nd" else if v = 3 || z = 3 then string_v^"rd" else string_v^"th";;

I created a function n that takes a number as a parameter and does the work. Its long but thought it'd be great to have a functional example.

  • \$\begingroup\$ input: 11 would yield output: 11st \$\endgroup\$
    – user3614
    Jan 23, 2012 at 2:04
  • \$\begingroup\$ yeah, you're right.. I've made the correction. Thanks \$\endgroup\$ Feb 7, 2012 at 15:42
  • \$\begingroup\$ I've just tidied up the formatting on your code. Each line needs to be preceded by four spaces in order to be recognized as a code block. \$\endgroup\$
    – Gareth
    Feb 7, 2012 at 16:29
  • \$\begingroup\$ Wouldn't your first if check be shorter as if v>10 && v<14? I'm not familiar with ocaml, but is it necessary to have the string_v variable be so long? \$\endgroup\$
    – Gaffi
    Apr 29, 2012 at 3:49
  • \$\begingroup\$ No its not necessary, I could have chosen w or x. Just wanted something meaningful. But you're right, it would have made the code a little shorter. \$\endgroup\$ May 11, 2012 at 13:32

K - 44 char

It so happens that this is exactly as long as the J, and works in almost the same way.



  • x$: - First, we convert the operand x into a string, and then assign that back to x. We will need its string rep again later, so doing it now saves characters.
  • .:' - Convert (.:) each (') digit back into a number.
  • -2#0, - Append a 0 to the front of the list of digits (in case of single-digit numbers), and then take the last two.
  • {y*(y<4)*~1=x}. - Use the two digits as arguments x and y to this inner function, which returns y if y is less than 4 and x is not equal to 1, otherwise 0.
  • `th`st`nd`rd@ - Index the list of suffixes by this result.
  • x,$ - Convert the suffix from symbol to string, and append it to the original number.


  {x,$`th`st`nd`rd@{y*(y<4)*~1=x}.-2#0,.:'x$:} 3021
  {x,$`th`st`nd`rd@{y*(y<4)*~1=x}.-2#0,.:'x$:}' 1 2 3 4 11 12 13 14  /for each in list

C - 95 83 characters



    k=(n+9)%10; // xx1->0, xx2->1, xx3->2

We could do k=(n-1)%10 instead of adding 9, but for n=0 we would get an incorrect behaviour, because in C (-1)%10 evaluates to -1, not 9.


Javascript, 75

function s(i){return i+'thstndrd'.substr(~~(i/10%10)-1&&i%10<4?i%10*2:0,2)}

PHP, 98 bytes

function c($n){$c=$n%100;$s=['','st','nd','rd'];echo$c>9&&$c<14||!$s[$c%10]?$n.'th':$n.$s[$c%10];}

The 11-13 bit is killing me here. Works for any integer $n >= 0.

For any integer $n:

PHP, 103 bytes

function c($n){$c=abs($n%100);$s=['','st','nd','rd'];echo$c>9&&$c<14||!$s[$c%10]?$n.'th':$n.$s[$c%10];}

Oracle SQL 11.2, 101 bytes

SELECT:1||DECODE(ROUND(MOD(:1,100),-1),10,'th',DECODE(MOD(:1,10),1,'st',2,'nd',3,'rd','th'))FROM DUAL

Haxe, 83 chars

function o(n:Int){return ['th','st','nd','rd'][n%100>20||n%100<4?n%10>3?0:n%10:0];}

Based on the javascript version, fixed for n > 100 as well.

  • \$\begingroup\$ Two suggestions: 1) Remove all whitespace to save some bytes. Right now, I count 91 total. 2) This doesn't seem to be a full program or a function. Perhaps you should consider turning it into one of those. \$\endgroup\$ Feb 24, 2016 at 17:47
  • \$\begingroup\$ 1) yeah, but thats quite straightforward, so I tried to stay readable. \$\endgroup\$
    – csomakk
    Feb 28, 2016 at 19:21
  • \$\begingroup\$ 2) yeah, you're right, I just pasted it here so anyone can use it without reconverting from another lang. :) \$\endgroup\$
    – csomakk
    Feb 28, 2016 at 19:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.