Java中有没有办法将整数转换为序数?

lak*_*com 50 java

我想取一个整数并得到它的序数,即:

1 -> "First"
2 -> "Second"
3 -> "Third"
...
Run Code Online (Sandbox Code Playgroud)

Boh*_*ian 118

如果您对"1st","2nd","3rd"等没问题,这里有一些能够正确处理任何整数的简单代码:

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

    }
}
Run Code Online (Sandbox Code Playgroud)

以下是边缘情况的一些测试:

public static void main(String[] args) {
    int[] edgeCases = { 0, 1, 2, 3, 4, 5, 10, 11, 12, 13, 14, 20, 21, 22, 23, 24, 100, 101, 102, 103, 104, 111, 112, 113, 114 };
    for (int edgeCase : edgeCases) {
        System.out.println(ordinal(edgeCase));
    }
}
Run Code Online (Sandbox Code Playgroud)

输出:

0th
1st
2nd
3rd
4th
5th
10th
11th
12th
13th
14th
20th
21st
22nd
23rd
24th
100th
101st
102nd
103rd
104th
111th
112th
113th
114th
Run Code Online (Sandbox Code Playgroud)

  • @desgraci不确定是否会被接受,但是我认为它是最受好评的答案,因为它通常对其他人有用。 (4认同)
  • 为什么这是公认的答案?OP用文字问序数,答案不是那样做。可能他拒绝了这个要求,但仍然是他所要求的答案。 (2认同)

JFK*_*JFK 27

使用优秀的ICU4J(还有一个优秀的C版本)你也可以做到这一点,并将普通话作为简单的单词;

RuleBasedNumberFormat nf = new RuleBasedNumberFormat(Locale.UK, RuleBasedNumberFormat.SPELLOUT);
for(int i = 0; i <= 30; i++)
{
    System.out.println(i + " -> "+nf.format(i, "%spellout-ordinal"));
}
Run Code Online (Sandbox Code Playgroud)

例如生产

0 -> zeroth
1 -> first
2 -> second
3 -> third
4 -> fourth
5 -> fifth
6 -> sixth
7 -> seventh
8 -> eighth
9 -> ninth
10 -> tenth
11 -> eleventh
12 -> twelfth
13 -> thirteenth
14 -> fourteenth
15 -> fifteenth
16 -> sixteenth
17 -> seventeenth
18 -> eighteenth
19 -> nineteenth
20 -> twentieth
21 -> twenty-first
22 -> twenty-second
23 -> twenty-third
24 -> twenty-fourth
25 -> twenty-fifth
26 -> twenty-sixth
27 -> twenty-seventh
28 -> twenty-eighth
29 -> twenty-ninth
30 -> thirtieth
Run Code Online (Sandbox Code Playgroud)

  • 我认为这应该被选为接受的答案,因为它直接回答了这个问题. (4认同)

kla*_*ann 18

另一种方法

public static String ordinal(int i) {
    int mod100 = i % 100;
    int mod10 = i % 10;
    if(mod10 == 1 && mod100 != 11) {
        return i + "st";
    } else if(mod10 == 2 && mod100 != 12) {
        return i + "nd";
    } else if(mod10 == 3 && mod100 != 13) {
        return i + "rd";
    } else {
        return i + "th";
    }
}
Run Code Online (Sandbox Code Playgroud)

Pro:不需要初始化数组(减少垃圾)
Con:不是单行...


Ale*_*rol 11

我已经想出了如何在Android中以一种非常简单的方式做到这一点.您需要做的就是将依赖项添加到您appbuild.gradle文件中:

implementation "com.ibm.icu:icu4j:53.1"
Run Code Online (Sandbox Code Playgroud)

接下来,创建此方法:

科特林:

fun Number?.getOrdinal(): String? {
    if (this == null) {
        return null
    }

    val format = "{0,ordinal}"

    return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
        android.icu.text.MessageFormat.format(format, this)
    } else {
        com.ibm.icu.text.MessageFormat.format(format, this)
    }
}
Run Code Online (Sandbox Code Playgroud)

Java的:

public static String getNumberOrdinal(Number number) {
        if (number == null) {
            return null;
        }

        String format = "{0,ordinal}";

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            return android.icu.text.MessageFormat.format(format, number);
        } else {
            return com.ibm.icu.text.MessageFormat.format(format, number);
        }
    }
Run Code Online (Sandbox Code Playgroud)

然后,您可以像这样简单地使用它:

科特林:

val ordinal = 2.getOrdinal()
Run Code Online (Sandbox Code Playgroud)

Java的:

String ordinal = getNumberOrdinal(2)
Run Code Online (Sandbox Code Playgroud)

这个怎么运作

从Android N(API 24)开始Android使用icu.text而不是常规java.text(此处更多信息),其中已包含序数的国际化实现.所以解决方案显然很简单 - 将icu4j库添加到项目中并在下面的版本上使用它Nougat

  • 重要提示:这不适用于较新版本的 ICU4J(至少 63.1);它将抛出 API 级别 &lt;24,因为 java.util.Locale.Category 不受支持。 (3认同)
  • 对于这样简单的功能添加了太多的 apk 大小,+9mb。 (3认同)

nvr*_*dow 6

一行:

public static String ordinal(int i) {
    return i % 100 == 11 || i % 100 == 12 || i % 100 == 13 ? i + "th" : i + new String[]{"th", "st", "nd", "rd", "th", "th", "th", "th", "th", "th"}[i % 10];
}
Run Code Online (Sandbox Code Playgroud)


zac*_*usz 5

这是转换的代码,这是另一个.看看这个答案如何在java中将数字转换为单词

  • 该代码用于基数(一,二,三),但不适用于序数(第一,第二,第三). (3认同)