3

I am attempting to convert an ordinal string, say "FOURTH" to integer 4. How can I accomplish this with the International Components for Unicode library for Java, icu4j?

RuleBasedNumberFormat formatter = new RuleBasedNumberFormat(Locale.US, RuleBasedNumberFormat.ORDINAL);

return formatter.parse("FOURTH").intValue();

Seems like this is not working, as 0 is being returned. I'm expecting this to return 4.

Any help would be appreciated. Thank you.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
dojie
  • 33
  • 4
  • You can use PluralRules. https://unicode-org.github.io/icu-docs/apidoc/released/icu4j/com/ibm/icu/text/PluralRules.html –  Apr 05 '23 at 15:10
  • For me, your code does not return `0`. It throws `java.text.ParseException: Unparseable number: "FOURTH"`. – andrewJames Apr 05 '23 at 15:38
  • Interestingly, I found several existing questions that ask about integer to ordinal text, but this is the only Question I could find asking about the opposite, ordinal text to integer. – Basil Bourque Apr 05 '23 at 19:10

1 Answers1

3

So I read up on the Javadoc documentation of icu4j and the RuleBasedNumberFormat does not have FOURTH as a parsable string although it does support four. And instead of using RuleBasedNumberFormat.ORDINAL uses RuleBasedNumberFormat.SPELLOUT.

RuleBasedNumberFormat formatter = new RuleBasedNumberFormat( Locale.US , RuleBasedNumberFormat.SPELLOUT );
try
{
    int result = formatter.parse( "FOURTH".toLowerCase( Locale.ROOT ) ).intValue();
    System.out.println( "result = " + result );
}
catch ( ParseException e )
{
    … add code to handle exception.
}

When run:

4

This is what the code should look like and make sure to use .toLowerCase(Locale.ROOT) because parse does not work on upperCase strings.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
  • 1
    Nice! A word of warning: This works for `THIRTY-THIRD` - but for `THIRTYTHIRD`, it incorrectly returns `30` (with no errors). So, this relies on hyphenation (and spelling, of course). – andrewJames Apr 05 '23 at 18:02