|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectmaspack.util.NumberFormat
Object for formatting numbers in the same way similiar to the
C printf
function.
A printf
style format string is specified in the
constructor, or can be respecified using the set
method.
Once instantiated, the format
methods of this
class may be used to convert numeric types (float, double,
int, long) into Strings.
Examples:
double theta1 = 45.0; double theta2 = 85.0; NumberFormat fmt = new NumberFormat ("%7.2f\n"); System.out.println ("theta1=" + fmt.format(theta1) + "theta2=" + fmt.format(theta2));
Constructor Summary | |
NumberFormat()
Creates a new instance of PrintfFormat with the default format string %g . |
|
NumberFormat(java.lang.String fmtStr)
Creates a new instance of PrintfFormat from the supplied format string. |
Method Summary | |
java.lang.String |
format(double x)
Formats a double into a string. |
java.lang.StringBuffer |
format(double x,
java.lang.StringBuffer sbuf)
Formats a double and appends it to the end of a StringBuffer |
java.lang.String |
format(int x)
Formats an int into a string. |
java.lang.StringBuffer |
format(int x,
java.lang.StringBuffer sbuf)
Formats an int and appends it to the end of a StringBuffer |
java.lang.String |
format(long x)
Formats a long into a string. |
java.lang.StringBuffer |
format(long x,
java.lang.StringBuffer sbuf)
Formats a long and appends it to the end of a StringBuffer |
int |
getFieldWidth()
Returns the field width associated with this format. |
int |
getPrecision()
Returns the precision associated with this format. |
java.lang.String |
getPrefix()
Gets the prefix associated with this format. |
java.lang.String |
getSuffix()
Gets the suffix associated with this format. |
void |
set(java.lang.String fmtStr)
Sets the format characteristics according to the supplied String. |
void |
setFieldWidth(int w)
Sets the field width associated with this format. |
void |
setPrecision(int p)
Sets the precision associated with this format. |
void |
setPrefix(java.lang.String pre)
Sets the prefix associated with this format. |
void |
setSuffix(java.lang.String suf)
Sets the suffix associated with this format. |
Methods inherited from class java.lang.Object |
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
public NumberFormat()
%g
.
public NumberFormat(java.lang.String fmtStr)
set
.
fmtStr
- Format string
java.lang.IllegalArgumentException
- Malformed format stringset(java.lang.String)
Method Detail |
public void setFieldWidth(int w)
w
- field width
java.lang.IllegalArgumentException
- Negative field widthgetFieldWidth()
,
set(java.lang.String)
public int getFieldWidth()
setFieldWidth(int)
,
set(java.lang.String)
public void setPrecision(int p)
set
.
A negative value implies either that no precision is set,
or a default value should be set.
p
- precisiongetPrecision()
,
set(java.lang.String)
public int getPrecision()
setPrecision(int)
,
set(java.lang.String)
public void setPrefix(java.lang.String pre)
pre
- new prefixgetPrefix()
public java.lang.String getPrefix()
public void setSuffix(java.lang.String suf)
suf
- new suffixgetSuffix()
public java.lang.String getSuffix()
public void set(java.lang.String fmtStr) throws java.lang.IllegalArgumentException
The format string has a format similar to the one used by the C
printf
function, except that only one conversion
may be specified.
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:
[-]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.
[-]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.
Double.toString
method. This
produces a decimal representation that is accurate
enough to reproduce the number exactly if it is
read back into a Java application using
Double.parseDouble
. Note that this
is different from the usual C printf
interpretation of %g
.
[-]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.
fmtStr
- Format string
java.lang.IllegalArgumentException
- Malformed format stringpublic java.lang.String format(double x)
x
- value to be formatted
public java.lang.StringBuffer format(double x, java.lang.StringBuffer sbuf)
StringBuffer.
- Parameters:
x
- value to be formattedsbuf
- buffer to place the result in
- Returns:
- pointer to the result buffer
public java.lang.String format(int x)
x
- value to be formatted
public java.lang.StringBuffer format(int x, java.lang.StringBuffer sbuf)
StringBuffer.
- Parameters:
x
- value to be formattedsbuf
- buffer to place the result in
- Returns:
- pointer to the result buffer
public java.lang.String format(long x)
x
- value to be formatted
public java.lang.StringBuffer format(long x, java.lang.StringBuffer sbuf)
StringBuffer.
- Parameters:
x
- value to be formattedsbuf
- buffer to place the result in
- Returns:
- pointer to the result buffer
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |