public class NumberFormat
extends java.lang.Object
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 and Description |
---|
NumberFormat()
Creates a new instance of PrintfFormat with the default format string
%g . |
NumberFormat(NumberFormat fmt)
Creates a new NumberFormat, whose setting are copied from an existing
NumberFormat.
|
NumberFormat(java.lang.String fmtStr)
Creates a new NumberFormat, with settings given by the supplied format
string.
|
Modifier and Type | Method and Description |
---|---|
boolean |
equals(NumberFormat fmt)
Returns true if this NumberFormat is identical in function (i.e., will
produce the same output) as the NumberFormat supplied as an argument.
|
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(float x)
Formats a float into a string.
|
java.lang.StringBuffer |
format(float x,
java.lang.StringBuffer sbuf)
Formats a float 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 . |
static java.lang.String |
formatHex(int num) |
int |
getConversionChar()
Returns the conversion character for this format.
|
int |
getFieldWidth()
Returns the field width associated with this format.
|
int |
getIndentColumn() |
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 |
incIndentColumn(int inc) |
java.lang.String |
indent() |
boolean |
isFloatingPoint()
Returns true if this number format corresponds to one
of the floating point formats ('eEfgaA')
|
boolean |
isFullPrecisionDouble()
Returns true if this number format corresponds to '%g'.
|
void |
set(NumberFormat fmt)
Sets this formatter to have the same settings as the formatter supplied by
the argument.
|
void |
set(java.lang.String fmtStr)
Sets the format characteristics according to the supplied String.
|
void |
setAlternate(boolean enable)
Enables 'alternate' formatting.
|
void |
setFieldWidth(int w)
Sets the field width associated with this format.
|
void |
setIndentColumn(int col) |
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.
|
java.lang.String |
toString()
Returns a string representation of this NumberFormat.
|
public NumberFormat()
%g
.public NumberFormat(java.lang.String fmtStr)
set
.fmtStr
- Format stringjava.lang.IllegalArgumentException
- Malformed format stringset(String)
public NumberFormat(NumberFormat fmt)
fmt
- formatter to be copiesset(NumberFormat)
public void setFieldWidth(int w)
w
- field widthjava.lang.IllegalArgumentException
- Negative field widthgetFieldWidth()
,
set(java.lang.String)
public int getFieldWidth()
setFieldWidth(int)
,
set(java.lang.String)
public void setAlternate(boolean enable)
enable
- if true, enables 'alternate' formattingisAlternate()
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 int getConversionChar()
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 a different behavior
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 stringjava.lang.IllegalArgumentException
- Malformed format stringpublic java.lang.String format(double x)
x
- value to be formattedpublic java.lang.StringBuffer format(double x, java.lang.StringBuffer sbuf)
StringBuffer
.x
- value to be formattedsbuf
- buffer to place the result inpublic java.lang.String format(float x)
x
- value to be formattedpublic java.lang.StringBuffer format(float x, java.lang.StringBuffer sbuf)
StringBuffer
.x
- value to be formattedsbuf
- buffer to place the result inpublic java.lang.String format(int x)
x
- value to be formattedpublic java.lang.StringBuffer format(int x, java.lang.StringBuffer sbuf)
StringBuffer
.x
- value to be formattedsbuf
- buffer to place the result inpublic java.lang.String format(long x)
x
- value to be formattedpublic java.lang.StringBuffer format(long x, java.lang.StringBuffer sbuf)
StringBuffer
.x
- value to be formattedsbuf
- buffer to place the result inpublic void setIndentColumn(int col)
public void incIndentColumn(int inc)
public int getIndentColumn()
public java.lang.String indent()
public java.lang.String toString()
set
, this string should specify a NumberFormat
identical to this one.toString
in class java.lang.Object
public boolean isFullPrecisionDouble()
public boolean isFloatingPoint()
public void set(NumberFormat fmt)
fmt
- formatter to be copiedpublic boolean equals(NumberFormat fmt)
fmt
- NumberFormat to compare withpublic static java.lang.String formatHex(int num)