cformat
Class PrintfFormat

java.lang.Object
  |
  +--cformat.PrintfFormat

public class PrintfFormat
extends java.lang.Object

Object for formatting output in the same way as the C printf function.

A printf style format string is specified in the constructor. Once instantiated, the tostr methods of this class may be used to convert primitives types (float, double, char, int, long, String) into Strings. Alternatively, instances of this class may be passed as arguments to the printf methods of the PrintfWriter or PrintfStream classes.

Examples:

  
   double theta1 = 45.0;
   double theta2 = 85.0;
   PrintfFormat fmt = new PrintfFormat ("%7.2f\n");
   
   System.out.println ("theta1=" + fmt.tostr(theta1) +
                       "theta2=" + fmt.tostr(theta2)); 

   PrintfStream pfw = new PrintfStream (System.out, true);
   pfw.print ("theta1=");
   pfw.printf (fmt, theta1);
   pfw.print ("theta2=");
   pfw.printf (fmt, theta2);
 

Author:
John E. Lloyd, Fall 2000
See Also:
PrintfWriter, PrintfStream

Constructor Summary
PrintfFormat(java.lang.String fmt)
          Creates a new instance of PrintfFormat from the supplied format string.
 
Method Summary
 java.lang.String getPrefix()
          Gets the prefix string associated with the format.
 java.lang.String getSuffix()
          Gets the suffix string associated with the format.
 void set(java.lang.String fmt)
          Sets the format characteristics according to the supplied String.
 java.lang.String setPrefix(java.lang.String s)
          Sets the prefix string associated with the format.
 java.lang.String setSuffix(java.lang.String s)
          Sets the suffix string associated with the format.
 java.lang.String tostr(char x)
          Formats a char into a string.
 java.lang.String tostr(double x)
          Formats a double into a string.
 java.lang.String tostr(float x)
          Formats a float into a string.
 java.lang.String tostr(javax.vecmath.GVector vec)
          Formats a GVector into a string.
 java.lang.String tostr(int x)
          Formats an int into a string.
 java.lang.String tostr(long x)
          Formats a long into a string.
 java.lang.String tostr(java.lang.String x)
          Formats a String into a string.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

PrintfFormat

public PrintfFormat(java.lang.String fmt)
             throws java.lang.IllegalArgumentException
Creates a new instance of PrintfFormat from the supplied format string. The structure of the format string is described in the documentation for the set method.

Parameters:
fmt - Format string
Throws:
java.lang.IllegalArgumentException - Malformed format string
See Also:
set(java.lang.String)
Method Detail

set

public void set(java.lang.String fmt)
         throws java.lang.IllegalArgumentException
Sets the format characteristics according to the supplied String.

The format string has the same form as the one used by the C printf function, except that only one conversion sequence may be specified (because routines which use PrintfFormat each convert only one object).

The format string consists of an optional prefix of regular characters, followed by a conversion sequence, followed by an optional suffix of regular characters.

The conversion sequence is introduced by a '%' character, and is followed by any number of optional flag characters, an optional unsigned decimal integer specifying a field width, another optional unsigned decimal integer (preceded by a '.' character) specifying a precision, and finally a conversion character. To incorporate a '%' character into either the prefix or suffix, one should specify the sequence "%%". The allowed flag characters are:

#
The value is converted into an "alternate" form. For 'o' conversions, the output is always prefixed with a '0'. For 'x' and 'X' conversions, the output is prefixed with "0x" or "0X", respectively. For 'a', 'A', 'e', 'E', 'f', 'g', and 'G' conversions, the result will always contain a decimal point. For 'g' and 'G' conversions, trailing zeros are not removed. There is no effect for other conversions.
0
Use '0' to pad the field on the left, instead of blanks. If the conversion is 'd', 'i', 'o', 'u', 'x', or 'X', and a precision is given, then this flag is ignored.
-
The output is aligned with the left of the field boundary, and padded on the right with blanks. This flag overrides the '0' flag.
' '
Leave a blank before a positive number produced by a signed conversion.
+
A '+' sign is placed before non-negative numbers produced by a signed conversion. This flag overrides the ' ' flag.

