Java Cookbook
Format Ordinal Numbers
Problem:
You want to format an ordinal number like "1st" or "2nd".
Solution:
Java programmers can handle ordinal number formatting by leveraging ICU4J's RuleBasedNumberFormat class.
An ordinal number is a number like "1st" "2nd" etc.
To format an ordinal number:
//Get a RuleBasedNumberFormat appropriate for English ordinal format
RuleBasedNumberFormat rbnf = new RuleBasedNumberFormat(ULocale.ENGLISH,RuleBasedNumberFormat.ORDINAL);
//Format the number
System.out.println(rbnf.format(21));
The output:
21st
If you are testing any of these recipes in Eclipse and the characters are not displaying correctly in your console visit http://i18ncookbook.com/eclipse_settings.
This site is ad supported. I hope you find something among our sponsors worth clicking. ;)
i18n search
Custom Search