有没有优雅的方法如何在js / coffee中以单词格式获得序数? 像这样:
ordinalInWord(1) # => "first"
ordinalInWord(2) # => "second"
ordinalInWord(5) # => "fifth"
答案 0 :(得分:8)
我担心这些序数不够规则,以免输入它们。
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 ];
}
将其扩展到99年以后应该不难。
答案 1 :(得分:4)
这是一个最高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)));
}