next up previous contents
Next: Input from a file Up: 5 More advanced input Previous: 5 More advanced input   Contents

Formatting output

Sometimes you will want to control the way in which the output of a program is written. For example, for some numbers not all the significant figures will be required, or in fact they may get in the way if you wish to write more than one result on each line. It can be helpful then to able to format output.

There are three steps to printing formatted output. The first is to create the format9that you wish to use. This is done using

  DecimalFormat myFormat = new DecimalFormat("###.000");

where myFormat is what you decide to call the format. The format is defined using the string "###.000" where # denotes a digit and 0 denotes a digit or a zero (if there is not a digit to go in this place a zero is printed instead). The decimal point has its usual meaning. E.g. the number 23 in this format would be printed as 23.000.

For comparison, if the format was "000.000", the output would be 023.000. This shows the difference between # and 0.

Note that to use DecimalFormat, you will need to add import java.text.DecimalFormat; at the top of your program, so that the compiler understands what you mean by it. If you forget this, you will get a compiler error ``cannot resolve symbol''.

Once a format has been created in this way it may be used again and again for different numbers being printed out. To output a number, here 23, in the format myFormat use the code:

  // creates a string (called sOutput),
  //  which is the number in the desired format
  String sOutput = myFormat.format(23);

  // print out as usual
  System.out.println(sOutput);

The formats created may be used on all number variable types in exactly the same way. Despite the name DecimalFormat the value may be an integer, as demonstrated above, or formats need not include a decimal place, thus printing numbers truncated to integer form.

As well as #'s and 0's an E may be used to specify the exponential from of a number. For example "0.##E0" would give a number in scientific format, with a maximum of two decimal places -- any extra digits being rounded off.

  DecimalFormat mySciForm = new DecimalFormat("0.##E0");
  String sOutput = mySciForm.format(157.98642);
  System.out.println(sOutput);

The above code outputs 157.98642 in scientific format with two decimal places. The output from the program is therefore 1.58e2

As a shortcut, there is no need to create the intermediate String. The following code does exactly the same as the previous example.

  DecimalFormat mySciForm = new DecimalFormat("0.##E0");
  System.out.println(mySciForm.format(157.98642);

Exercise 5.1


(5 marks)



What would the following print out? Work it out without using the computer and write the answer in your lab book. Then write a program to check your answers.

  DecimalFormat fmt1 = new DecimalFormat("0.000E0");
  DecimalFormat fmt2 = new DecimalFormat("0.0##");
  System.out.println ( fmt1.format(0.1) );           // (a)
  System.out.println ( fmt1.format(3.14159) );       // (b)
  System.out.println ( fmt2.format(1.5) );           // (c)
  System.out.println ( fmt2.format(0.0009) );        // (d)
  System.out.println ( fmt2.format(2.7e3) );         // (e)
Note: your test program will need to import java.text.DecimalFormat.



More detailed information on formatting numbers can be found in the online Java API documentation [4].


next up previous contents
Next: Input from a file Up: 5 More advanced input Previous: 5 More advanced input   Contents
RHUL Dept. of Physics