The conversion character is one of:

d,i
The integer argument is output as a signed decimal number. If a precision is given, it describes the minimum number of digits that must appear (default 1). If the precision exceeds the number of digits that would normally appear, the output is padded on the left with zeros. When 0 is printed with precision 0, the result is empty.
o,u,x,X
The integer argument is output as an unsigned number in either octal ('o'), decimal ('u'), or hexadecimal ('x' or 'X'). The digits "abcdef" are used for 'x', and "ABCDEF" are used for 'X'. If a precision is given, it describes the minimum number of digits that must appear (default 1). If the precision exceeds the number of digits that would normally appear, the output is padded on the left with zeros. When 0 is printed with precision 0, the result is empty.
e,E
The floating point argument is output in the exponential form [-]d.ddde+dd, with the number of digits after the decimal point given by the precision. The default precision value is 6. No decimal point is output if the precision is 0. Conversion 'E' causes 'E' to be used as an exponent instead of 'e'. The exponent is always at least two characters.
f
The floating point argument is output in the form [-]ddd.ddd, with the number of digits after the decimal point given by the precision. The default precision value is 6. No decimal point is output if the precision is 0. If a decimal point appears, at least one digit appears before it.
g,G
The floating point argument is output in either the 'f' or 'e' style (or 'E' style of 'G' conversions). The precision gives the number of significant digits, with a default value of 6. Style 'e' is used if the resulting exponent is less than -4 or greater than or equal to the precision. Trailing zeros are removed from the fractional part of the result, and a decimal point appears if it is followed by at least one digit.
a,A
The floating point argument is output in the hexadecimal floating point form [-]0xh.hhhp+dd. The exponent is a decimal number and describes a power of 2. The 'A' style uses the prefix "0X", the hex digits "ABCDEF", and the exponent character 'P'. The number of digits after the decimal point is given by the precision. The default precision is enough for an exact representation of the value.
c
The single character argument is output as a character.
s
The string argument is output. If a precision is given, then the number of characters output is limited to this.

Parameters:
fmt - Format string
Throws:
java.lang.IllegalArgumentException - Malformed format string

tostr

public java.lang.String tostr(float x)
Formats a float into a string.

Parameters:
x - Float value to convert
Returns:
Resulting string

tostr

public java.lang.String tostr(double x)
Formats a double into a string.

Parameters:
x - Double value to convert
Returns:
Resulting string

tostr

public java.lang.String tostr(int x)
Formats an int into a string.

Parameters:
x - Int value to convert
Returns:
Resulting string

tostr

public java.lang.String tostr(long x)
Formats a long into a string.

Parameters:
x - Long value to convert
Returns:
Resulting string

tostr

public java.lang.String tostr(char x)
Formats a char into a string.

Parameters:
x - Char value to convert
Returns:
Resulting string

tostr

public java.lang.String tostr(java.lang.String x)
Formats a String into a string.

Parameters:
x - String value to format
Returns:
Resulting string

tostr

public java.lang.String tostr(javax.vecmath.GVector vec)
Formats a GVector into a string. The format string should contain one floating point conversion specifier, which is used repeatedly to convert each element of the vector. A space ' ' is inserted between elements.

Parameters:
vec - GVector to format
Returns:
Resulting string

getPrefix

public java.lang.String getPrefix()
Gets the prefix string associated with the format. The prefix string is that part of the format that appears before the conversion.

Returns:
Prefix string
See Also:
setPrefix(java.lang.String)

getSuffix

public java.lang.String getSuffix()
Gets the suffix string associated with the format. The suffix string is that part of the format that appears after the conversion.

Returns:
Suffix string
See Also:
setSuffix(java.lang.String)

setPrefix

public java.lang.String setPrefix(java.lang.String s)
Sets the prefix string associated with the format.

Parameters:
s - New prefix string
See Also:
getPrefix()

setSuffix

public java.lang.String setSuffix(java.lang.String s)
Sets the suffix string associated with the format.

Parameters:
s - New suffix string
See Also:
getSuffix()