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.
So I read up on the Javadoc documentation of icu4j and the
RuleBasedNumberFormatdoes not have FOURTH as a parsable string although it does support four. And instead of usingRuleBasedNumberFormat.ORDINALuses RuleBasedNumberFormat.SPELLOUT.When run:
This is what the code should look like and make sure to use .toLowerCase(Locale.ROOT) because parse does not work on upperCase strings.