0

I am retrieving ordinal numbers using below code:

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

    }
}

Output : 1st, 2nd, 3rd...

How can I get string like first, second, third from these ordinal numbers in java, Android.

J A
  • 15
  • 7
R G
  • 461
  • 3
  • 15

1 Answers1

3

You can use this class I wrote. Just download the file (license header included) to your project, update the package declaration, and you can use it like this.

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

which outputs

twelve thousand and forty-eight
twelve thousand and forty-eighth
one hundred and two thousand, five hundred
one hundred and two thousand, five hundredth

The second argument to toWords, if specified, determines whether to output ordinals (true) or regular numerals (false). You can take a look at it to see how it works.

Leo Aso
  • 11,898
  • 3
  • 25
  • 46
  • This worked for me.. Thank you. – R G Dec 16 '17 at 06:15
  • That is an extremely poor solution when it comes to Android, as it lacks internationalization. – M. Prokhorov Dec 18 '17 at 11:17
  • @M.Prokhorov quite true. The OP would probably have been much better off using a full featured library. But I had the code anyway, so I thought I'd just share it. Plus, I missed the `android` tag when I first answered. – Leo Aso Dec 18 '17 at 16:03