5

有没有什么优雅的方法可以在 js/coffee 中获取 word 格式的序数?像这样的东西:

ordinalInWord(1) # => "first"
ordinalInWord(2) # => "second"
ordinalInWord(5) # => "fifth"
4

3 回答 3

11

恐怕序数不够规则,无法避免将它们中的每一个都打出来。

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

    return ordinals[ cardinal ];
}

如果您需要该功能在 20 岁以后工作,您可以利用出现的模式:

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 ];
}

演示:http: //jsfiddle.net/AQCqK/

将其扩展到 99 之后应该不难。

于 2013-04-14T10:39:40.440 回答
6

这是最高 64 位的解决方案。我无法想象你为什么需要它,但为什么从来都不是问题。

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)));
}
于 2017-06-16T13:59:24.997 回答
1

派对有点晚了,但是这个呢?

const numberToOrdinal = (n) => {
  const s = ['th', 'st', 'nd', 'rd'];
  const v = n % 100;
  return n + (s[(v - 20) % 10] || s[v] || s[0]);
};
于 2021-08-26T19:52:08.087 回答