|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object | +--cformat.PrintfFormat
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);
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 |
public PrintfFormat(java.lang.String fmt) throws java.lang.IllegalArgumentException
set
method.
fmt
- Format string
java.lang.IllegalArgumentException
- Malformed format stringset(java.lang.String)
Method Detail |
public void set(java.lang.String fmt) throws java.lang.IllegalArgumentException
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 conversion character is one of:
[-]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.
[-]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.
[-]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.
fmt
- Format string
java.lang.IllegalArgumentException
- Malformed format stringpublic java.lang.String tostr(float x)
x
- Float value to convert
public java.lang.String tostr(double x)
x
- Double value to convert
public java.lang.String tostr(int x)
x
- Int value to convert
public java.lang.String tostr(long x)
x
- Long value to convert
public java.lang.String tostr(char x)
x
- Char value to convert
public java.lang.String tostr(java.lang.String x)
x
- String value to format
public java.lang.String tostr(javax.vecmath.GVector vec)
vec
- GVector to format
public java.lang.String getPrefix()
setPrefix(java.lang.String)
public java.lang.String getSuffix()
setSuffix(java.lang.String)
public java.lang.String setPrefix(java.lang.String s)
s
- New prefix stringgetPrefix()
public java.lang.String setSuffix(java.lang.String s)
s
- New suffix stringgetSuffix()
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |