Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

cformat
Class PrintfFormat  view PrintfFormat download PrintfFormat.java

java.lang.Object
  extended bycformat.PrintfFormat

public class PrintfFormat
extends java.lang.Object

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

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);
 


Nested Class Summary
private  class PrintfFormat.DecDouble
          Stores the decimal representation of a double.
private  class PrintfFormat.OutBuffer
          OutBuffer is used to form the output character sequence.
 
Field Summary
(package private)  boolean addBlank
           
(package private)  boolean addSign
           
(package private)  boolean alternate
           
private  PrintfFormat.DecDouble dd
           
private static char[] ddigits
           
private  int idx
           
(package private)  boolean leftAdjust
           
private  PrintfFormat.OutBuffer output
           
(package private)  int prec
           
(package private)  java.lang.String prefix
           
(package private)  java.lang.String suffix
           
(package private)  char type
           
private  java.lang.String validTypes
           
(package private)  int width
           
private static char[] xdigits
           
private static char[] Xdigits
           
(package private)  boolean zeropad
           
 
Constructor Summary
PrintfFormat(java.lang.String fmt)
          Creates a new instance of PrintfFormat from the supplied format string.
 
Method Summary
private  void convert(long x, int n, int m, java.lang.String d)
           
private  void expFormat(PrintfFormat.DecDouble dd, int p)
           
private  void expHexFormat(double d, int p)
           
private  void fixedFormat(PrintfFormat.DecDouble dd, int p)
           
private  void freeFormat(PrintfFormat.DecDouble dd, int prec)
           
 java.lang.String getPrefix()
          Gets the prefix string associated with the format.
 java.lang.String getSuffix()
          Gets the suffix string associated with the format.
private  java.lang.String pad()
           
private  void roundUpFixedOutput(PrintfFormat.OutBuffer out)
           
private  int scanRegularChars(char[] buf, java.lang.String fmt)
           
private  int scanUnsignedInt(java.lang.String fmt)
           
 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(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.
private  void uconv(long val, int radix, char[] digits)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

ddigits

private static char[] ddigits

xdigits

private static char[] xdigits

Xdigits

private static char[] Xdigits

prefix

java.lang.String prefix

suffix

java.lang.String suffix

addBlank

boolean addBlank

addSign

boolean addSign

alternate

boolean alternate

leftAdjust

boolean leftAdjust

zeropad

boolean zeropad

type

char type

prec

int prec

width

int width

dd

private PrintfFormat.DecDouble dd

output

private PrintfFormat.OutBuffer output

validTypes

private java.lang.String validTypes

idx

private int idx
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.

Method Detail

setPrefix

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


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.


setSuffix

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


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.


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 methodName, 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.


tostr

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


tostr

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


tostr

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


tostr

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


tostr

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


tostr

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


convert

private void convert(long x,
                     int n,
                     int m,
                     java.lang.String d)

expFormat

private void expFormat(PrintfFormat.DecDouble dd,
                       int p)

expHexFormat

private void expHexFormat(double d,
                          int p)

fixedFormat

private void fixedFormat(PrintfFormat.DecDouble dd,
                         int p)

freeFormat

private void freeFormat(PrintfFormat.DecDouble dd,
                        int prec)

pad

private java.lang.String pad()

roundUpFixedOutput

private void roundUpFixedOutput(PrintfFormat.OutBuffer out)

scanRegularChars

private int scanRegularChars(char[] buf,
                             java.lang.String fmt)

scanUnsignedInt

private int scanUnsignedInt(java.lang.String fmt)

uconv

private void uconv(long val,
                   int radix,
                   char[] digits)