gpt4 book ai didi

java - 将序数转换为字符串

转载 作者:行者123 更新时间:2023-12-02 02:25:37 25 4
gpt4 key购买 nike

我使用以下代码检索序数:

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 中的这些序数中获取像第一个、第二个、第三个这样的字符串。

最佳答案

你可以使用我写的这个类。只是download将文件(包括许可证头)添加到您的项目中,更新包声明,然后您就可以像这样使用它。

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)。您可以看一下它是如何工作的。

关于java - 将序数转换为字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47832937/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com