Rogue Wave banner
Previous fileTop of DocumentContentsIndexNext file

8.6 QuickFormat

The QuickFormat class provides a convenient interface for the combined functionality of TextPicture, TextAlignment, NumericPicture, and NumericFormat. Typically, you should start with a picture string, then set formatting parameters and substitute in values. For example, this code:

produces:

Writing code to produce formatted output using the four classes separately can require a lot of typing. The QuickFormat class is a simply wrapper for these classes that provides a more terse output syntax:

The QuickFormat class interface is a mixture of C and C++ output styles. TextPicture provides the intuitive readability of printf, while the chaining of methods is reminiscent of C++'s overloaded shift operator. Each method returns a QuickFormat object on which the subsequent method is invoked.

A QuickFormat object is usually created using a TextPicture picture string; the specifics of formatting and the substitution of values is then done with method calls.

There are a number of arg methods that correspond to those in the TextPicture class, with the exception that numeric values are formatted according to a NumericPicture object kept internally in QuickFormat objects. A NumericFormat object is also held by QuickFormat for determining the width and precision of numeric arguments.

The npic(NumericPicture nl) method can be used to substitute a new NumericPicture object for the one in use. The nfmt(int width, int precision) method changes the width and precision of the current NumericFormat in use, while pre(int precision) changes only the precision.

Note that to make use of the most advanced features of NumericFormat, you have to create your own NumericFormat object separate from QuickFormat and use the NumericFormat.format(n) method to obtain a string. That string can then be used with the QuickFormat.arg(String) method.

The following program, QuickFormatExample2.java, prints another bank statement, this one based on the QuickFormat class.


NOTE: Complete code for this example is located in the examples directory created for your installation of Tools.h++ Professional. The "Examples" chapter in Part V, "Resources," describes the location of that directory, or you can check the online build guide for your installation media.

Note that the NumericPicture has been modified to use parentheses for negative balances, giving as output:



Previous fileTop of DocumentContentsIndexNext file

©Copyright 2000, Rogue Wave Software, Inc.
Contact Rogue Wave about documentation or support issues.