public class PrintfFormat extends 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(String fmt)
Creates a new instance of PrintfFormat from the supplied format string.
|
Modifier and Type | Method and Description |
---|---|
String |
getPrefix()
Gets the prefix string associated with the format.
|
String |
getSuffix()
Gets the suffix string associated with the format.
|
void |
set(String fmt)
Sets the format characteristics according to the supplied
String.
|
String |
setPrefix(String s)
Sets the prefix string associated with the format.
|
String |
setSuffix(String s)
Sets the suffix string associated with the format.
|
String |
tostr(char x)
Formats a char into a string.
|
String |
tostr(double x)
Formats a double into a string.
|
String |
tostr(Double x)
Formats a Double into a string.
|
String |
tostr(float x)
Formats a float into a string.
|
String |
tostr(int x)
Formats an int into a string.
|
String |
tostr(Integer x)
Formats an Integer into a string.
|
String |
tostr(long x)
Formats a long into a string.
|
String |
tostr(String x)
Formats a String into a string.
|
public PrintfFormat(String fmt) throws IllegalArgumentException
fmt
- Format stringIllegalArgumentException
- Malformed format stringset(java.lang.String)
public String getPrefix()
setPrefix(java.lang.String)
public String getSuffix()
setSuffix(java.lang.String)
public void set(String fmt) throws 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 stringIllegalArgumentException
- Malformed format stringpublic String setPrefix(String s)
s
- New prefix stringgetPrefix()
public String setSuffix(String s)
s
- New suffix stringgetSuffix()
public String tostr(float x)
x
- Float value to convertpublic String tostr(Double x)
x
- Float value to convertpublic String tostr(Integer x)
x
- Float value to convertpublic String tostr(double x)
x
- Double value to convertpublic String tostr(int x)
x
- Int value to convertpublic String tostr(long x)
x
- Long value to convertpublic String tostr(char x)
x
- Char value to convertCopyright © 2018. All rights reserved.