Fields Summary |
---|
public static final int | INTEGER_FIELDField constant used to construct a FieldPosition object. Signifies that
the position of the integer part of a formatted number should be returned. |
public static final int | FRACTION_FIELDField constant used to construct a FieldPosition object. Signifies that
the position of the fraction part of a formatted number should be returned. |
private static final Hashtable | cachedLocaleDataCache to hold the NumberPatterns of a Locale. |
private static final int | NUMBERSTYLE |
private static final int | CURRENCYSTYLE |
private static final int | PERCENTSTYLE |
private static final int | SCIENTIFICSTYLE |
private static final int | INTEGERSTYLE |
private boolean | groupingUsedTrue if the the grouping (i.e. thousands) separator is used when
formatting and parsing numbers. |
private byte | maxIntegerDigitsThe maximum number of digits allowed in the integer portion of a
number. maxIntegerDigits must be greater than or equal to
minIntegerDigits .
Note: This field exists only for serialization
compatibility with JDK 1.1. In Java platform 2 v1.2 and higher, the new
int field maximumIntegerDigits is used instead.
When writing to a stream, maxIntegerDigits is set to
maximumIntegerDigits or Byte.MAX_VALUE ,
whichever is smaller. When reading from a stream, this field is used
only if serialVersionOnStream is less than 1. |
private byte | minIntegerDigitsThe minimum number of digits allowed in the integer portion of a
number. minimumIntegerDigits must be less than or equal to
maximumIntegerDigits .
Note: This field exists only for serialization
compatibility with JDK 1.1. In Java platform 2 v1.2 and higher, the new
int field minimumIntegerDigits is used instead.
When writing to a stream, minIntegerDigits is set to
minimumIntegerDigits or Byte.MAX_VALUE ,
whichever is smaller. When reading from a stream, this field is used
only if serialVersionOnStream is less than 1. |
private byte | maxFractionDigitsThe maximum number of digits allowed in the fractional portion of a
number. maximumFractionDigits must be greater than or equal to
minimumFractionDigits .
Note: This field exists only for serialization
compatibility with JDK 1.1. In Java platform 2 v1.2 and higher, the new
int field maximumFractionDigits is used instead.
When writing to a stream, maxFractionDigits is set to
maximumFractionDigits or Byte.MAX_VALUE ,
whichever is smaller. When reading from a stream, this field is used
only if serialVersionOnStream is less than 1. |
private byte | minFractionDigitsThe minimum number of digits allowed in the fractional portion of a
number. minimumFractionDigits must be less than or equal to
maximumFractionDigits .
Note: This field exists only for serialization
compatibility with JDK 1.1. In Java platform 2 v1.2 and higher, the new
int field minimumFractionDigits is used instead.
When writing to a stream, minFractionDigits is set to
minimumFractionDigits or Byte.MAX_VALUE ,
whichever is smaller. When reading from a stream, this field is used
only if serialVersionOnStream is less than 1. |
private boolean | parseIntegerOnlyTrue if this format will parse numbers as integers only. |
private int | maximumIntegerDigitsThe maximum number of digits allowed in the integer portion of a
number. maximumIntegerDigits must be greater than or equal to
minimumIntegerDigits . |
private int | minimumIntegerDigitsThe minimum number of digits allowed in the integer portion of a
number. minimumIntegerDigits must be less than or equal to
maximumIntegerDigits . |
private int | maximumFractionDigitsThe maximum number of digits allowed in the fractional portion of a
number. maximumFractionDigits must be greater than or equal to
minimumFractionDigits . |
private int | minimumFractionDigitsThe minimum number of digits allowed in the fractional portion of a
number. minimumFractionDigits must be less than or equal to
maximumFractionDigits . |
static final int | currentSerialVersion |
private int | serialVersionOnStreamDescribes the version of NumberFormat present on the stream.
Possible values are:
- 0 (or uninitialized): the JDK 1.1 version of the stream format.
In this version, the
int fields such as
maximumIntegerDigits were not present, and the byte
fields such as maxIntegerDigits are used instead.
- 1: the 1.2 version of the stream format. The values of the
byte fields such as maxIntegerDigits are ignored,
and the int fields such as maximumIntegerDigits
are used instead.
When streaming out a NumberFormat , the most recent format
(corresponding to the highest allowable serialVersionOnStream )
is always written. |
static final long | serialVersionUID |
Methods Summary |
---|
public java.lang.Object | clone()Overrides Cloneable
NumberFormat other = (NumberFormat) super.clone();
return other;
|
public boolean | equals(java.lang.Object obj)Overrides equals
if (obj == null) {
return false;
}
if (this == obj) {
return true;
}
if (getClass() != obj.getClass()) {
return false;
}
NumberFormat other = (NumberFormat) obj;
return (maximumIntegerDigits == other.maximumIntegerDigits
&& minimumIntegerDigits == other.minimumIntegerDigits
&& maximumFractionDigits == other.maximumFractionDigits
&& minimumFractionDigits == other.minimumFractionDigits
&& groupingUsed == other.groupingUsed
&& parseIntegerOnly == other.parseIntegerOnly);
|
public java.lang.StringBuffer | format(java.lang.Object number, java.lang.StringBuffer toAppendTo, java.text.FieldPosition pos)Formats a number and appends the resulting text to the given string
buffer.
The number can be of any subclass of {@link java.lang.Number}.
This implementation extracts the number's value using
{@link java.lang.Number#longValue()} for all integral type values that
can be converted to long without loss of information,
including BigInteger values with a
{@link java.math.BigInteger#bitLength() bit length} of less than 64,
and {@link java.lang.Number#doubleValue()} for all other types. It
then calls
{@link #format(long,java.lang.StringBuffer,java.text.FieldPosition)}
or {@link #format(double,java.lang.StringBuffer,java.text.FieldPosition)}.
This may result in loss of magnitude information and precision for
BigInteger and BigDecimal values.
if (number instanceof Long || number instanceof Integer ||
number instanceof Short || number instanceof Byte ||
(number instanceof BigInteger &&
((BigInteger)number).bitLength() < 64)) {
return format(((Number)number).longValue(), toAppendTo, pos);
} else if (number instanceof Number) {
return format(((Number)number).doubleValue(), toAppendTo, pos);
} else {
throw new IllegalArgumentException("Cannot format given Object as a Number");
}
|
public final java.lang.String | format(double number)Specialization of format.
return format(number, new StringBuffer(),
DontCareFieldPosition.INSTANCE).toString();
|
public final java.lang.String | format(long number)Specialization of format.
return format(number, new StringBuffer(),
DontCareFieldPosition.INSTANCE).toString();
|
public abstract java.lang.StringBuffer | format(double number, java.lang.StringBuffer toAppendTo, java.text.FieldPosition pos)Specialization of format.
|
public abstract java.lang.StringBuffer | format(long number, java.lang.StringBuffer toAppendTo, java.text.FieldPosition pos)Specialization of format.
|
public static java.util.Locale[] | getAvailableLocales()Returns an array of all locales for which the
get*Instance methods of this class can return
localized instances.
The array returned must contain at least a Locale
instance equal to {@link java.util.Locale#US Locale.US}.
return LocaleData.getAvailableLocales("NumberPatterns");
|
public java.util.Currency | getCurrency()Gets the currency used by this number format when formatting
currency values. The initial value is derived in a locale dependent
way. The returned value may be null if no valid
currency could be determined and no currency has been set using
{@link #setCurrency(java.util.Currency) setCurrency}.
The default implementation throws
UnsupportedOperationException .
throw new UnsupportedOperationException();
|
public static final java.text.NumberFormat | getCurrencyInstance()Returns a currency format for the current default locale.
return getInstance(Locale.getDefault(), CURRENCYSTYLE);
|
public static java.text.NumberFormat | getCurrencyInstance(java.util.Locale inLocale)Returns a currency format for the specified locale.
return getInstance(inLocale, CURRENCYSTYLE);
|
public static final java.text.NumberFormat | getInstance()Returns a general-purpose number format for the current default locale.
This is the same as calling
{@link #getNumberInstance() getNumberInstance()}.
return getInstance(Locale.getDefault(), NUMBERSTYLE);
|
public static java.text.NumberFormat | getInstance(java.util.Locale inLocale)Returns a general-purpose number format for the specified locale.
This is the same as calling
{@link #getNumberInstance(java.util.Locale) getNumberInstance(inLocale)}.
return getInstance(inLocale, NUMBERSTYLE);
|
private static java.text.NumberFormat | getInstance(java.util.Locale desiredLocale, int choice)
/* try the cache first */
String[] numberPatterns = (String[])cachedLocaleData.get(desiredLocale);
if (numberPatterns == null) { /* cache miss */
ResourceBundle resource = LocaleData.getLocaleElements(desiredLocale);
numberPatterns = resource.getStringArray("NumberPatterns");
/* update cache */
cachedLocaleData.put(desiredLocale, numberPatterns);
}
DecimalFormatSymbols symbols = new DecimalFormatSymbols(desiredLocale);
int entry = (choice == INTEGERSTYLE) ? NUMBERSTYLE : choice;
DecimalFormat format = new DecimalFormat(numberPatterns[entry], symbols);
if (choice == INTEGERSTYLE) {
format.setMaximumFractionDigits(0);
format.setDecimalSeparatorAlwaysShown(false);
format.setParseIntegerOnly(true);
} else if (choice == CURRENCYSTYLE) {
format.adjustForCurrencyDefaultFractionDigits();
}
return format;
|
public static final java.text.NumberFormat | getIntegerInstance()Returns an integer number format for the current default locale. The
returned number format is configured to round floating point numbers
to the nearest integer using IEEE half-even rounding (see {@link
java.math.BigDecimal#ROUND_HALF_EVEN ROUND_HALF_EVEN}) for formatting,
and to parse only the integer part of an input string (see {@link
#isParseIntegerOnly isParseIntegerOnly}).
return getInstance(Locale.getDefault(), INTEGERSTYLE);
|
public static java.text.NumberFormat | getIntegerInstance(java.util.Locale inLocale)Returns an integer number format for the specified locale. The
returned number format is configured to round floating point numbers
to the nearest integer using IEEE half-even rounding (see {@link
java.math.BigDecimal#ROUND_HALF_EVEN ROUND_HALF_EVEN}) for formatting,
and to parse only the integer part of an input string (see {@link
#isParseIntegerOnly isParseIntegerOnly}).
return getInstance(inLocale, INTEGERSTYLE);
|
public int | getMaximumFractionDigits()Returns the maximum number of digits allowed in the fraction portion of a
number.
return maximumFractionDigits;
|
public int | getMaximumIntegerDigits()Returns the maximum number of digits allowed in the integer portion of a
number.
return maximumIntegerDigits;
|
public int | getMinimumFractionDigits()Returns the minimum number of digits allowed in the fraction portion of a
number.
return minimumFractionDigits;
|
public int | getMinimumIntegerDigits()Returns the minimum number of digits allowed in the integer portion of a
number.
return minimumIntegerDigits;
|
public static final java.text.NumberFormat | getNumberInstance()Returns a general-purpose number format for the current default locale.
return getInstance(Locale.getDefault(), NUMBERSTYLE);
|
public static java.text.NumberFormat | getNumberInstance(java.util.Locale inLocale)Returns a general-purpose number format for the specified locale.
return getInstance(inLocale, NUMBERSTYLE);
|
public static final java.text.NumberFormat | getPercentInstance()Returns a percentage format for the current default locale.
return getInstance(Locale.getDefault(), PERCENTSTYLE);
|
public static java.text.NumberFormat | getPercentInstance(java.util.Locale inLocale)Returns a percentage format for the specified locale.
return getInstance(inLocale, PERCENTSTYLE);
|
static final java.text.NumberFormat | getScientificInstance()Returns a scientific format for the current default locale.
return getInstance(Locale.getDefault(), SCIENTIFICSTYLE);
|
static java.text.NumberFormat | getScientificInstance(java.util.Locale inLocale)Returns a scientific format for the specified locale.
return getInstance(inLocale, SCIENTIFICSTYLE);
|
public int | hashCode()Overrides hashCode
return maximumIntegerDigits * 37 + maxFractionDigits;
// just enough fields for a reasonable distribution
|
public boolean | isGroupingUsed()Returns true if grouping is used in this format. For example, in the
English locale, with grouping on, the number 1234567 might be formatted
as "1,234,567". The grouping separator as well as the size of each group
is locale dependant and is determined by sub-classes of NumberFormat.
return groupingUsed;
|
public boolean | isParseIntegerOnly()Returns true if this format will parse numbers as integers only.
For example in the English locale, with ParseIntegerOnly true, the
string "1234." would be parsed as the integer value 1234 and parsing
would stop at the "." character. Of course, the exact format accepted
by the parse operation is locale dependant and determined by sub-classes
of NumberFormat.
return parseIntegerOnly;
|
public abstract java.lang.Number | parse(java.lang.String source, java.text.ParsePosition parsePosition)Returns a Long if possible (e.g., within the range [Long.MIN_VALUE,
Long.MAX_VALUE] and with no decimals), otherwise a Double.
If IntegerOnly is set, will stop at a decimal
point (or equivalent; e.g., for rational numbers "1 2/3", will stop
after the 1).
Does not throw an exception; if no object can be parsed, index is
unchanged!
|
public java.lang.Number | parse(java.lang.String source)Parses text from the beginning of the given string to produce a number.
The method may not use the entire text of the given string.
See the {@link #parse(String, ParsePosition)} method for more information
on number parsing.
ParsePosition parsePosition = new ParsePosition(0);
Number result = parse(source, parsePosition);
if (parsePosition.index == 0) {
throw new ParseException("Unparseable number: \"" + source + "\"",
parsePosition.errorIndex);
}
return result;
|
public final java.lang.Object | parseObject(java.lang.String source, java.text.ParsePosition pos)Parses text from a string to produce a Number .
The method attempts to parse text starting at the index given by
pos .
If parsing succeeds, then the index of pos is updated
to the index after the last character used (parsing does not necessarily
use all characters up to the end of the string), and the parsed
number is returned. The updated pos can be used to
indicate the starting point for the next call to this method.
If an error occurs, then the index of pos is not
changed, the error index of pos is set to the index of
the character where the error occurred, and null is returned.
See the {@link #parse(String, ParsePosition)} method for more information
on number parsing.
return parse(source, pos);
|
private void | readObject(java.io.ObjectInputStream stream)First, read in the default serializable data.
Then, if serialVersionOnStream is less than 1, indicating that
the stream was written by JDK 1.1,
set the int fields such as maximumIntegerDigits
to be equal to the byte fields such as maxIntegerDigits ,
since the int fields were not present in JDK 1.1.
Finally, set serialVersionOnStream back to the maximum allowed value so that
default serialization will work properly if this object is streamed out again.
If minimumIntegerDigits is greater than
maximumIntegerDigits or minimumFractionDigits
is greater than maximumFractionDigits , then the stream data
is invalid and this method throws an InvalidObjectException .
In addition, if any of these values is negative, then this method throws
an InvalidObjectException .
stream.defaultReadObject();
if (serialVersionOnStream < 1) {
// Didn't have additional int fields, reassign to use them.
maximumIntegerDigits = maxIntegerDigits;
minimumIntegerDigits = minIntegerDigits;
maximumFractionDigits = maxFractionDigits;
minimumFractionDigits = minFractionDigits;
}
if (minimumIntegerDigits > maximumIntegerDigits ||
minimumFractionDigits > maximumFractionDigits ||
minimumIntegerDigits < 0 || minimumFractionDigits < 0) {
throw new InvalidObjectException("Digit count range invalid");
}
serialVersionOnStream = currentSerialVersion;
|
public void | setCurrency(java.util.Currency currency)Sets the currency used by this number format when formatting
currency values. This does not update the minimum or maximum
number of fraction digits used by the number format.
The default implementation throws
UnsupportedOperationException .
throw new UnsupportedOperationException();
|
public void | setGroupingUsed(boolean newValue)Set whether or not grouping will be used in this format.
groupingUsed = newValue;
|
public void | setMaximumFractionDigits(int newValue)Sets the maximum number of digits allowed in the fraction portion of a
number. maximumFractionDigits must be >= minimumFractionDigits. If the
new value for maximumFractionDigits is less than the current value
of minimumFractionDigits, then minimumFractionDigits will also be set to
the new value.
maximumFractionDigits = Math.max(0,newValue);
if (maximumFractionDigits < minimumFractionDigits) {
minimumFractionDigits = maximumFractionDigits;
}
|
public void | setMaximumIntegerDigits(int newValue)Sets the maximum number of digits allowed in the integer portion of a
number. maximumIntegerDigits must be >= minimumIntegerDigits. If the
new value for maximumIntegerDigits is less than the current value
of minimumIntegerDigits, then minimumIntegerDigits will also be set to
the new value.
maximumIntegerDigits = Math.max(0,newValue);
if (minimumIntegerDigits > maximumIntegerDigits) {
minimumIntegerDigits = maximumIntegerDigits;
}
|
public void | setMinimumFractionDigits(int newValue)Sets the minimum number of digits allowed in the fraction portion of a
number. minimumFractionDigits must be <= maximumFractionDigits. If the
new value for minimumFractionDigits exceeds the current value
of maximumFractionDigits, then maximumIntegerDigits will also be set to
the new value
minimumFractionDigits = Math.max(0,newValue);
if (maximumFractionDigits < minimumFractionDigits) {
maximumFractionDigits = minimumFractionDigits;
}
|
public void | setMinimumIntegerDigits(int newValue)Sets the minimum number of digits allowed in the integer portion of a
number. minimumIntegerDigits must be <= maximumIntegerDigits. If the
new value for minimumIntegerDigits exceeds the current value
of maximumIntegerDigits, then maximumIntegerDigits will also be set to
the new value
minimumIntegerDigits = Math.max(0,newValue);
if (minimumIntegerDigits > maximumIntegerDigits) {
maximumIntegerDigits = minimumIntegerDigits;
}
|
public void | setParseIntegerOnly(boolean value)Sets whether or not numbers should be parsed as integers only.
parseIntegerOnly = value;
|
private void | writeObject(java.io.ObjectOutputStream stream)Write out the default serializable data, after first setting
the byte fields such as maxIntegerDigits to be
equal to the int fields such as maximumIntegerDigits
(or to Byte.MAX_VALUE , whichever is smaller), for compatibility
with the JDK 1.1 version of the stream format.
maxIntegerDigits = (maximumIntegerDigits > Byte.MAX_VALUE) ?
Byte.MAX_VALUE : (byte)maximumIntegerDigits;
minIntegerDigits = (minimumIntegerDigits > Byte.MAX_VALUE) ?
Byte.MAX_VALUE : (byte)minimumIntegerDigits;
maxFractionDigits = (maximumFractionDigits > Byte.MAX_VALUE) ?
Byte.MAX_VALUE : (byte)maximumFractionDigits;
minFractionDigits = (minimumFractionDigits > Byte.MAX_VALUE) ?
Byte.MAX_VALUE : (byte)minimumFractionDigits;
stream.defaultWriteObject();
|