回答
如果你用「1」 OK,「第二」,「第三」等,這裏有一些簡單的代碼,將正確處理任何整數
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
這個代碼適用於基數(一,二,三),不適用於序數(第一,第二,第三)。 –
沒錯,但它是微不足道的,我想 – zacheusz
在1行:
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];
}
另一種解決方案
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";
}
}
臨:不要求的陣列被初始化(以下垃圾)
缺點:不是一個襯墊...
使用優良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
我認爲這應該被選爲接受的答案,因爲它直接回答了問題。 – TheGT
最簡單的方式,在這裏,我們去:
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));
}
}
我沒有看到這個處理數字11,12和13? – Enoobong
波希米亞人答案是非常好的,但我建議改進錯誤處理。如果您提供了一個負整數,那麼使用原始版本的序號會拋出ArrayIndexOutOfBoundsException。我認爲我的版本更清晰。我希望junit也很有用,所以沒有必要直觀地檢查輸出。
public class FormattingUtils {
/**
* Return the ordinal of a cardinal number (positive integer) (as per common usage rather than set theory).
* {@link http://stackoverflow.com/questions/6810336/is-there-a-library-or-utility-in-java-to-convert-an-integer-to-its-ordinal}
*
* @param i
* @return
* @throws {@code IllegalArgumentException}
*/
public static String ordinal(int i) {
if (i < 0) {
throw new IllegalArgumentException("Only +ve integers (cardinals) have an ordinal but " + i + " was supplied");
}
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];
}
}
}
import org.junit.Test;
import static org.assertj.core.api.Assertions.assertThat;
public class WhenWeCallFormattingUtils_Ordinal {
@Test
public void theEdgeCasesAreCovered() {
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 };
String[] expectedResults = { "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" };
for (int i = 0; i < edgeCases.length; i++) {
assertThat(FormattingUtils.ordinal(edgeCases[i])).isEqualTo(expectedResults[i]);
}
}
@Test(expected = IllegalArgumentException.class)
public void supplyingANegativeNumberCausesAnIllegalArgumentException() {
FormattingUtils.ordinal(-1);
}
}
在斯卡拉的變化,
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
Java問題? – Enoobong
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";
}
}
我沒有看到這個處理數字11,12和13?使用和如果檢查它們,例如 - 「''if(number> = 11 && number <= 13)返回數字+」th「; }''' – Enoobong
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";
}
}
問題是要返回'First'爲1等等,就像'Eleventh'爲11等等。你的回答並沒有達到那個目的。 –
我有一個長期的,複雜的一個,但很容易理解的概念
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");
}
}
- 1. '整數+'「'是一種在Java中將整數轉換爲字符串的好方法嗎?
- 2. java - 無法將整數轉換爲int
- 3. 使用類Integer和多種方法將整數轉換爲Octal
- 4. 有一種將靜態HTML轉換爲獨立應用程序的方法嗎?
- 5. 你知道一種在Java中將CopyBook轉換爲XSD的方法嗎?
- 6. Java將浮點數轉換爲整數
- 7. 如何將整數方法轉換爲Java中的字符串方法?
- 8. 一種將數字轉換爲秒的方法
- 9. 有沒有一種有效的方法將Bundle轉換爲ContentValues?
- 10. 有沒有一種將LINQ-to-SQL數據轉換爲XML的有效方法?
- 11. 是否有一種簡單的方法可以將int轉換爲每個數字的整數數組?
- 12. 更簡潔的方法將整數除法轉換爲小數
- 13. 有沒有辦法將數字轉換爲java中的複數?
- 14. 有沒有一種方法來初始化整數Java中
- 15. 將數字轉換爲名稱有更快的方法嗎?
- 16. 爲什麼to_java方法將Java中的整數(會是什麼)轉換爲Long?
- 17. 無法將字符串轉換爲Android Studio/Java中的整數
- 18. Java:將字節轉換爲整數
- 19. 將Java對象轉換爲整數
- 20. 將Actionlistener類型轉換爲整數java
- 21. Java將字符串轉換爲整數
- 22. Java將對象轉換爲字節數組的有效方法
- 23. 有沒有一種方法可以將網頁轉換爲pdf?
- 24. 有沒有辦法將整數轉換爲向上計數的整數列表?
- 25. 將字符串轉換爲Python中整數的最佳方法
- 26. 這是一種使用jQuery將HTML轉換爲文本的有效方法嗎?
- 27. 轉換字節數組爲整數沒有循環的方法?
- 28. 是否有java方法來將字符串和轉換爲整數
- 29. 無法在Java中將Double轉換爲整數
- 30. 將Java方法轉換爲Scala方法
的http://計算器.com/questions/3911966/how-to-convert-number-to-words-in-java – Felix
它會很整潔也可以做前綴... st,nd,rd –
請注意,如果您希望您的程序在您的國家以外使用,則應該小心使用可以本地化的解決方案。 –