Second Edition

Java Software Solutions

Lewis and Loftus

 

Web Bonus

Patterns for the DecimalFormat Class


This web bonus is referenced in the text on page 90.

You may also download this web bonus...
As a MS Word document: decimalFormat.doc
Or as a PDF document: decimalFormat.pdf

As discussed in Chapter 2 of the textbook, the DecimalFormat class is a valuable part of the Java API for formatting numbers specified by a pattern that you define. This web bonus expands on the book's discussion about the patterns that you can create.

Essentially, a DecimalFormat object represents a set of rules to convert a number to a String, using a pattern supplied when instantiating the object. That pattern determines how many digits are printed, and whether the number should be rounded or zero-extended to extra places.

The DecimalFormat class recognizes the following symbols in a formatting pattern:

Symbol

Meaning

0 Digit
# Digit, zero shows as absent
. Decimal separator or monetary decimal separator
- Minus sign
, Grouping separator
E Separates mantissa and exponent in scientific notation. Need not be quoted in prefix or suffix.
; Separates positive and negative subpatterns
% Multiply by 100 and show as percentage
¤

(\u00A4)

Currency sign, replaced by currency symbol. If doubled, replaced by international currency symbol. If present in a pattern, the monetary decimal separator is used instead of the decimal separator.
' Used to quote special characters in a prefix or suffix, for example, "'#'#" formats 123 to "#123". To create a single quote itself, use two in a row: "# o''clock".


The following code fragment shows how DecimalFormat might be used:

     double d = 28.5;
     DecimalFormat df = new DecimalFormat ("####.##");
     String s = df.format (d);
     System.out.println (s);

The following table shows several DecimalFormat patterns and how each pattern would represent the number 123.456.

Pattern

Result

"####.##" 123.46
"######.######" 123.456
"000000.000000" 000123.456000
"######.000000" 123.456000
"000000.######" 000123.456
" ###.###" 123.456
"$#####.######" $123.456
"$$####.######" $$123.456



Sometimes it would be nice to format a vertical list of floating point numbers such that all of the decimal points are aligned. For example:

      123.456
     7892.8
        9.75

In the current state of the API, this type of formatting is somewhat complicated. Each number is formatted separately, so we have to know how one number relates to the others. The DecimalFormat class can report how far a format operation moved the start of the number within the field allowed for it. You could then pad each number appropriately with leading spaces. Future revisions to the class libraries may make this operation easier to perform.

Beware that careless use of DecimalFormat can introduce false precision, when used to fill zeros to the right of the decimal. For example, the number 27.5 formatted with "###.000" produces 27.500, which could be misleading depending on the situation.

 


Send questions and comments to john.lewis@villanova.edu