public class PrintfFormat
extends java.lang.Object
implements java.lang.CharSequence, java.lang.Appendable
StringBuffer which defines new types
of append methods.
It defines certain functionalities of the ANSI C printf
function that also can be accessed through static methods.
The information given here is strongly inspired
from the man page of the C printf function.
Most methods of this class format numbers for the English US
locale only. One can use the Java class Formatter
for performing locale-independent formatting.
| Modifier and Type | Field and Description |
|---|---|
static java.lang.String |
LINE_SEPARATOR
Deprecated.
|
static java.lang.String |
NEWLINE
End-of-line symbol or line separator.
|
| Constructor and Description |
|---|
PrintfFormat()
Constructs a new buffer object containing an empty string.
|
PrintfFormat(int length)
Constructs a new buffer object with an initial capacity of length.
|
PrintfFormat(java.lang.String str)
Constructs a new buffer object containing the initial string str.
|
| Modifier and Type | Method and Description |
|---|---|
PrintfFormat |
append(char c)
Appends a single character to the buffer.
|
java.lang.Appendable |
append(java.lang.CharSequence csq) |
java.lang.Appendable |
append(java.lang.CharSequence csq,
int start,
int end) |
PrintfFormat |
append(double x)
Appends x to the buffer.
|
PrintfFormat |
append(int x)
Appends x to the buffer.
|
PrintfFormat |
append(int fieldwidth,
double x)
Uses the
f static method to append x to the buffer. |
PrintfFormat |
append(int fieldwidth,
int x)
Uses the
d static method to append x to the buffer. |
PrintfFormat |
append(int fieldwidth,
int precision,
double x)
Uses the
f static method to append x to the buffer. |
PrintfFormat |
append(int fieldwidth,
int accuracy,
int precision,
double x)
Uses the
format
static method with the same four arguments to append x to the buffer. |
PrintfFormat |
append(int fieldwidth,
long x)
Uses the
d static method to append x to the buffer. |
PrintfFormat |
append(int fieldwidth,
java.lang.String str)
Uses the
s static method to append str to the buffer. |
PrintfFormat |
append(long x)
Appends x to the buffer.
|
PrintfFormat |
append(java.lang.String str)
Appends str to the buffer.
|
char |
charAt(int index) |
void |
clear()
Clears the contents of the buffer.
|
static java.lang.String |
d(int fieldwidth,
int precision,
long x)
Formats the long integer x into a string like %d
in the C printf function.
|
static java.lang.String |
d(int fieldwidth,
long x)
Same as
d (fieldwidth, 1, x). |
static java.lang.String |
d(long x)
Same as
d (0, 1, x). |
static java.lang.String |
e(double x)
Same as
e (0, 6, x). |
static java.lang.String |
E(double x)
Same as
E (0, 6, x). |
static java.lang.String |
e(int fieldwidth,
double x)
Same as
e (fieldwidth, 6, x). |
static java.lang.String |
E(int fieldwidth,
double x)
Same as
E (fieldwidth, 6, x). |
static java.lang.String |
e(int fieldwidth,
int precision,
double x)
The same as E, except that `e' is used as the exponent
character instead of `E'.
|
static java.lang.String |
E(int fieldwidth,
int precision,
double x)
Formats a double-precision number x like %E in C printf.
|
static java.lang.String |
f(double x)
Same as
f (0, 6, x). |
static java.lang.String |
f(int fieldwidth,
double x)
Same as
f (fieldwidth, 6, x). |
static java.lang.String |
f(int fieldwidth,
int precision,
double x)
Formats the double-precision x into a string like
%f in C printf.
|
static java.lang.String |
format(int fieldwidth,
int accuracy,
int precision,
double x)
Returns a
String containing x. |
static java.lang.String |
format(int fieldwidth,
long x)
Converts a long integer to a
String with a minimum length
of fieldwidth, the result is right-padded with spaces if
necessary but it is not truncated. |
static java.lang.String |
format(java.util.Locale locale,
int fieldwidth,
int accuracy,
int precision,
double x)
This method is equivalent to
format, except it formats the given
value for the locale locale. |
static java.lang.String |
format(long x)
Same as
d (0, 1, x). |
static java.lang.String |
formatBase(int fieldwidth,
int accuracy,
int b,
double x)
Converts x to a String representation in base b using formatting similar
to the f methods.
|
static java.lang.String |
formatBase(int fieldwidth,
int b,
long x)
Converts the integer x to a
String representation in base
b. |
static java.lang.String |
formatBase(int b,
long x)
Same as
formatBase (0, b, x). |
static void |
formatWithError(int fieldwidth,
int fieldwidtherr,
int precision,
double x,
double error,
java.lang.String[] res)
Stores a string containing x into res[0], and
a string containing error into res[1], both strings being
formatted with the same notation.
|
static void |
formatWithError(int fieldwidth,
int fieldwidtherr,
int accuracy,
int precision,
double x,
double error,
java.lang.String[] res)
Stores a string containing x into res[0], and
a string containing error into res[1], both strings being
formatted with the same notation.
|
static void |
formatWithError(java.util.Locale locale,
int fieldwidth,
int fieldwidtherr,
int precision,
double x,
double error,
java.lang.String[] res)
This method is equivalent to
formatWithError,
except that it formats the given value and error for the
locale locale. |
static void |
formatWithError(java.util.Locale locale,
int fieldwidth,
int fieldwidtherr,
int accuracy,
int precision,
double x,
double error,
java.lang.String[] res)
This method is equivalent to
formatWithError,
except that it formats the given value and error for the
locale locale. |
static java.lang.String |
g(double x)
Same as
g (0, 6, x). |
static java.lang.String |
G(double x)
Same as
G (0, 6, x). |
static java.lang.String |
g(int fieldwidth,
double x)
Same as
g (fieldwidth, 6, x). |
static java.lang.String |
G(int fieldwidth,
double x)
Same as
G (fieldwidth, 6, x). |
static java.lang.String |
g(int fieldwidth,
int precision,
double x)
The same as G, except that `e' is used in the scientific
notation.
|
static java.lang.String |
G(int fieldwidth,
int precision,
double x)
Formats the double-precision x into a string like
%G in C printf.
|
java.lang.StringBuffer |
getBuffer()
Returns the
StringBuffer associated with that object. |
int |
length() |
static java.lang.String |
s(int fieldwidth,
java.lang.String str)
Formats the string str like the %s in the C printf
function.
|
static java.lang.String |
s(java.lang.String str)
Same as
s (0, str). |
java.lang.CharSequence |
subSequence(int start,
int end) |
java.lang.String |
toString()
Converts the buffer into a
String. |
public static final java.lang.String NEWLINE
@Deprecated public static final java.lang.String LINE_SEPARATOR
public PrintfFormat()
public PrintfFormat(int length)
length - initial length of the bufferpublic PrintfFormat(java.lang.String str)
str - initial contents of the bufferpublic PrintfFormat append(java.lang.String str)
str - string to append to the bufferpublic PrintfFormat append(int fieldwidth, java.lang.String str)
s static method to append str to the buffer.
A minimum of fieldwidth characters will be used.fieldwidth - minimum number of characters that will be added to the bufferstr - string to append to the bufferpublic PrintfFormat append(double x)
x - value being added to the bufferpublic PrintfFormat append(int fieldwidth, double x)
f static method to append x to the buffer.
A minimum of fieldwidth characters will be used.fieldwidth - minimum length of the converted string to be appendedx - value to be appended to the bufferpublic PrintfFormat append(int fieldwidth, int precision, double x)
f static method to append x to the buffer.
A minimum of fieldwidth characters will be used with the given precision.fieldwidth - minimum length of the converted string to be appendedprecision - number of digits after the decimal point of the converted valuex - value to be appended to the bufferpublic PrintfFormat append(int x)
x - value to be appended to the bufferpublic PrintfFormat append(int fieldwidth, int x)
d static method to append x to the buffer.
A minimum of fieldwidth characters will be used.fieldwidth - minimum length of the converted string to be appendedx - value to be appended to the bufferpublic PrintfFormat append(long x)
x - value to be appended to the bufferpublic PrintfFormat append(int fieldwidth, long x)
d static method to append x to the buffer.
A minimum of fieldwidth characters will be used.fieldwidth - minimum length of the converted string to be appendedx - value to be appended to the bufferpublic PrintfFormat append(int fieldwidth, int accuracy, int precision, double x)
format
static method with the same four arguments to append x to the buffer.fieldwidth - minimum length of the converted string to be appendedaccuracy - number of digits after the decimal pointprecision - number of significant digitsx - value to be appended to the bufferpublic PrintfFormat append(char c)
append in interface java.lang.Appendablec - character to be appended to the bufferpublic void clear()
public java.lang.StringBuffer getBuffer()
StringBuffer associated with that object.StringBuffer objectpublic java.lang.String toString()
String.toString in interface java.lang.CharSequencetoString in class java.lang.ObjectString conversion of the internal bufferpublic static java.lang.String s(java.lang.String str)
s (0, str). If the string str is null,
it returns the string ``null''.str - the string to processpublic static java.lang.String s(int fieldwidth,
java.lang.String str)
String will never
be truncated. If str is null, it calls
s (fieldwidth, ``null'').
The fieldwidth argument
has the same effect for the other methods in this class.fieldwidth - minimum length of the returned stringstr - the string to processpublic static java.lang.String d(long x)
d (0, 1, x).x - the string to processpublic static java.lang.String d(int fieldwidth,
long x)
d (fieldwidth, 1, x).fieldwidth - minimum length of the returned stringx - the string to processpublic static java.lang.String d(int fieldwidth,
int precision,
long x)
fieldwidth - minimum length of the returned stringprecision - number of digits in the returned stringx - the string to processpublic static java.lang.String format(long x)
d (0, 1, x).x - the value to be processedpublic static java.lang.String format(int fieldwidth,
long x)
String with a minimum length
of fieldwidth, the result is right-padded with spaces if
necessary but it is not truncated. If only one argument is specified,
a fieldwidth of 0 is assumed.fieldwidth - minimum length of the returned stringx - the value to be processedpublic static java.lang.String formatBase(int b,
long x)
formatBase (0, b, x).b - the base used for conversionx - the value to be processedpublic static java.lang.String formatBase(int fieldwidth,
int b,
long x)
String representation in base
b. Restrictions: 2 <= b <= 10.fieldwidth - minimum length of the returned stringb - the base used for conversionx - the value to be processedpublic static java.lang.String E(double x)
E (0, 6, x).x - the value to be converted to stringpublic static java.lang.String E(int fieldwidth,
double x)
E (fieldwidth, 6, x).fieldwidth - minimum length of the returned stringx - the value to be converted to stringpublic static java.lang.String E(int fieldwidth,
int precision,
double x)
fieldwidth - minimum length of the returned stringprecision - number of digits after the decimal pointx - the value to be converted to stringpublic static java.lang.String e(double x)
e (0, 6, x).x - the value to be converted to stringpublic static java.lang.String e(int fieldwidth,
double x)
e (fieldwidth, 6, x).fieldwidth - minimum length of the returned stringx - the value to be converted to stringpublic static java.lang.String e(int fieldwidth,
int precision,
double x)
fieldwidth - minimum length of the returned stringprecision - number of digits after the decimal pointx - the value to be converted to stringpublic static java.lang.String f(double x)
f (0, 6, x).x - the value to be converted to stringpublic static java.lang.String f(int fieldwidth,
double x)
f (fieldwidth, 6, x).fieldwidth - minimum length of the returned stringx - the value to be converted to stringpublic static java.lang.String f(int fieldwidth,
int precision,
double x)
fieldwidth - minimum length of the returned stringprecision - number of digits after the decimal pointx - the value to be converted to stringpublic static java.lang.String G(double x)
G (0, 6, x).x - the value to be converted to stringpublic static java.lang.String G(int fieldwidth,
double x)
G (fieldwidth, 6, x).fieldwidth - minimum length of the returned stringx - the value to be converted to stringpublic static java.lang.String G(int fieldwidth,
int precision,
double x)
fieldwidth - minimum length of the returned stringprecision - number of significant digitsx - the value to be converted to stringpublic static java.lang.String g(double x)
g (0, 6, x).x - the value to be converted to stringpublic static java.lang.String g(int fieldwidth,
double x)
g (fieldwidth, 6, x).fieldwidth - minimum length of the returned stringx - the value to be converted to stringpublic static java.lang.String g(int fieldwidth,
int precision,
double x)
fieldwidth - minimum length of the returned stringprecision - number of significant digitsx - the value to be converted to stringpublic static java.lang.String format(int fieldwidth,
int accuracy,
int precision,
double x)
String containing x. Uses a total of at least
fieldwidth positions (including the sign and point when they appear),
accuracy digits after the decimal point and at least precision
significant digits. accuracy and precision must be strictly
smaller than fieldwidth. The number is rounded if necessary.
If there is not enough space to format the number in decimal notation
with at least precision significant digits (accuracy or
fieldwidth is too small), it will be converted to scientific
notation with at least precision significant digits.
In that case, fieldwidth is increased if necessary.fieldwidth - minimum length of the returned stringaccuracy - number of digits after the decimal pointprecision - number of significant digitsx - the value to be processedpublic static java.lang.String format(java.util.Locale locale,
int fieldwidth,
int accuracy,
int precision,
double x)
format, except it formats the given
value for the locale locale.locale - the locale being used for formattingfieldwidth - minimum length of the returned stringaccuracy - number of digits after the decimal pointprecision - number of significant digitsx - the value to be processedpublic static java.lang.String formatBase(int fieldwidth,
int accuracy,
int b,
double x)
fieldwidth - minimum length of the returned stringaccuracy - number of digits after the decimal pointb - basex - the value to be processedpublic char charAt(int index)
charAt in interface java.lang.CharSequencepublic int length()
length in interface java.lang.CharSequencepublic java.lang.CharSequence subSequence(int start,
int end)
subSequence in interface java.lang.CharSequencepublic java.lang.Appendable append(java.lang.CharSequence csq)
append in interface java.lang.Appendablepublic java.lang.Appendable append(java.lang.CharSequence csq,
int start,
int end)
append in interface java.lang.Appendablepublic static void formatWithError(int fieldwidth,
int fieldwidtherr,
int accuracy,
int precision,
double x,
double error,
java.lang.String[] res)
fieldwidth - minimum length of the value stringfieldwidtherr - minimum length of the error stringaccuracy - number of digits after the decimal point for the value and errorprecision - number of significant digits for the valuex - the value to be processederror - the error on the value to be processedres - an array that will be filled with the formatted value and formatted errorpublic static void formatWithError(int fieldwidth,
int fieldwidtherr,
int precision,
double x,
double error,
java.lang.String[] res)
formatWithError with
the minimal accuracy for which the formatted string for error is non-zero.
If error is 0, the accuracy is 0.
If this minimal accuracy causes the strings to be formatted using scientific
notation, this method increases the accuracy until the decimal notation can be used.fieldwidth - minimum length of the value stringfieldwidtherr - minimum length of the error stringprecision - number of significant digits for the valuex - the value to be processederror - the error on the value to be processedres - an array that will be filled with the formatted value and formatted errorpublic static void formatWithError(java.util.Locale locale,
int fieldwidth,
int fieldwidtherr,
int accuracy,
int precision,
double x,
double error,
java.lang.String[] res)
formatWithError,
except that it formats the given value and error for the
locale locale.locale - the locale being usedfieldwidth - minimum length of the value stringfieldwidtherr - minimum length of the error stringaccuracy - number of digits after the decimal point for the value and errorprecision - number of significant digits for the valuex - the value to be processederror - the error on the value to be processedres - an array that will be filled with the formatted value and formatted errorpublic static void formatWithError(java.util.Locale locale,
int fieldwidth,
int fieldwidtherr,
int precision,
double x,
double error,
java.lang.String[] res)
formatWithError,
except that it formats the given value and error for the
locale locale.locale - the locale being usedfieldwidth - minimum length of the value stringfieldwidtherr - minimum length of the error stringprecision - number of significant digits for the valuex - the value to be processederror - the error on the value to be processedres - an array that will be filled with the formatted value and formatted errorTo submit a bug or ask questions, send an e-mail to Pierre L'Ecuyer.