Is there a way in Java to convert an integer to its ordinal name?

后端 未结 12 1330
生来不讨喜
生来不讨喜 2020-11-27 15:12

I want to take an integer and get its ordinal, i.e.:

1 -> \"First\"
2 -> \"Second\"
3 -> \"Third\"
...
12条回答
  •  醉话见心
    2020-11-27 15:38

    private static String getOrdinalIndicator(int number) {
            int mod = number;
            if (number > 13) {
                mod = number % 10;
            }
            switch (mod) {
            case 1:
                return "st";
            case 2:
                return "nd";
            case 3:
                return "rd";
            default:
                return "th";
            }
        }
    

提交回复
热议问题