記錄一個覺得不錯用的 javascript 方法,也學習了英文關于順序縮寫的規則,從 stackoverflow 的文章 C# 代碼改寫而來,
function asOrdinal(num) {
if (isNaN(num) || num <= 0) {
return num + '';
}
switch (num % 100) {
case 11:
case 12:
case 13:
return num + 'th';
}
switch (num % 10) {
case 1:
return num + 'st';
case 2:
return num + 'nd';
case 3:
return num + 'rd';
}
return num + 'th';
}
其中關于 11,12,13 的特殊處理值得注意,很容易忽略掉!
stackoverflow 原文位址
http://stackoverflow.com/questions/20156/is-there-an-easy-way-to-create-ordinals-in-c