public class PrintfFormat
extends java.lang.Object
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 and Description |
|---|
PrintfFormat(java.lang.String fmt)
Creates a new instance of PrintfFormat from the supplied format string.
|
| Modifier and Type | Method and Description |
|---|---|
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(java.lang.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(java.lang.Integer x)
Formats an Integer 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.
|
public PrintfFormat(java.lang.String fmt)
throws java.lang.IllegalArgumentException
fmt - Format stringjava.lang.IllegalArgumentException - Malformed format stringset(java.lang.String)public java.lang.String getPrefix()
setPrefix(java.lang.String)public java.lang.String getSuffix()
setSuffix(java.lang.String)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 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 conversion character is one of:
fmt - Format stringjava.lang.IllegalArgumentException - Malformed format stringpublic 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()public java.lang.String tostr(float x)
x - Float value to convertpublic java.lang.String tostr(java.lang.Double x)
x - Float value to convertpublic java.lang.String tostr(java.lang.Integer x)
x - Float value to convertpublic java.lang.String tostr(double x)
x - Double value to convertpublic java.lang.String tostr(int x)
x - Int value to convertpublic java.lang.String tostr(long x)
x - Long value to convertpublic java.lang.String tostr(char x)
x - Char value to convertpublic java.lang.String tostr(java.lang.String x)
x - String value to format