Java Reference
In-Depth Information
any '#' optional digits follow the zero digits '0' after the decimal point in the pattern.
Use "#0.0##" ; do not use "0#.0##" or "#0.##0" .
For example, consider the following code:
DecimalFormat formattingObject = new DecimalFormat("#0.0##");
System.out.println(formattingObject.format(12.3456789));
System.out.println(formattingObject.format(1.23456789));
This produces the output
12.346
1.235
percentages
The character '%' placed at the end of a pattern indicates that the number is to be
expressed as a percentage. The '%' causes the number to be multiplied by 100 and
appends a percent sign, '%' . Examples of this and other formatting patterns are given
in Display 2.5.
E-notation is specified by including an 'E' in the pattern string. For example, the
pattern "00.###E0" approximates specifying two digits before the decimal point, three
or fewer digits after the decimal point, and at least one digit after the 'E' , as in
12.346E1 . As you can see by the examples of E-notation in Display 2.5, the exact
details of which E-notation string is produced can be a bit more involved than our
explanation so far. Here are a couple more details:
The number of digits indicated after the 'E' is the minimum number of digits used
for the exponent. As many more digits as are needed will be used.
The mantissa is the decimal number before the 'E' . The minimum number of sig-
nificant digits in the mantissa (that is, the sum of the number of digits before and
after the decimal point) is the minimum of the number of digits indicated before
the decimal point plus the maximum of the number of digits indicated after the
decimal point. For example, 12345 formatted with "##0.##E0" is "12.3E3" .
To get a feel for how E-notation patterns work, it would pay to play with a few
cases. In any event, do not count on a very precisely specified number of significant
digits.
E-notation
mantissa
DecimalFormat Class
Objects of the class DecimalFormat are used to produce strings of a specified format from
numbers. As such, these objects can be used to format numeric output. The object is asso-
ciated with a pattern when it is created using new . The object can then be used with the
method format to create strings that satisfy the format. See Display 2.5 for examples of
the DecimalFormat class in use.
Search WWH ::




Custom Search