将序数转换为字符串

Convert ordinal number to string

我正在使用以下代码检索序号:

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.

中的这些序数中获取 first、second、third 等字符串

你可以用这个class我写的。只需 download 文件(包含许可证 header)到您的项目,更新包声明,您就可以像这样使用它了。

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

输出

一万二千和forty-eight
一万二千 forty-eighth
十二万、五百
十一万二千,百分之五

toWords 的第二个参数,如果指定,则确定是输出序数 (true) 还是常规数字 (false)。你可以看看它,看看它是如何工作的。