Question

Is there any elegant way how to get ordinals in word format in js/coffee? Something like this:

ordinalInWord(1) # => "first"
ordinalInWord(2) # => "second"
ordinalInWord(5) # => "fifth"
Was it helpful?

Solution

I'm afraid the ordinals aren't regular enough to avoid typing each of them out.

function ordinalInWord( cardinal ) {
    var ordinals = [ 'zeroth', 'first', 'second', 'third' /* and so on */ ];

    return ordinals[ cardinal ];
}

If you need the function work past 20, you can take advantage of the pattern that emerges:

function ordinalInWord( cardinal ) {
    var ordinals = [ 'zeroth', 'first', 'second', 'third' /* and so on up to "twentieth" */ ];
    var tens = {
        20: 'twenty',
        30: 'thirty',
        40: 'forty' /* and so on up to 90 */
    };
    var ordinalTens = {
        30: 'thirtieth',
        40: 'fortieth',
        50: 'fiftieth' /* and so on */
    };

    if( cardinal <= 20 ) {                    
        return ordinals[ cardinal ];
    }

    if( cardinal % 10 === 0 ) {
        return ordinalTens[ cardinal ];
    }

    return tens[ cardinal - ( cardinal % 10 ) ] + ordinals[ cardinal % 10 ];
}

Demo: http://jsfiddle.net/AQCqK/

Expanding that to work past 99 shouldn't be difficult.

OTHER TIPS

Here's a solution for anything up to 64-bit max. I can't imagine why you'd need it, but why is never the question.

var nth = function(i){
  var n = ["one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"];
  var s = ["zeroth", "first", "second", "third", "fourth", "fifth", "sixth", "seventh", "eighth", "ninth", "tenth",
       "eleventh", "twelfth", "thirteenth", "fourteenth", "fifteenth", "sixteenth", "seventeenth", "eighteenth", "nineteenth"];
  var p = ["twent", "thirt", "fourt", "fift", "sixt", "sevent", "eight", "ninet"];
  var c = ["hundred", "thousand", "million", "billion", "trillion", "quadrillion", "quintillion"];
  var b = Math.floor(Math.log10(i));
  if(i<20) return s[i]; // Special case for sub-20
  if(b==1){ // Between 21 and 99
    if(i%10 === 0) return p[Math.floor(i/10)-2]+"ieth"; // On the tens, return p+"ieth"
    return p[Math.floor(i/10)-2] + "y-" + s[i%10]; // Otherwise, return hyphenated
  }
  if(b==2){ // Between 100 and 999
      var e = Math.floor(i/Math.pow(10,b)); // The first number
      return n[e-1]+"-"+c[0]+" "+nth(i-(e*Math.pow(10,b)));
  }
  // Greater than 1000 we break into groups of 10^3 followed by a multiplier
  var m = b%3 + 1; // Take the first m digits off
  var cm = Math.floor(b/3);
  var x = Math.floor(i/Math.pow(10,b-m+1));
  var numberToString = function(y){ // Converts a number less than 1000 to its string representation as a multiplier
    if(y<20) return n[y-1];
    if(y<100) return p[Math.floor(y/10)-2] + "y-" + n[y%10-1];
    return n[Math.floor(y/100)-1] + " " + c[0] + " " + numberToString(y-(Math.floor(y/100)*100));
  }
  return numberToString(x) + " " + c[cm] + " " + nth(i-(x*Math.pow(10, b-m+1)));
}

A bit late to the party, but what about this?

const numberToOrdinal = (n) => {
  const s = ['th', 'st', 'nd', 'rd'];
  const v = n % 100;
  return n + (s[(v - 20) % 10] || s[v] || s[0]);
};
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top