Ordinals with Number.prototype extension

Number.prototype.toOrdinal()

/*  (c) APV, Artistic License 2, retain this notice  */

Number.prototype.toOrdinal = function () {
  var i = this.toString();
  if ( i.match(/\D/) ) return i;
  if ( i == 3 || i.match(/[^1]3$/) ) return i + 'rd';
  if ( i == 2 || i.match(/[^1]2$/) ) return i + 'nd';
  if ( i == 1 || i.match(/[^1]1$/) ) return i + 'st';
  return i + 'th';
}

Test script

<script type="text/javascript">//<![CDATA[
// import or include the above class prototype extension first
for ( var n = 0; n <= 150; n++ ) {
  document.write( n.toOrdinal() );
  if ( n != 150 ) document.write(' · ');
//]]></script>

Output

digg stumbleupon del.icio.us reddit Fark Technorati Faves