Java 将序号转换为字符串

Java 将序号转换为字符串,java,android,ordinals,Java,Android,Ordinals,我正在使用以下代码检索序号: 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"; defaul

我正在使用以下代码检索序号:

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];

    }
}
输出: 第一,第二,第三


在java、Android中,我如何从这些序号中获得像first、second、third这样的字符串。

您可以使用我编写的这个类。只需将文件(包括许可证标题)添加到项目中,更新包声明,就可以这样使用它

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));
哪个输出

壹万贰仟肆佰捌元
一万二千四百四十八
壹万贰仟伍佰
十万二千五百分之一


如果指定了
toWords
,则第二个参数决定是输出序数(true)还是常规数字(false)。您可以看看它是如何工作的。

您可以使用我编写的这个类。只需将文件(包括许可证标题)添加到项目中,更新包声明,就可以这样使用它

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));
哪个输出

壹万贰仟肆佰捌元
一万二千四百四十八
壹万贰仟伍佰
十万二千五百分之一


如果指定了
toWords
,则第二个参数决定是输出序数(true)还是常规数字(false)。您可以看看它是如何工作的。

我已经可以使用开关(I%100)获得如上所述的序号。序号字符串::第0个序号字符串::第1个序号字符串::第2个序号字符串::第2个序号字符串::第2个序号。您是否可以查找序号,如第1、第2、第3个序号?不。我已经得到了。我想要像第一,第二,第三这样的字符串。你看起来像这个1->“第一”2->“第二”3->“第三”是的。如果可能的话,可以使用像1,2,3到1,2,3这样的数字。。也可以。我已经可以使用开关(i%100)获得上面的序号了。序号字符串::第0个序号字符串::第1个序号字符串::第2个序号字符串::第2个序号。你敢找像第1、第2、第3个序号一样的序号吗?不。我已经得到了。我想要像第一,第二,第三这样的字符串。你看起来像这个1->“第一”2->“第二”3->“第三”是的。如果可能的话,可以使用像1,2,3到1,2,3这样的数字。。也会很好这对我来说很有效。。谢谢。对于Android来说,这是一个非常糟糕的解决方案,因为它缺乏国际化。@M.Prokhorov非常正确。如果使用功能齐全的库,OP可能会更好。但是我有代码,所以我想我应该和大家分享。另外,当我第一次回答时,我错过了
android
标签。这对我很有用。。谢谢。对于Android来说,这是一个非常糟糕的解决方案,因为它缺乏国际化。@M.Prokhorov非常正确。如果使用功能齐全的库,OP可能会更好。但是我有代码,所以我想我应该和大家分享。另外,当我第一次回答时,我错过了
android
标签。