Js As Ordinal

记录一个觉得不错用的 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

原文地址:https://www.cnblogs.com/rulee/p/5176445.html