Home : Course Map : Chapter 5 : Java : Tech :
The Core Java Format Class
JavaTech
Course Map
Chapter 5
File/Package/Import
  Demo 1
  Demo 2
Access/Visibility
final & Constants
Static Import
Jar Files
  Demo 3
Applet Directories
3rd Party Packages
CLASSPATH
javadoc
CodingConventions
Exercises

    Supplements
Scope
Debug Techniques
Java Runtime
Class Class
JVM Instructions 2
JVM Processing
pack200

     About JavaTech
     Codes List
     Exercises
     Feedback
     References
     Resources
     Tips
     Topic Index
     Course Guide
     What's New
[Note: With the addition in J2SE5.0 of the java.text.Formatter class and the system.out.printf() method, this Core Java Format class is no longer needed. However,we will leave this section here in case you are working on platforms with only an earlier Java version available or you encounter legacy code with this class.]

The popular book Core Java by Cay S. Horstmann & G. Cornell provided a free class intended for output formatting. It is called Format (not surprisingly!) and is now licensed under the LGPL rules

You can download the source and class files from his site in a jar file called Format.jar. Extract the files and examine the source code.

The class Format provides several overloaded Format.print(..) static methods to output a single number or string with formatting descriptions similar to the printf function in C.

For example,

  double q =10.0/3.0;
  Format.print("0.4f", q);

will result in an output of

  3.3333

You will find in the the Format.java source code a description of the formatting rules. It also includes a test stub that prints out a number of examples. Here is the output from running this stub:

x = |1.234568|
u = | 0.000012|
x = | 1.23457|
w = | 1.02000|
x = |00000000000001.23457|
x = | +1.23457|
x = |+0000000000001.23457|
x = | 0000000000001.23457|
y = | +123.00000|
y = |+123.00000 |
z = | 1.23450E+030|
x = |1.234568e+000|
u = | 1.234000e-005|
x = | 1.23457e+000|
w = | 1.02000e+000|
x = |000000001.23457e+000|
x = | +1.23457e+000|
x = |+00000001.23457e+000|
x = | 00000001.23457e+000|
y = | +1.23000e+002|
y = |+1.23000e+002 |
v = |1.00000e+001|
x = |1.234568|
z = |1.2345e+030|
w = |1.02|
u = |1.234e-005|
y = |1.23e+002|
y = |1.23e+002|
d = |51966|
d = | 51966|
d = |00000000000000051966|
d = | +51966|
d = | 0000000000000051966|
d = |51966 |
d = | 00051966|
d = |cafe|
d = | CAFE|
d = | 0xcafe|
d = |0000000000000000CAFE|
d = | 0000cafe|
d = |145376|
d = |00000000000000145376|
d = | 0145376|
d = |00000000000000145376|
d = | 000000145376|
s = |Hello |
s = |! |
|-9223372036854775808|
|0.00e+000|
| 0|
| 9.99|
| 10.00|
|2.00|
| 10.|
| Hello|
-1 = |FFFFFFFF|
100 = |1.000000e+002|
1/0 = |Inf|
-1/0 = |-Inf|
0/0 = |NaN|

 

You can see that the class provides a wide range of useful formats.

To send formatted outputs directly to a string, the class provides several overloaded format(arg) methods, where arg is a number type. An instance of the format must first be created with the desired formatting description.

This can be done in a convenient single line approach by appending the method invocation to the instantiation operation as shown here:

  double q =10.0/3.0;
  String str = new Format("0.3e").format(q)

which results in the string variable str referencing "3.333e+000".

The following applet illustrates how the previous demo would change if Format is used.

CJ_applet7.java

... code segment ...

    double q = 1.0/3.0;

    // Create an instance of the format pattern
    // and then create the string with the method format
    qValStr = new Format ("%0.3f").format (q);
    System.out.println ("1.0/3.0 = " + qValStr);

    // Can change the format pattern:
    qValStr = new Format ("%0.5f").format (q);
    System.out.println ("1.0/3.0 = " + qValStr);

    // The # symbol indicates trailing blanks
    q = 1.0/2.0;
    qValStr = new Format ("%0.5g").format (q);
    System.out.println ("1.0/2.0 = " + qValStr);

    //
    q = 1000.0/3.0;
    qValStr = new Format ("%0.2e").format (q);
    System.out.println ("1000.0/3.0 = " + qValStr);

    //
    q = 3.0/4567.0;
    qValStr = new Format ("%0.2e").format (q);
    System.out.println ("3.0/4567.0 = " + qValStr);


    // Negative infinity
    q = -1.0/0.0;
    qValStr = new Format ("%0.3e").format (q);
    System.out.println ("-1.0/0.0 = " + qValStr);

    // NaN
    q = 0.0/0.0;
    qValStr = new Format ("%0.3e").format (q);
    System.out.println ("0.0/0.0 = " + qValStr);


.... rest of the code ...

Output of this program:

1.0/3.0 = 0.333
1.0/3.0 = 0.33333
1.0/2.0 = 0.5
1000.0/3.0 = 3.33e+002
3.0/4567.0 = 6.57e-004
-1.0/0.0 = -Inf
0.0/0.0 = NaN

The following applet uses the Format class to illustrate how to select the precision for the format of floating point numbers.

FPformatCJ.java

We have not yet discussed how to program graphical interfaces but you can look at this code for a preview.

Latest update: Oct. 25, 2004

            Tech
DecimalFormat
  Demo 1
  Demo 2

System.out.printf
  Demo 3
CJ Format Class
  Demo 4   Demo 5
Other Format Tools

Immutable Complex
Exercises

           Physics
Interpolation
Integration
  Demo 1
Simpson Rule
  Demo 2
Exercises

  Part I Part II Part III
Java Core 1  2  3  4  5  6  7  8  9  10  11  12 13 14 15 16 17
18 19 20
21
22 23 24
Supplements

1  2  3  4  5  6  7  8  9  10  11  12

Tech 1  2  3  4  5  6  7  8  9  10  11  12
Physics 1  2  3  4  5  6  7  8  9  10  11  12

Java is a trademark of Sun Microsystems, Inc.