umontreal.iro.lecuyer.util
Class PrintfFormat
- java.lang.Object
-
- umontreal.iro.lecuyer.util.PrintfFormat
-
- All Implemented Interfaces:
- java.lang.Appendable, java.lang.CharSequence
public class PrintfFormat extends java.lang.Object implements java.lang.CharSequence, java.lang.AppendableThis class acts like aStringBufferwhich 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
Formatterfor performing locale-independent formatting.
-
-
Field Summary
Fields Modifier and Type Field and Description static java.lang.StringLINE_SEPARATORDeprecated.static java.lang.StringNEWLINEEnd-of-line symbol or line separator.
-
Constructor Summary
Constructors 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.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method and Description PrintfFormatappend(char c)Appends a single character to the buffer.java.lang.Appendableappend(java.lang.CharSequence csq)java.lang.Appendableappend(java.lang.CharSequence csq, int start, int end)PrintfFormatappend(double x)Appends x to the buffer.PrintfFormatappend(int x)Appends x to the buffer.PrintfFormatappend(int fieldwidth, double x)Uses thefstatic method to append x to the buffer.PrintfFormatappend(int fieldwidth, int x)Uses thedstatic method to append x to the buffer.PrintfFormatappend(int fieldwidth, int precision, double x)Uses thefstatic method to append x to the buffer.PrintfFormatappend(int fieldwidth, int accuracy, int precision, double x)Uses theformatstatic method with the same four arguments to append x to the buffer.PrintfFormatappend(int fieldwidth, long x)Uses thedstatic method to append x to the buffer.PrintfFormatappend(int fieldwidth, java.lang.String str)Uses thesstatic method to append str to the buffer.PrintfFormatappend(long x)Appends x to the buffer.PrintfFormatappend(java.lang.String str)Appends str to the buffer.charcharAt(int index)voidclear()Clears the contents of the buffer.static java.lang.Stringd(int fieldwidth, int precision, long x)Formats the long integer x into a string like %d in the C printf function.static java.lang.Stringd(int fieldwidth, long x)Same asd(fieldwidth, 1, x).static java.lang.Stringd(long x)Same asd(0, 1, x).static java.lang.Stringe(double x)Same ase(0, 6, x).static java.lang.StringE(double x)Same asE(0, 6, x).static java.lang.Stringe(int fieldwidth, double x)Same ase(fieldwidth, 6, x).static java.lang.StringE(int fieldwidth, double x)Same asE(fieldwidth, 6, x).static java.lang.Stringe(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.StringE(int fieldwidth, int precision, double x)Formats a double-precision number x like %E in C printf.static java.lang.Stringf(double x)Same asf(0, 6, x).static java.lang.Stringf(int fieldwidth, double x)Same asf(fieldwidth, 6, x).static java.lang.Stringf(int fieldwidth, int precision, double x)Formats the double-precision x into a string like %f in C printf.static java.lang.Stringformat(int fieldwidth, int accuracy, int precision, double x)Returns aStringcontaining x.static java.lang.Stringformat(int fieldwidth, long x)Converts a long integer to aStringwith a minimum length of fieldwidth, the result is right-padded with spaces if necessary but it is not truncated.static java.lang.Stringformat(java.util.Locale locale, int fieldwidth, int accuracy, int precision, double x)This method is equivalent toformat, except it formats the given value for the locale locale.static java.lang.Stringformat(long x)Same asd(0, 1, x).static java.lang.StringformatBase(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.StringformatBase(int fieldwidth, int b, long x)Converts the integer x to aStringrepresentation in base b.static java.lang.StringformatBase(int b, long x)Same asformatBase(0, b, x).static voidformatWithError(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 voidformatWithError(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 voidformatWithError(java.util.Locale locale, int fieldwidth, int fieldwidtherr, int precision, double x, double error, java.lang.String[] res)This method is equivalent toformatWithError, except that it formats the given value and error for the locale locale.static voidformatWithError(java.util.Locale locale, int fieldwidth, int fieldwidtherr, int accuracy, int precision, double x, double error, java.lang.String[] res)This method is equivalent toformatWithError, except that it formats the given value and error for the locale locale.static java.lang.Stringg(double x)Same asg(0, 6, x).static java.lang.StringG(double x)Same asG(0, 6, x).static java.lang.Stringg(int fieldwidth, double x)Same asg(fieldwidth, 6, x).static java.lang.StringG(int fieldwidth, double x)Same asG(fieldwidth, 6, x).static java.lang.Stringg(int fieldwidth, int precision, double x)The same as G, except that `e' is used in the scientific notation.static java.lang.StringG(int fieldwidth, int precision, double x)Formats the double-precision x into a string like %G in C printf.java.lang.StringBuffergetBuffer()Returns theStringBufferassociated with that object.intlength()static java.lang.Strings(int fieldwidth, java.lang.String str)Formats the string str like the %s in the C printf function.static java.lang.Strings(java.lang.String str)Same ass(0, str).java.lang.CharSequencesubSequence(int start, int end)java.lang.StringtoString()Converts the buffer into aString.
-
-
-
Field Detail
-
NEWLINE
public static final java.lang.String NEWLINE
End-of-line symbol or line separator. It is `` \n'' for Unix/Linux, `` \r \n" for MS-DOS/MS-Windows, and `` \r'' for Apple OSX.
-
LINE_SEPARATOR
@Deprecated public static final java.lang.String LINE_SEPARATOR
Deprecated.Same as NEWLINE.
-
-
Constructor Detail
-
PrintfFormat
public PrintfFormat()
Constructs a new buffer object containing an empty string.
-
PrintfFormat
public PrintfFormat(int length)
Constructs a new buffer object with an initial capacity of length.- Parameters:
length- initial length of the buffer
-
PrintfFormat
public PrintfFormat(java.lang.String str)
Constructs a new buffer object containing the initial string str.- Parameters:
str- initial contents of the buffer
-
-
Method Detail
-
append
public PrintfFormat append(java.lang.String str)
Appends str to the buffer.- Parameters:
str- string to append to the buffer- Returns:
- this object
-
append
public PrintfFormat append(int fieldwidth, java.lang.String str)
Uses thesstatic method to append str to the buffer. A minimum of fieldwidth characters will be used.- Parameters:
fieldwidth- minimum number of characters that will be added to the bufferstr- string to append to the buffer- Returns:
- this object
-
append
public PrintfFormat append(double x)
Appends x to the buffer.- Parameters:
x- value being added to the buffer- Returns:
- this object
-
append
public PrintfFormat append(int fieldwidth, double x)
Uses thefstatic method to append x to the buffer. A minimum of fieldwidth characters will be used.- Parameters:
fieldwidth- minimum length of the converted string to be appendedx- value to be appended to the buffer- Returns:
- this object
-
append
public PrintfFormat append(int fieldwidth, int precision, double x)
Uses thefstatic method to append x to the buffer. A minimum of fieldwidth characters will be used with the given precision.- Parameters:
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 buffer- Returns:
- this object
-
append
public PrintfFormat append(int x)
Appends x to the buffer.- Parameters:
x- value to be appended to the buffer- Returns:
- this object
-
append
public PrintfFormat append(int fieldwidth, int x)
Uses thedstatic method to append x to the buffer. A minimum of fieldwidth characters will be used.- Parameters:
fieldwidth- minimum length of the converted string to be appendedx- value to be appended to the buffer- Returns:
- this object
-
append
public PrintfFormat append(long x)
Appends x to the buffer.- Parameters:
x- value to be appended to the buffer- Returns:
- this object
-
append
public PrintfFormat append(int fieldwidth, long x)
Uses thedstatic method to append x to the buffer. A minimum of fieldwidth characters will be used.- Parameters:
fieldwidth- minimum length of the converted string to be appendedx- value to be appended to the buffer- Returns:
- this object
-
append
public PrintfFormat append(int fieldwidth, int accuracy, int precision, double x)
Uses theformatstatic method with the same four arguments to append x to the buffer.- Parameters:
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 buffer- Returns:
- this object
-
append
public PrintfFormat append(char c)
Appends a single character to the buffer.- Specified by:
appendin interfacejava.lang.Appendable- Parameters:
c- character to be appended to the buffer- Returns:
- this object
-
clear
public void clear()
Clears the contents of the buffer.
-
getBuffer
public java.lang.StringBuffer getBuffer()
Returns theStringBufferassociated with that object.- Returns:
- the internal
StringBufferobject
-
toString
public java.lang.String toString()
Converts the buffer into aString.- Specified by:
toStringin interfacejava.lang.CharSequence- Overrides:
toStringin classjava.lang.Object- Returns:
- the
Stringconversion of the internal buffer
-
s
public static java.lang.String s(java.lang.String str)
Same ass(0, str). If the string str is null, it returns the string ``null''.- Parameters:
str- the string to process- Returns:
- the same string
-
s
public static java.lang.String s(int fieldwidth, java.lang.String str)Formats the string str like the %s in the C printf function. The fieldwidth argument gives the minimum length of the resulting string. If str is shorter than fieldwidth, it is left-padded with spaces. If fieldwidth is negative, the string is right-padded with spaces if necessary. TheStringwill never be truncated. If str is null, it callss(fieldwidth, ``null''). The fieldwidth argument has the same effect for the other methods in this class.- Parameters:
fieldwidth- minimum length of the returned stringstr- the string to process- Returns:
- the same string padded with spaces if necessary
-
d
public static java.lang.String d(long x)
Same asd(0, 1, x).- Parameters:
x- the string to process- Returns:
- the same string, padded with spaces or zeros if appropriate
-
d
public static java.lang.String d(int fieldwidth, long x)Same asd(fieldwidth, 1, x).- Parameters:
fieldwidth- minimum length of the returned stringx- the string to process- Returns:
- the same string, padded with spaces or zeros if appropriate
-
d
public 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. It converts its argument to decimal notation, precision gives the minimum number of digits that must appear; if the converted value requires fewer digits, it is padded on the left with zeros. When zero is printed with an explicit precision 0, the output is empty.- Parameters:
fieldwidth- minimum length of the returned stringprecision- number of digits in the returned stringx- the string to process- Returns:
- the same string, padded with spaces or zeros if appropriate
-
format
public static java.lang.String format(long x)
Same asd(0, 1, x).- Parameters:
x- the value to be processed- Returns:
- the string resulting from the conversion
-
format
public static java.lang.String format(int fieldwidth, long x)Converts a long integer to aStringwith 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.- Parameters:
fieldwidth- minimum length of the returned stringx- the value to be processed- Returns:
- the string resulting from the conversion
-
formatBase
public static java.lang.String formatBase(int b, long x)Same asformatBase(0, b, x).- Parameters:
b- the base used for conversionx- the value to be processed- Returns:
- a string representing x in base b
-
formatBase
public static java.lang.String formatBase(int fieldwidth, int b, long x)Converts the integer x to aStringrepresentation in base b. Restrictions: 2 <= b <= 10.- Parameters:
fieldwidth- minimum length of the returned stringb- the base used for conversionx- the value to be processed- Returns:
- a string representing x in base b
-
E
public static java.lang.String E(double x)
Same asE(0, 6, x).- Parameters:
x- the value to be converted to string- Returns:
- the converted value as a string
-
E
public static java.lang.String E(int fieldwidth, double x)Same asE(fieldwidth, 6, x).- Parameters:
fieldwidth- minimum length of the returned stringx- the value to be converted to string- Returns:
- the converted value as a string
-
E
public static java.lang.String E(int fieldwidth, int precision, double x)Formats a double-precision number x like %E in C printf. The double argument is rounded and converted in the style [-]d.dddE+-dd where there is one digit before the decimal-point character and the number of digits after it is equal to the precision; if the precision is 0, no decimal-point character appears. The exponent always contains at least two digits; if the value is zero, the exponent is 00.- Parameters:
fieldwidth- minimum length of the returned stringprecision- number of digits after the decimal pointx- the value to be converted to string- Returns:
- the converted value as a string
-
e
public static java.lang.String e(double x)
Same ase(0, 6, x).- Parameters:
x- the value to be converted to string- Returns:
- the converted value as a string
-
e
public static java.lang.String e(int fieldwidth, double x)Same ase(fieldwidth, 6, x).- Parameters:
fieldwidth- minimum length of the returned stringx- the value to be converted to string- Returns:
- the converted value as a string
-
e
public 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'.- Parameters:
fieldwidth- minimum length of the returned stringprecision- number of digits after the decimal pointx- the value to be converted to string- Returns:
- the converted value as a string
-
f
public static java.lang.String f(double x)
Same asf(0, 6, x).- Parameters:
x- the value to be converted to string- Returns:
- the converted value as a string
-
f
public static java.lang.String f(int fieldwidth, double x)Same asf(fieldwidth, 6, x).- Parameters:
fieldwidth- minimum length of the returned stringx- the value to be converted to string- Returns:
- the converted value as a string
-
f
public static java.lang.String f(int fieldwidth, int precision, double x)Formats the double-precision x into a string like %f in C printf. The argument is rounded and converted to decimal notation in the style [-]ddd.ddd, where the number of digits after the decimal-point character is equal to the precision specification. If the precision is explicitly 0, no decimal-point character appears. If a decimal point appears, at least one digit appears before it.- Parameters:
fieldwidth- minimum length of the returned stringprecision- number of digits after the decimal pointx- the value to be converted to string- Returns:
- the converted value as a string
-
G
public static java.lang.String G(double x)
Same asG(0, 6, x).- Parameters:
x- the value to be converted to string- Returns:
- the converted value as a string
-
G
public static java.lang.String G(int fieldwidth, double x)Same asG(fieldwidth, 6, x).- Parameters:
fieldwidth- minimum length of the returned stringx- the value to be converted to string- Returns:
- the converted value as a string
-
G
public static java.lang.String G(int fieldwidth, int precision, double x)Formats the double-precision x into a string like %G in C printf. The argument is converted in style %f or %E. precision specifies the number of significant digits. If it is 0, it is treated as 1. Style %E is used if the exponent from its conversion is less than -4 or greater than or equal to precision. Trailing zeros are removed from the fractional part of the result; a decimal point appears only if it is followed by at least one digit.- Parameters:
fieldwidth- minimum length of the returned stringprecision- number of significant digitsx- the value to be converted to string- Returns:
- the converted value as a string
-
g
public static java.lang.String g(double x)
Same asg(0, 6, x).- Parameters:
x- the value to be converted to string- Returns:
- the converted value as a string
-
g
public static java.lang.String g(int fieldwidth, double x)Same asg(fieldwidth, 6, x).- Parameters:
fieldwidth- minimum length of the returned stringx- the value to be converted to string- Returns:
- the converted value as a string
-
g
public static java.lang.String g(int fieldwidth, int precision, double x)The same as G, except that `e' is used in the scientific notation.- Parameters:
fieldwidth- minimum length of the returned stringprecision- number of significant digitsx- the value to be converted to string- Returns:
- the converted value as a string
-
format
public static java.lang.String format(int fieldwidth, int accuracy, int precision, double x)Returns aStringcontaining 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.- Parameters:
fieldwidth- minimum length of the returned stringaccuracy- number of digits after the decimal pointprecision- number of significant digitsx- the value to be processed- Returns:
- the converted value as a string
-
format
public static java.lang.String format(java.util.Locale locale, int fieldwidth, int accuracy, int precision, double x)This method is equivalent toformat, except it formats the given value for the locale locale.- Parameters:
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 processed- Returns:
- the converted value as a string
-
formatBase
public 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. Uses a total of at least fieldwidth positions (including the sign and point when they appear) and accuracy digits after the decimal point. If fieldwidth is negative, the number is printed left-justified, otherwise right-justified. Restrictions: 2 <= b <= 10 and | x| < 263.- Parameters:
fieldwidth- minimum length of the returned stringaccuracy- number of digits after the decimal pointb- basex- the value to be processed- Returns:
- the converted value as a string
-
charAt
public char charAt(int index)
- Specified by:
charAtin interfacejava.lang.CharSequence
-
length
public int length()
- Specified by:
lengthin interfacejava.lang.CharSequence
-
subSequence
public java.lang.CharSequence subSequence(int start, int end)- Specified by:
subSequencein interfacejava.lang.CharSequence
-
append
public java.lang.Appendable append(java.lang.CharSequence csq)
- Specified by:
appendin interfacejava.lang.Appendable
-
append
public java.lang.Appendable append(java.lang.CharSequence csq, int start, int end)- Specified by:
appendin interfacejava.lang.Appendable
-
formatWithError
public 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. Uses a total of at least fieldwidth positions (including the sign and point when they appear) for x, fieldwidtherr positions for error, accuracy digits after the decimal point and at least precision significant digits. accuracy and precision must be strictly smaller than fieldwidth. The numbers are rounded if necessary. If there is not enough space to format x in decimal notation with at least precision significant digits (accuracy or fieldwidth are too small), it will be converted to scientific notation with at least precision significant digits. In that case, fieldwidth is increased if necessary, and the error is also formatted in scientific notation.- Parameters:
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 error
-
formatWithError
public 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. This callsformatWithErrorwith 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.- Parameters:
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 error
-
formatWithError
public 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 toformatWithError, except that it formats the given value and error for the locale locale.- Parameters:
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 error
-
formatWithError
public 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 toformatWithError, except that it formats the given value and error for the locale locale.- Parameters:
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 error
-
-
DMelt 3.0 © DataMelt by jWork.ORG