Java Reference
In-Depth Information
2. Enter lines 83 through 104 as shown in Figure 4-27 on page 4.39.
TextPad displays the getComm() method in the coding window
(Figure 4-29).
switch
statement
getComm()
method
case
statements
FIGURE 4-29
While the getComm() method is complete, it merely returns a commission
rate to the main() method. Additional code must be added to the program to
display output showing the calculated commission.
Formatting Numeric Output
As you have learned, both the System.out.println() and the JOptionPane
methods can display numeric output, but it is not formatted in any special way.
Formatted numeric output includes features such as dollar signs, commas, dec-
imal points, leading zeroes, and other formatting symbols, applied automatically
to a displayed value.
Java has a special class of methods to handle numeric formatting. For
example, the DecimalFormat class formats decimal numbers. It has a variety of
features designed to make it possible to parse and format numbers in any locale,
including support for Western, Arabic, and Indic digits. It also supports different
kinds of numbers, including integers (123), fixed-point numbers (123.4), scien-
tific notation (1.23E4), percentages (12%), and currency amounts ($123).
Programmers use the DecimalFormat class to format decimal numbers into
Strings for output. This class allows you to control the display of leading and
trailing zeroes, prefixes and suffixes, grouping (thousands) separators, and the
decimal separator.
A constructor is used to create a named String using the DecimalFormat()
method . The argument for the DecimalFormat() method is a String called a
pattern , which determines how the formatted number should be displayed.
Table 4-10 on the next page displays some examples of patterns and the
resulting output.
 
Search WWH ::




Custom Search