header

Format Method

In a computer, a number and its string representation are two very different things. Numeric storage formats are used for performing arithmetic operations. However, before a number can be displayed it must be converted to a corresponding string representation (a numeral).

We have already used two different ways to convert numeric values to their corresponding string representations. The first is the String concatenation operator. For example, if length represents a numeric variable, the expression, "Length: " + length represents a string value formed by concatenating the literal string, "Length: ", with the string representation of the length.

The second way that we have performed this conversion is by using the print or printf methods (defined in the PrintStream class). For example, the statement println(length) prints the text representation of the value of length.

Formatted Output Using the "format" Method of the PrintStream Class

Neither of these two approaches give you any control over how numeric values will be displayed. Consider the following code fragment:

double dbl = 101.0/3.0;
System.out.println("Value of dbl is: " + dbl);

The text displayed by the output statement will be:

Value of dbl is: 33.666666666666664

Suppose, however, you want to display this value rounded to 3 decimal places instead? One way to generate formatted output is to use the "format" method of the PrintStream class. The first argument of the "format" method is a format string which contains normal text with imbedded format codes (each format code begins with the % character). The remaining arguments represent numeric values and are usually variable identifiers (the format string should include a format code for each variable).

printf(formatString, var1, var2, ...) 

The code fragment below will display the value of dbl to 3 decimal places.

double dbl = 101.0/3.0;
System.out.format("Value of dbl is: %.3f%n", dbl); 

The text displayed by the output statement will be:

Value of dbl is: 33.667 

While the value of dbl is displayed to 3 decimal places, the actual value of dbl has not changed. All we are doing is controlling how the value of dbl is to be displayed. Note also, that the value of the numeric argument replaces the format code in the format string.

New Line Format Code: %n

Each format code begins with a percent sign (%). The format code %n inserts a new line character into the print stream. In our example above, the %n code was the very last item in the format string and indicates that subsequent output will begin on the next line down. The %n format code is special in that it does not correspond to any of the variables in the argument list following the format string.

Floating Point Format Code: %[-][,][width][.precision]f 

The final letter 'f' in this format code indicates that the value of the corresponding numeric argument is to be formatted as a floating point value.

The hyphen specifies that the output will be left-justified. It is optional and if omitted, the output will be right-justified (the default).

The comma specifier indicates that the output will contain grouping characters in the integer part of the string representation. In the U.S., the grouping character is a comma (e.g., 3,453,745). The square brackets indicate that this specifier is optional. If it is omitted, the output will not contain grouping characters (e.g., 3454745).

The width is a non-negative integer that indicates the minimum number of characters that will be used to display the floating-point value. This is known as the field width. The width is optional. If it is omitted, the value will be formatted using only as many characters as necessary. If the specified field width is too small, it will be ignored.

The precision is a non-negative integer that specifies the number of decimal places (the number of digits to the right of the decimal point). The displayed value will be rounded to the specified number of decimal places though the actual value of the corresponding variable will be left unchanged. The precision is optional. If it is omitted, the number of decimal places will be determined by the default setting.

Here are some examples of how to use the floating point format code.

format("%f", Math.PI)     ==> 3.141593   The default floating-point format
format("%10f", Math.PI)   ==>   3.141593 Default format with a field width of 10
format("%.3f", Math.PI)   ==> 3.142      Rounded to 3 places
format("%10.3f", Math.PI) ==>      3.142 Field width of 10 to 3 places
format("%2.3f", Math.PI)  ==> 3.142      Field width of 2 is ignored because it is too small

String Format Code %[-][width]s

The final letter 's' in this format code indicates that the argument is a string value. This format code is used primarily to control field width and alignment of text in tabular output (output in the form of a table).

The hyphen specifies that the output will be left-justified. It is optional and if omitted, the output will be right-justified (the default).

The width specifier indicates the field width which has the same properties here as it does in the floating point format described above. If the text is too short to fill up the specified width, it will be padded with extra spaces. These spaces are added to the end of the string if the string is left-justified and added to the beginning of the string if the text is right-justified.

Decimal Integer Format Code %[-][,][width]d

The hyphen specifies that the output will be left-justified. It is optional and if omitted, the output will be right-justified (the default).

The comma specifier indicates that the output will contain grouping characters in the string representation. In the U.S., the grouping character is a comma (e.g., 3,453,745). This specifier is optional. If it is omitted, the output will not contain grouping characters (e.g., 3454745).

The width is a non-negative integer that indicates the minimum number of characters that will be used to display the integer value. This is known as the field width. The width is optional. If it is omitted, the value will be formatted using only as many characters as necessary. If the specified field width is too small, it will be ignored.

Formatted Output Using the "format" Method of the String Class

The String class also contains a format method. In the String class, the format method is a class method and returns a String value. For example,

String.format("Value of dbl is: %.3f", dbl)

returns a string value consisting of the literal string "Value of dbl is: " concatenated with the string representation of the variable dbl rounded to three decimal places.