Printing with Field Widths and Precisions

The exact size of a field in which data is printed is specified by a field width. If the field width is larger than the data being printed, the data will be right justified within that field by default. (We demonstrate left justification in Section 28.10) The programmer inserts an integer representing the field width between the percent sign (%) and the conversion character (e.g., %4d) in the format specifier. Figure 28.12 prints two groups of five numbers each, right justifying those numbers that contain fewer digits than the field width. Note that the field width is increased to print values wider than the field and that the minus sign for a negative value uses one character position in the field. Also, if no field width is specified, the data prints in exactly as many positions as it needs. Field widths can be used with all format specifiers except the line separator (%n).

Figure 28.12. Right justifying integers in fields.

(This item is displayed on page 1337 in the print version)

 1 // Fig. 28.12: FieldWidthTest.java
 2 // Right justifying integers in fields.
 3
 4 public class FieldWidthTest
 5 {
 6 public static void main( String args[] )
 7 {
 8 System.out.printf( "%4d
", 1 );
 9 System.out.printf( "%4d
", 12 );
10 System.out.printf( "%4d
", 123 );
11 System.out.printf( "%4d
", 1234 );
12 System.out.printf( "%4d

", 12345 ); // data too large
13
14 System.out.printf( "%4d
", -1 );
15 System.out.printf( "%4d
", -12 );
16 System.out.printf( "%4d
", -123 );
17 System.out.printf( "%4d
", -1234 ); // data too large
18 System.out.printf( "%4d
", -12345 ); // data too large
19 } // end main
20 } // end class RightJustifyTest
 
 1
 12
 123
1234
12345
 -1
 -12
-123
-1234
-12345
 

Common Programming Error 28.3

Not providing a sufficiently large field width to handle a value to be printed can offset other data being printed and produce confusing outputs. Know your data!

Method printf also provides the ability to specify the precision with which data is printed. Precision has different meanings for different types. When used with floating-point conversion characters e and f, the precision is the number of digits that appear after the decimal point. When used with conversion character g, the precision is the maximum number of significant digits to be printed. When used with conversion character s, the precision is the maximum number of characters to be written from the string. To use precision, place between the percent sign and the conversion specifier a decimal point (.) followed by an integer representing the precision. Figure 28.13 demonstrates the use of precision in format strings. Note that when a floating-point value is printed with a precision smaller than the original number of decimal places in the value, the value is rounded. Also note that the format specifier %.3g indicates that the total number of digits used to display the floating-point value is 3. Because the value has three digits to the left of the decimal point, the value is rounded to the ones position.

Figure 28.13. Using precision for floating-point numbers and strings.

(This item is displayed on page 1338 in the print version)

 1 // Fig. 28.13: PrecisionTest.java
 2 // Using precision for floating-point numbers and strings.
 3 public class PrecisionTest
 4 {
 5 public static void main( String args[] )
 6 {
 7 double f = 123.94536;
 8 String s = "Happy Birthday";
 9
10 System.out.printf( "Using precision for floating-point numbers
" );
11 System.out.printf( "	%.3f
	%.3e
	%.3g

", f, f, f );
12
13 System.out.printf( "Using precision for strings
" );
14 System.out.printf( "	%.11s
", s );
15 } // end main
16 } // end class PrecisionTest
 
Using precision for floating-point numbers
 123.945
 1.239e+02
 124
Using precision for strings
 Happy Birth
 

The field width and the precision can be combined by placing the field width, followed by a decimal point, followed by a precision between the percent sign and the conversion character, as in the statement

printf( "%9.3f", 123.456789 );

which displays 123.457 with three digits to the right of the decimal point right justified in a nine-digit fieldthis number will be preceded in its field by two blanks.

Introduction to Computers, the Internet and the World Wide Web

Introduction to Java Applications

Introduction to Classes and Objects

Control Statements: Part I

Control Statements: Part 2

Methods: A Deeper Look

Arrays

Classes and Objects: A Deeper Look

Object-Oriented Programming: Inheritance

Object-Oriented Programming: Polymorphism

GUI Components: Part 1

Graphics and Java 2D™

Exception Handling

Files and Streams

Recursion

Searching and Sorting

Data Structures

Generics

Collections

Introduction to Java Applets

Multimedia: Applets and Applications

GUI Components: Part 2

Multithreading

Networking

Accessing Databases with JDBC

Servlets

JavaServer Pages (JSP)

Formatted Output

Strings, Characters and Regular Expressions

Appendix A. Operator Precedence Chart

Appendix B. ASCII Character Set

Appendix C. Keywords and Reserved Words

Appendix D. Primitive Types

Appendix E. (On CD) Number Systems

Appendix F. (On CD) Unicode®

Appendix G. Using the Java API Documentation

Appendix H. (On CD) Creating Documentation with javadoc

Appendix I. (On CD) Bit Manipulation

Appendix J. (On CD) ATM Case Study Code

Appendix K. (On CD) Labeled break and continue Statements

Appendix L. (On CD) UML 2: Additional Diagram Types

Appendix M. (On CD) Design Patterns

Appendix N. Using the Debugger

Inside Back Cover



Java(c) How to Program
Java How to Program (6th Edition) (How to Program (Deitel))
ISBN: 0131483986
EAN: 2147483647
Year: 2003
Pages: 615

Flylib.com © 2008-2020.
If you may any questions please contact us: flylib@qtcs.net