Formatting Console Output
Paul Ngugi
Posted on May 6, 2024
You can use the System.out.printf method to display formatted output on the console.
Often, it is desirable to display numbers in a certain format. For example, the following code computes interest, given the amount and the annual interest rate.
double amount = 12618.98;
double interestRate = 0.0013;
double interest = amount * interestRate;
System.out.println("Interest is $" + interest);
Interest is $16.404674
Because the interest amount is currency, it is desirable to display only two digits after the decimal point. To do this, you can write the code as follows:
double amount = 12618.98;
double interestRate = 0.0013;
double interest = amount * interestRate;
System.out.println("Interest is $"
+ (int)(interest * 100) / 100.0);
Interest is $16.4
However, the format is still not correct. There should be two digits after the decimal point: 16.40 rather than 16.4. You can fix it by using the printf method, like this:
double amount = 12618.98;
double interestRate = 0.0013;
double interest = amount * interestRate;
System.out.printf("Interest is $%4.2f",
interest);
Interest is $16.40
The syntax to invoke this method is
System.out.printf(format, item1, item2, ..., itemk)
where format is a string that may consist of substrings and format specifiers.
A format specifier specifies how an item should be displayed. An item may be a numeric value, a character, a Boolean value, or a string. A simple format specifier consists of a percent sign (%) followed by a conversion code. The table below lists some frequently used simple format specifiers.
Here is an example:
int count = 5;
double amount = 45.56;
System.out.printf("count is %d and amount is %f", count, amount);
display
count is 5 and amount is 45.560000
Items must match the format specifiers in order, in number, and in exact type. For example, the format specifier for count is %d and for amount is %f. By default, a floating-point value is displayed with six digits after the decimal point. You can specify the width and precision in a format specifier, as shown in the examples in Table below:
If an item requires more spaces than the specified width, the width is automatically increased. For example, the following code
System.out.printf("%3d#%2s#%4.2f\n", 1234, "Java", 51.6653);
displays
1234#Java#51.67
The specified width for int item 1234 is 3, which is smaller than its actual size 4. The width is automatically increased to 4. The specified width for string item Java is 2, which is smaller than its actual size 4. The width is automatically increased to 4. The specified width for double item 51.6653 is 4, but it needs width 5 to display 51.67, so the width is automatically increased to 5.
By default, the output is right justified. You can put the minus sign (-) in the format specifier to specify that the item is left justified in the output within the specified field. For example, the following statements
System.out.printf("%8d%8s%8.1f\n", 1234, "Java", 5.63);
System.out.printf("%-8d%-8s%-8.1f \n", 1234, "Java", 5.63);
display
The items must match the format specifiers in exact type. The item for the format specifier %f or %e must be a floating-point type value such as 40.0, not 40. Thus, an int variable cannot match %f or %e. The % sign denotes a format specifier. To output a literal % in the format string, use %%.
The statement in lines 7 displays the column names of the table. The column names are strings. Each string is displayed using the specifier %-10s, which left-justifies the string. The statement in lines 12 displays the degrees as an integer and four float values. The integer is displayed using the specifier %-10d and each float is displayed using the specifier** %-10.4f**, which specifies four digits after the decimal point.
Posted on May 6, 2024
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.