This section provides a tutorial example on how to use java.util.NumberFormat and java.util.DecimalFormat classes to convert or format numeric values into text strings in various locales.
The Number class, java.lang.Number, is an abstract class representing a numerical
value. It has several concrete subclasses: Byte, Short, Integer, Long, Float, and Double.
The NumberFormat class, java.text.NumberFormat, is an abstract class providing
a foundation for the derived subclasses to format numbers to strings and parse strings
back to numbers.
The DecimalFormat class, java.text.DecimalFormat, is a concrete subclass of
NumberFormat. An object of DecimalFormat contains a formatting pattern and locale
information. It can be used to format numbers into string representations; or
parsing strings for numbers.
There are some factory methods in NumberFormat class that return predefined default
DecimalFormat objects with commonly used formatting patterns. For example,
NumberFormat.getCurrencyInstance() returns a DecimalFormat object with a
pattern of "\u00A4#,##0.00;(\u00A4#,##0.00)" good for formatting a number into
a currency representation.
The following programs shows some examples of how to format numbers to strings: