Java Cookbook
number format
Problem:
You want to format and parse integer values.
Solution:
The Java NumberFormat class makes formatting and parsing a number is localized manner easy and efficient. Simple specify the locale when retrieving the NumberFormat instance.
To format and parse a number in Hindi:
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
Problem:
You want to convert a spelled out number to a Number.
Solution:
Parsing a formatted number in Java takes a new twist when the number is fully spelled out. This can be easily accomplished thanks to the ICU4J library from IBM.
To parse a spelled out number:
//Get a RuleBasedNumberFormat appropriate for French spellout
RuleBasedNumberFormat rbnf = new RuleBasedNumberFormat(ULocale.ENGLISH,RuleBasedNumberFormat.SPELLOUT);
Problem:
You want to spell out a localized number like "thirty-six."
Solution:
ICU4J provides some number formatting capabilities that core Java does not. One of those features is the ability to spell out a numeric value.
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