Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>My own suggestion, would be:</p> <pre><code>$(".ordinal").text(function (i, t) { i++; var str = i.toString().slice(-1), ord = ''; switch (str) { case '1': ord = 'st'; break; case '2': ord = 'nd'; break; case '3': ord = 'rd'; break; case '4': case '5': case '6': case '7': case '8': case '9': case '0': ord = 'th'; break; } return i + ord; }); </code></pre> <p><a href="http://jsfiddle.net/davidThomas/BMTxa/2/" rel="noreferrer">JS Fiddle demo</a>.</p> <p>This effectively takes the incremented number (<code>i++</code>, in order to start from <code>1</code> not <code>0</code>), converts it to a string, then looks at the last number of that string. This <em>should</em> work for any number, since the ordinal is based purely on that last number.</p> <p>You could also extend the <code>Number</code> prototype to implement this functionality:</p> <pre><code>Number.prototype.ordinate = function(){ var num = this + 1, last = num.toString().slice(-1), ord = ''; switch (last) { case '1': ord = 'st'; break; case '2': ord = 'nd'; break; case '3': ord = 'rd'; break; case '4': case '5': case '6': case '7': case '8': case '9': case '0': ord = 'th'; break; } return num.toString() + ord; }; $(".ordinal").text(function (i, t) { return i.ordinate(); }); </code></pre> <p><a href="http://jsfiddle.net/davidThomas/BMTxa/3/" rel="noreferrer">JS Fiddle demo</a>.</p> <p>Edited to offer a slight alternative:</p> <pre><code>Number.prototype.ordinate = function(){ var num = this, last = num.toString().slice(-1), ord = ''; switch (last) { case '1': ord = 'st'; break; case '2': ord = 'nd'; break; case '3': ord = 'rd'; break; default: ord = 'th'; break; } return num.toString() + ord; }; $(".ordinal").text(function (i,t) { return t.replace(/(\d+)/g, function(a){ return parseInt(a, 10).ordinate(); }); }); </code></pre> <p><a href="http://jsfiddle.net/davidThomas/BMTxa/7/" rel="noreferrer">JS Fiddle demo</a>.</p> <p>This essentially iterates over each <code>.ordinal</code> element, replacing the numbers present with the (same) numbers with the ordinal suffix added to it.</p> <hr /> <p><strong>Edited</strong> to address the problem, raised in the comments, below, that <code>11</code>, <code>12</code> and <code>13</code> were receiving the ordinal suffix of <code>st</code>, <code>nd</code> and <code>rd</code> (respectively). This is now corrected to being <code>th</code> in all cases:</p> <pre><code>Number.prototype.ordinate = function(){ var num = this, numStr = num.toString(), last = numStr.slice(-1), len = numStr.length, ord = ''; switch (last) { case '1': ord = numStr.slice(-2) === '11' ? 'th' : 'st'; break; case '2': ord = numStr.slice(-2) === '12' ? 'th' : 'nd'; break; case '3': ord = numStr.slice(-2) === '13' ? 'th' : 'rd'; break; default: ord = 'th'; break; } return num.toString() + ord; }; </code></pre> <p><a href="http://jsfiddle.net/davidThomas/BMTxa/11/" rel="noreferrer">JS Fiddle demo</a>.</p> <p>References:</p> <ul> <li><a href="https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Array/slice" rel="noreferrer"><code>slice()</code></a>.</li> <li><a href="https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Statements/switch" rel="noreferrer"><code>switch()</code></a>.</li> <li><a href="http://api.jquery.com/text/" rel="noreferrer"><code>text()</code></a>.</li> <li><a href="https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Object/toString" rel="noreferrer"><code>toString()</code></a>.</li> </ul>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload