// This works only in JDK 1.5+ import static java.lang.System.out; // ... int c = 65; // printf works much like a Fortran FORMAT or C printf. // %2x means insert first parameter here in a field 2 chars wide in hex. // %3d means insert next parameter here in a field 3 characters wide in decimal. // %1c means insert next parameter here in a field 1 wide as a Unicode character. // Everything else, including the [] and :s are just decorative text. out.printf( "result is: [%2x] : %3d : %1c\n", c, c, c ); // prints : result is:_[41]_:__65_:_A // In JDK 1.4- you would code that as: System.out.println( "result is: [" + Stringtools.leftPad( Integer.toHexString( c ), 2 ) + "] : " + Stringtools.leftPad( Integer.toString( c ), 3 ) + " : " + (char) c); // The JDK 1.5 code is considerably easier to write and proofread, but the 1.4 code is faster. // Possible codes you might use include: // // %b boolean // %h hashcode of the value. // %s String // %c Unicode character // %d decimal integer // %o octal integer // %x hex integger // %e floating point scientific notation with exponent. // %f floating point as a decimal number without exponent. // %g general format %e or %f depending on size. // %a precise hex floating number. // %t date/time // %% a literal % // %n line separator, a platform-specific line separator, not necessarily \n. // see documentation on java.util.Formatter for a complete list and the full syntax. // Use of G format: // %[flags][min width of field].[number of significant digits of precision (not # of decimal places)]g System.out.printf( "value is %4.3g\n", 1.4d ); // prints value is 1.40 System.out.printf( "value is %4.3g\n", 1.456d ); // prints value is 1.46 System.out.printf( "value is %4.3g\n", 145.0d ); // prints value is _145 System.out.printf( "value is %8.4g\n", 1.5d ); // prints value is ___1.500 System.out.printf( "value is %4.4g\n", 1.5E-6 ); // prints value is 1.500e-06 System.out.printf( "value is %,8.6g\n", 12345.0d ); // prints value is 12,345.0