Class PrintfFormat
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);
- See Also:
-
Constructor Summary
ConstructorsConstructorDescriptionPrintfFormat(String fmt) Deprecated.Creates a new instance of PrintfFormat from the supplied format string. -
Method Summary
Modifier and TypeMethodDescriptionDeprecated.Gets the prefix string associated with the format.Deprecated.Gets the suffix string associated with the format.voidDeprecated.Sets the format characteristics according to the supplied String.Deprecated.Sets the prefix string associated with the format.Deprecated.Sets the suffix string associated with the format.tostr(char x) Deprecated.Formats a char into a string.tostr(double x) Deprecated.Formats a double into a string.tostr(float x) Deprecated.Formats a float into a string.tostr(int x) Deprecated.Formats an int into a string.tostr(long x) Deprecated.Formats a long into a string.Deprecated.Formats a Double into a string.Deprecated.Formats an Integer into a string.Deprecated.Formats a String into a string.
-
Constructor Details
-
PrintfFormat
Deprecated.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:
IllegalArgumentException- Malformed format string- See Also:
-
-
Method Details
-
getPrefix
Deprecated.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:
-
getSuffix
Deprecated.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:
-
set
Deprecated.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.
- Parameters:
fmt- Format string- Throws:
IllegalArgumentException- Malformed format string
-
setPrefix
Deprecated.Sets the prefix string associated with the format.- Parameters:
s- New prefix string- See Also:
-
setSuffix
Deprecated.Sets the suffix string associated with the format.- Parameters:
s- New suffix string- See Also:
-
tostr
Deprecated.Formats a float into a string.- Parameters:
x- Float value to convert- Returns:
- Resulting string
-
tostr
Deprecated.Formats a Double into a string.- Parameters:
x- Float value to convert- Returns:
- Resulting string
-
tostr
Deprecated.Formats an Integer into a string.- Parameters:
x- Float value to convert- Returns:
- Resulting string
-
tostr
Deprecated.Formats a double into a string.- Parameters:
x- Double value to convert- Returns:
- Resulting string
-
tostr
Deprecated.Formats an int into a string.- Parameters:
x- Int value to convert- Returns:
- Resulting string
-
tostr
Deprecated.Formats a long into a string.- Parameters:
x- Long value to convert- Returns:
- Resulting string
-
tostr
Deprecated.Formats a char into a string.- Parameters:
x- Char value to convert- Returns:
- Resulting string
-
tostr
Deprecated.Formats a String into a string.- Parameters:
x- String value to format- Returns:
- Resulting string
-