我想取一个整数并得到它的序数,即:
1 -> "First"
2 -> "Second"
3 -> "Third"
...
答案 0 :(得分:110)
如果您对“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];
}
}
以下是边缘情况的一些测试:
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));
}
}
输出:
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
答案 1 :(得分:22)
使用优秀的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"));
}
例如生成
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
答案 2 :(得分: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";
}
}
Pro:不需要初始化数组(减少垃圾)
骗局:不是单行......
答案 3 :(得分:9)
我已经在Android中以非常简单的方式想出了如何做到这一点。您需要做的就是将依赖项添加到app
的{{1}}文件中:
build.gradle
接下来,创建此方法:
<强>科特林:强>
implementation "com.ibm.icu:icu4j:53.1"
<强>爪哇:强>
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)
}
}
然后,您可以像这样使用它:
<强>科特林:强>
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);
}
}
<强>爪哇:强>
val ordinal = 2.getOrdinal()
工作原理
从Android N(API 24)开始,Android使用String ordinal = getNumberOrdinal(2)
而非常规icu.text
(more info here),其中已包含ordinal numbers的国际化实施。所以解决方案显然很简单 - 将java.text
库添加到项目中并在icu4j
答案 4 :(得分: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];
}
答案 5 :(得分:5)
答案 6 :(得分:2)
波希米亚人的回答非常好,但我建议改进错误处理。如果提供负整数,则使用原始版本的序号,将抛出ArrayIndexOutOfBoundsException。我认为下面的版本更清晰。我希望junit也很有用,所以没有必要直观地检查输出。
uid
答案 7 :(得分:0)
在Scala中进行更改,
List(1, 2, 3, 4, 5, 10, 11, 12, 13, 14 , 19, 20, 23, 33, 100, 113, 123, 101, 1001, 1011, 1013, 10011) map {
case a if (a % 10) == 1 && (a % 100) != 11 => a + "-st"
case b if (b % 10) == 2 && (b % 100) != 12 => b + "-nd"
case c if (c % 10) == 3 && (c % 100) != 13 => c + "-rd"
case e => e + "-th"
} foreach println
答案 8 :(得分:0)
public static String getOrdinalFor(int value) {
int tenRemainder = value % 10;
switch (tenRemainder) {
case 1:
return value+"st";
case 2:
return value+"nd";
case 3:
return value+"rd";
default:
return value+"th";
}
}
答案 9 :(得分:0)
我有一个漫长而复杂的但很容易理解的概念
private static void convertMe() {
Scanner in = new Scanner(System.in);
try {
System.out.println("input a number to convert: ");
int n = in.nextInt();
String s = String.valueOf(n);
//System.out.println(s);
int len = s.length() - 1;
if (len == 0){
char lastChar = s.charAt(len);
if (lastChar == '1'){
System.out.println(s + "st");
} else if (lastChar == '2') {
System.out.println(s + "nd");
} else if (lastChar == '3') {
System.out.println(s + "rd");
} else {
System.out.println(s + "th");
}
} else if (len > 0){
char lastChar = s.charAt(len);
char preLastChar = s.charAt(len - 1);
if (lastChar == '1' && preLastChar != '1'){ //not ...11
System.out.println(s + "st");
} else if (lastChar == '2' && preLastChar != '1'){ //not ...12
System.out.println(s + "nd");
} else if (lastChar == '3' && preLastChar != '1'){ //not ...13
System.out.println(s + "rd");
} else {
System.out.println(s + "th");
}
}
} catch(InputMismatchException exception){
System.out.println("invalid input");
}
}
答案 10 :(得分:0)
static String getOrdinal(int input) {
if(input<=0) {
throw new IllegalArgumentException("Number must be > 0");
}
int lastDigit = input % 10;
int lastTwoDigit = input % 100;
if(lastTwoDigit >= 10 && lastTwoDigit <= 20) {
return input+"th";
}
switch (lastDigit) {
case 1:
return input+"st";
case 2:
return input+"nd";
case 3:
return input+"rd";
default:
return input+"th";
}
}
答案 11 :(得分:-1)
最简单的方式,我们走了:
import java.util.*;
public class Numbers
{
public final static String print(int num)
{
num = num%10;
String str = "";
switch(num)
{
case 1:
str = "st";
break;
case 2:
str = "nd";
break;
case 3:
str = "rd";
break;
default:
str = "th";
}
return str;
}
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number: ");
int number = sc.nextInt();
System.out.print(number + print(number));
}
}
答案 12 :(得分:-1)
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";
}
}