Condor.depaul.edu



A Brief Introduction to Formatting Output with printf

The printf function is very similar to the print and println functions with which you are already familiar. The difference is that printf allows you to include additional options called ‘format specifiers’ that give you control over how the output is formatted. For example, you can tell floating-point values (i.e. values with decimals) to display to a specific number of decimal places, to display in exponential notation format, to place negative numbers in parentheses, and many more.

The printf function can seem a little bit confusing at first, but after playing with it for a little while, you can get the hang of the basics. Beyond that, you can, if interested, learn to harness some more advanced techniques that give you a great deal of control over how information gets output. For purposes of this discussion, however, we will focus only on a few basic techniques for formatting numbers.

When you invoke the printf function, you may find that your argument list has many values. Ultimately, however, there are two key pieces of information that must be provided: a “format string” and “format specifiers”.

You can have multiple format specifiers. Each one is a placeholder for a value that is to be output. These specifiers are where you can provide little codes to indicate how you would like the information to be output. Format specifiers always begin with a % sign and end with the type of information you are outputting. If you are outputting a float, your format string would begin with % and end with an ‘f’. However, if you wanted to display the value in exponential notation (e.g 1.324e5), you would end with an ‘e’. If you were outputting a String, you would begin with a % and end with a ‘s’. If you were outputting an integer you would begin with a % and end with a d. These are but a few of the many different options.

The real strength, however, lies in the information you put between the % and the datatype character. For example, if you put a ‘.3’ between the % and the f, it would output the floating point value to 3 decimal places.

Let’s begin by taking the value of pi and outputting it to a specified number of decimal places. When you are outputting a double, you need to include a %f in your format string. Consider the following code:

double pi = 3.14159;

System.out.printf("%f", pi);

System.out.printf("%.2f", pi);

The first printf statement would output 3.14159. The second would only output 3.14.

You can also output in exponential notation:

double number = -343.98765;

System.out.printf("%e", number);

would output -3.439877e+02

You can include other flags such as:

- , separates thousands so that 1140032 would be output as 1,114,032

- ( places negative numbers in parentheses

int number = -1457293;

System.out.printf("%,(d", number);

Would output (-1,457,293).

Recall that ‘d’ says you are going to output an integer value.

You can include your escape sequences such as \n as part of your string. In fact, because printf does not include a newline character the way println does, you may find that you will use \n quite a bit.

Including a number between the % and the typecode allows you to specify the width of the field. So if you wanted every number to take up 10 columns regardless of the size of the number, you could put a 10 in between. All numbers are right-justified in that 10-column area, but there are (of course!) ways of telling Java to right-justify the output.

System.out.printf("%10d\n", 1);

System.out.printf("%10d\n", -1);

System.out.printf("%10d\n", 12);

System.out.printf("%10d\n", 123);

System.out.printf("%10d\n", 1234);

System.out.printf("%10f\n", 3.14159);

[pic]

You can group all kinds of commands together like so:

double num = -2234.33333;

System.out.printf("\t%(,.1f\n\n", num);

[pic]

• The \t creates a tab

• The % sign indicates the beginning of the format specifier containing:

o ( ( says to place negative numbers in parentheses

o , ( says to place thousands with commas eg: 1,224,333

o .1 ( says to print to one decimal place

We have only scratched the surface of Java’s text formatting capabilities. Another brief discussion can be found at .

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download