我想取一个整数并得到它的序数,即:
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)
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)
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中以一种非常简单的方式做到这一点.您需要做的就是将依赖项添加到您app的build.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
一行:
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)