GeezerVison is showing docs.oracle.com — NumberFormat (Java Platform SE 8 )
Java™ Platform Standard Ed. 8
compact1, compact2, compact3
java.text
Class NumberFormat
-
-
Nested Class Summary
Nested Classes
| Modifier and Type |
Class |
Description |
static class |
NumberFormat.Field |
Defines constants that are used as attribute keys in the
AttributedCharacterIterator returned
from NumberFormat.formatToCharacterIterator and as
field identifiers in FieldPosition.
|
-
Field Summary
Fields
| Modifier and Type |
Field |
Description |
static int |
FRACTION_FIELD |
Field constant used to construct a FieldPosition object.
|
static int |
INTEGER_FIELD |
Field constant used to construct a FieldPosition object.
|
-
Constructor Summary
Constructors
| Modifier |
Constructor |
Description |
protected |
NumberFormat() |
Sole constructor.
|
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods
| Modifier and Type |
Method |
Description |
Object |
clone() |
Overrides Cloneable.
|
boolean |
equals(Object obj) |
Overrides equals.
|
String |
format(double number) |
Specialization of format.
|
abstract StringBuffer |
format(double number,
StringBuffer toAppendTo,
FieldPosition pos) |
Specialization of format.
|
String |
format(long number) |
Specialization of format.
|
abstract StringBuffer |
format(long number,
StringBuffer toAppendTo,
FieldPosition pos) |
Specialization of format.
|
StringBuffer |
format(Object number,
StringBuffer toAppendTo,
FieldPosition pos) |
Formats a number and appends the resulting text to the given string
buffer.
|
static Locale[] |
getAvailableLocales() |
Returns an array of all locales for which the
get*Instance methods of this class can return
localized instances.
|
Currency |
getCurrency() |
Gets the currency used by this number format when formatting
currency values.
|
static NumberFormat |
getCurrencyInstance() |
Returns a currency format for the current default
FORMAT locale.
|
static NumberFormat |
getCurrencyInstance(Locale inLocale) |
Returns a currency format for the specified locale.
|
static NumberFormat |
getInstance() |
Returns a general-purpose number format for the current default
FORMAT locale.
|
static NumberFormat |
getInstance(Locale inLocale) |
Returns a general-purpose number format for the specified locale.
|
static NumberFormat |
getIntegerInstance() |
Returns an integer number format for the current default
FORMAT locale.
|
static NumberFormat |
getIntegerInstance(Locale inLocale) |
Returns an integer number format for the specified locale.
|
int |
getMaximumFractionDigits() |
Returns the maximum number of digits allowed in the fraction portion of a
number.
|
int |
getMaximumIntegerDigits() |
Returns the maximum number of digits allowed in the integer portion of a
number.
|
int |
getMinimumFractionDigits() |
Returns the minimum number of digits allowed in the fraction portion of a
number.
|
int |
getMinimumIntegerDigits() |
Returns the minimum number of digits allowed in the integer portion of a
number.
|
static NumberFormat |
getNumberInstance() |
Returns a general-purpose number format for the current default
FORMAT locale.
|
static NumberFormat |
getNumberInstance(Locale inLocale) |
Returns a general-purpose number format for the specified locale.
|
static NumberFormat |
getPercentInstance() |
Returns a percentage format for the current default
FORMAT locale.
|
static NumberFormat |
getPercentInstance(Locale inLocale) |
Returns a percentage format for the specified locale.
|
RoundingMode |
getRoundingMode() |
|
int |
hashCode() |
Overrides hashCode.
|
boolean |
isGroupingUsed() |
Returns true if grouping is used in this format.
|
boolean |
isParseIntegerOnly() |
Returns true if this format will parse numbers as integers only.
|
Number |
parse(String source) |
Parses text from the beginning of the given string to produce a number.
|
abstract Number |
parse(String source,
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.
|
Object |
parseObject(String source,
ParsePosition pos) |
Parses text from a string to produce a Number.
|
void |
setCurrency(Currency currency) |
Sets the currency used by this number format when formatting
currency values.
|
void |
setGroupingUsed(boolean newValue) |
Set whether or not grouping will be used in this format.
|
void |
setMaximumFractionDigits(int newValue) |
Sets the maximum number of digits allowed in the fraction portion of a
number.
|
void |
setMaximumIntegerDigits(int newValue) |
Sets the maximum number of digits allowed in the integer portion of a
number.
|
void |
setMinimumFractionDigits(int newValue) |
Sets the minimum number of digits allowed in the fraction portion of a
number.
|
void |
setMinimumIntegerDigits(int newValue) |
Sets the minimum number of digits allowed in the integer portion of a
number.
|
void |
setParseIntegerOnly(boolean value) |
Sets whether or not numbers should be parsed as integers only.
|
void |
setRoundingMode(RoundingMode roundingMode) |
|
-
-
Method Detail
-
format
public abstract StringBuffer format(double number,
StringBuffer toAppendTo,
FieldPosition pos)
Specialization of format.
- Parameters:
number - the double number to format
toAppendTo - the StringBuffer to which the formatted text is to be
appended
pos - the field position
- Returns:
- the formatted StringBuffer
- Throws:
ArithmeticException - if rounding is needed with rounding
mode being set to RoundingMode.UNNECESSARY
- See Also:
Format.format(java.lang.Object)
-
format
public abstract StringBuffer format(long number,
StringBuffer toAppendTo,
FieldPosition pos)
Specialization of format.
- Parameters:
number - the long number to format
toAppendTo - the StringBuffer to which the formatted text is to be
appended
pos - the field position
- Returns:
- the formatted StringBuffer
- Throws:
ArithmeticException - if rounding is needed with rounding
mode being set to RoundingMode.UNNECESSARY
- See Also:
Format.format(java.lang.Object)
-
parse
public abstract Number parse(String source,
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!
- Parameters:
source - the String to parse
parsePosition - the parse position
- Returns:
- the parsed value
- See Also:
isParseIntegerOnly(),
Format.parseObject(java.lang.String, java.text.ParsePosition)
-
isParseIntegerOnly
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.
- Returns:
true if numbers should be parsed as integers only;
false otherwise
-
clone
public Object clone()
Overrides Cloneable.
- Overrides:
clone in class Format
- Returns:
- a clone of this instance.
- See Also:
Cloneable
-
isGroupingUsed
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.
- Returns:
true if grouping is used;
false otherwise
- See Also:
setGroupingUsed(boolean)
-
setMaximumIntegerDigits
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.
- Parameters:
newValue - the maximum number of integer digits to be shown; if
less than zero, then zero is used. The concrete subclass may enforce an
upper limit to this value appropriate to the numeric type being formatted.
- See Also:
getMaximumIntegerDigits()
-
setMinimumIntegerDigits
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
- Parameters:
newValue - the minimum number of integer digits to be shown; if
less than zero, then zero is used. The concrete subclass may enforce an
upper limit to this value appropriate to the numeric type being formatted.
- See Also:
getMinimumIntegerDigits()
-
setMaximumFractionDigits
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.
- Parameters:
newValue - the maximum number of fraction digits to be shown; if
less than zero, then zero is used. The concrete subclass may enforce an
upper limit to this value appropriate to the numeric type being formatted.
- See Also:
getMaximumFractionDigits()
-
setMinimumFractionDigits
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
- Parameters:
newValue - the minimum number of fraction digits to be shown; if
less than zero, then zero is used. The concrete subclass may enforce an
upper limit to this value appropriate to the numeric type being formatted.
- See Also:
getMinimumFractionDigits()
Java™ Platform Standard Ed. 8
Submit a bug or feature For further API reference and developer documentation, see Java SE Documentation. That documentation contains more detailed, developer-targeted descriptions, with conceptual overviews, definitions of terms, workarounds, and working code examples. Copyright © 1993, 2026, Oracle and/or its affiliates. All rights reserved. Use is subject to license terms. Also see the documentation redistribution policy.
|