Ordinals in words javascript
Asked Answered
P

4

6

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"
Paean answered 14/4, 2013 at 10:22 Comment(4)
If you're asking whether a function like that comes built-in, then no, you have to make it yourself.Odaniel
Nope I just want elegant way, not bunch of code :)Paean
Then try making a dirty version first then come back with your code and we'll help make that more elegant.Illfated
well, give me minut :DPaean
O
12

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.

Odaniel answered 14/4, 2013 at 10:39 Comment(0)
K
7

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)));
}
Kindness answered 16/6, 2017 at 13:59 Comment(1)
this solution gives results of one-hundred zeroth for 100 and two-hundred zeroth for 200 and one thousand zeroth for 1000, and so on.Infantryman
C
5

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]);
};
Cockneyfy answered 26/8, 2021 at 19:52 Comment(1)
the requirement is for conversion into the full English wordInfantryman
S
2

Another option is to make use of the built-in js Intl.PluralRules

See an example

const pr = new Intl.PluralRules("en-US", { type: "ordinal" });

const suffixes = new Map([
  ["one", "st"],
  ["two", "nd"],
  ["few", "rd"],
  ["other", "th"],
]);
const formatOrdinals = (n) => {
  const rule = pr.select(n);
  const suffix = suffixes.get(rule);
  return `${n}${suffix}`;
};


console.log(formatOrdinals(1)); // '1st'
console.log(formatOrdinals(2)); // '2nd'
console.log(formatOrdinals(3)); // '3rd'
console.log(formatOrdinals(4)); // '4th'
console.log(formatOrdinals(41)); // '41st'

more info on the MDN website

Sclerodermatous answered 26/11, 2023 at 12:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.