Convert ordinal number to string [duplicate]

为君一笑 提交于 2019-12-13 03:51:02

问题


I am retrieving ordinal numbers using below code:

public static String ordinal(int i) {
    String[] sufixes = new String[] { "th", "st", "nd", "rd", "th", "th", "th", "th", "th", "th" };
    switch (i % 100) {
    case 11:
    case 12:
    case 13:
        return i + "th";
    default:
        return i + sufixes[i % 10];

    }
}

Output : 1st, 2nd, 3rd...

How can I get string like first, second, third from these ordinal numbers in java, Android.


回答1:


You can use this class I wrote. Just download the file (license header included) to your project, update the package declaration, and you can use it like this.

Numerals ns = new Numerals();
System.out.println(ns.toWords(12048));
System.out.println(ns.toWords(12048, true));
System.out.println(ns.toWords(102500, false));
System.out.println(ns.toWords(102500, true));

which outputs

twelve thousand and forty-eight
twelve thousand and forty-eighth
one hundred and two thousand, five hundred
one hundred and two thousand, five hundredth

The second argument to toWords, if specified, determines whether to output ordinals (true) or regular numerals (false). You can take a look at it to see how it works.



来源:https://stackoverflow.com/questions/47832937/convert-ordinal-number-to-string

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!