求序数词的数字表示

1的序数词,英文first,数字表示1st

2的序数词,英文second,数字表示2nd

现在给定任意一个正整数,求其序数词的数字形式?

这道题估计英语母语的国家会经常考,因为需要知道序数词的规律,而且有坑。乍一看,1,2,3特殊情况,原始数字模10求余数分别和1,2,3比较就可以了。

然而漏了一种情况,就是十位数为1的情况。11,12,13这三个数分别是eleventh, twelfth, thirteenth,对应的数字表示也以th结尾。因此完整的算法如下,

/**
* 两种情况
* 1、十位数是1,全部都以th结尾,比如,11,12,13
* 11 -- eleventh -- 11th
* 12 -- twelfth -- 12th
* 13 -- thirteenth -- 13th
* 511 -- five hundred and eleventh -- 511th
* 2、十位数不是1,若个位数为1,2,3,分别为1st,2nd,3rd,其他以th结尾,如
* 1 -- first -- 1st
* 21 -- twenty first -- 21st
*/
    public static String getOrdinal(int n){
        String suffix = null;
        // n%100得到的余数是0~99,这个数再除/10,即取得十位数的数字
        // 十位数若为1,则全部都以th结尾
        if(n%100/10==1){
            suffix = "th";
        }
        // 十位数若不为1,模10求余数,再与1,2,3比较
        else if(n%10==1){
            suffix = "st";
        }else if(n%10==2){
            suffix = "nd";
        }else if(n%10==3){
            suffix = "rd";
        }else {
            suffix = "th";
        }
        return n+suffix;
    }

 

参考:https://blog.csdn.net/iteye_3619/article/details/82306393  

 

posted @ 2019-02-20 16:43  leondryu  阅读(834)  评论(0编辑  收藏  举报