|
Chapter 16 The java.text Package |
|
NumberFormat
Name
NumberFormat
- Class Name:
-
java.text.NumberFormat
- Superclass:
-
java.text.Format
- Immediate Subclasses:
-
java.text.ChoiceFormat,
java.text.DecimalFormat
- Interfaces Implemented:
-
java.lang.Cloneable
- Availability:
-
New as of JDK 1.1
The NumberFormat class formats
and parses numbers in a locale-specific manner. NumberFormat is an abstract
class, but it provides factory methods that return useful instances of
NumberFormat subclasses. These factory methods come in three groups:
- The getCurrencyInstance()
methods return objects that format and parse currency values.
- The getNumberInstance()
methods return objects that format and parse normal numbers.
- The getPercentInstance()
methods return objects that format percentage values.
For example, to format a number as an Italian currency value, you can use
this code:
double salary = 1234.56;
System.out.println
(NumberFormat.getCurrencyInstance(Locale.ITALY).format(salary));
This produces the following output:
The NumberFormat class defines
two field constants that represent the integer and fractional fields in
a formatted number. These field constants create FieldPosition
objects.
public abstract class java.text.NumberFormat extends java.text.Format
implements java.lang.Cloneable {
// Constants
public static final int FRACTION_FIELD;
public static final int INTEGER_FIELD;
// Class Methods
public static Locale[] getAvailableLocales();
public static final NumberFormat getCurrencyInstance();
public static NumberFormat getCurrencyInstance(Locale inLocale);
public static final NumberFormat getInstance();
public static NumberFormat getInstance(Locale inLocale);
public static final NumberFormat getNumberInstance();
public static NumberFormat getNumberInstance(Locale inLocale);
public static final NumberFormat getPercentInstance();
public static NumberFormat getPercentInstance(Locale inLocale);
// Instance Methods
public Object clone();
public boolean equals(Object obj);
public final String format(double number);
public final String format(long number);
public final StringBuffer format(Object number, StringBuffer toAppendTo,
FieldPosition pos);
public abstract StringBuffer format(double number,
StringBuffer toAppendTo, FieldPosition pos);
public abstract StringBuffer format(long number, StringBuffer toAppendTo,
FieldPosition pos);
public int getMaximumFractionDigits();
public int getMaximumIntegerDigits();
public int getMinimumFractionDigits();
public int getMinimumIntegerDigits();
public int hashCode();
public boolean isGroupingUsed();
public boolean isParseIntegerOnly();
public Number parse(String text);
public abstract Number parse(String text, ParsePosition parsePosition);
public final Object parseObject(String source,
ParsePosition parsePosition);
public void setGroupingUsed(boolean newValue);
public void setMaximumFractionDigits(int newValue);
public void setMaximumIntegerDigits(int newValue);
public void setMinimumFractionDigits(int newValue);
public void setMinimumIntegerDigits(int newValue);
public void setParseIntegerOnly(boolean value);
}
- Description
-
A field constant that represents the fractional
part of the number.
- Description
-
A field constant that represents the integer
part of the number.
- Returns
-
An array of Locale objects.
- Description
-
This method returns an array of the Locale
objects for which this class can create NumberFormat
objects.
- Returns
-
A NumberFormat appropriate
for the default Locale that
formats currency values.
- Description
-
This method creates a NumberFormat
that formats and parses currency values in the default
locale.
- Parameters
-
- inLocale
-
The Locale to use.
- Returns
-
A NumberFormat appropriate
for the given Locale that formats
currency values.
- Description
-
This method creates a NumberFormat
that formats and parses currency values in the given
locale.
- Returns
-
A default NumberFormat appropriate
for the default Locale.
- Description
-
This method creates a default NumberFormat
that formats and parses values in the default locale.
- Parameters
-
- inLocale
-
The Locale to use.
- Returns
-
A default NumberFormat appropriate
for the given Locale.
- Description
-
This method creates a NumberFormat
that formats and parses values in the given locale.
- Returns
-
A NumberFormat appropriate
for the default Locale that
formats normal numbers.
- Description
-
This method creates a NumberFormat
that formats and parses number values in the default
locale.
- Parameters
-
- inLocale
-
The Locale to use.
- Returns
-
A NumberFormat appropriate
for the given Locale that formats
normal numbers.
- Description
-
This method creates a NumberFormat
that formats and parses number values in the given
locale.
- Returns
-
A NumberFormat appropriate
for the default Locale that
formats percentage values.
- Description
-
This method creates a NumberFormat
that formats and parses percent values in the default
locale.
- Parameters
-
- inLocale
-
The Locale to use.
- Returns
-
A NumberFormat appropriate
for the given Locale that formats
percentage values.
- Description
-
This method creates a NumberFormat
that formats and parses percent values in the given
locale.
- Returns
-
A copy of this NumberFormat.
- Overrides
-
Format.clone()
- Description
-
This method creates a copy of this NumberFormat
and returns it.
- Parameters
-
- obj
-
The object to be compared with this object.
- Returns
-
true if the objects are equal;
false if they are not.
- Overrides
-
Object.equals()
- Description
-
This method returns true if
obj is an instance of NumberFormat
and is equivalent to this NumberFormat.
- Parameters
-
- number
-
The double value to be formatted.
- Returns
-
A string that contains a formatted representation of the value.
- Description
-
This method formats the given number and returns the result as a string.
- Parameters
-
- number
-
The long value to be formatted.
- Returns
-
A string that contains a formatted representation of the value.
- Description
-
This method formats the given number and returns the result as a string.
- Parameters
-
- number
-
The object to be formatted.
- toAppendTo
-
A StringBuffer
on which to append the formatted information.
- pos
-
A number field.
- Returns
-
The given buffer toAppendTo
with the formatted representation of the object appended to it.
- Overrides
-
Format.format(Object,
StringBuffer, FieldPosition)
- Description
-
This method formats the given object and appends the result to the given
StringBuffer. If pos
refers to one of the number fields, its beginning and ending indices are
filled with the beginning and ending positions of the given field in the
resulting formatted string.
- Parameters
-
- number
-
The double
value to be formatted.
- toAppendTo
-
A StringBuffer
on which to append the formatted information.
- pos
-
A number field.
- Returns
-
The given buffer toAppendTo
with the formatted representation of the object appended to it.
- Description
-
This method formats the given number and appends the result to the given
StringBuffer. If pos
refers to one of the number fields, its beginning and ending indices are
filled with the beginning and ending positions of the given field in the
resulting formatted string.
- Parameters
-
- number
-
The long
value to be formatted.
- toAppendTo
-
A StringBuffer
on which to append the formatted information.
- pos
-
A number field.
- Returns
-
The given buffer toAppendTo
with the formatted representation of the object appended to it.
- Description
-
This method formats the given number and appends the result to the given
StringBuffer. If pos
refers to one of the number fields, its beginning and ending indices are
filled with the beginning and ending positions of the given field in the
resulting formatted string.
- Returns
-
The maximum number of digits allowed in the fraction
portion.
- Description
-
This method returns the maximum number of digits that can be in the fraction
part of the number.
- Returns
-
The maximum number of digits allowed in the integer
portion.
- Description
-
This method returns the maximum number of digits that can be in the integer
part of the number.
- Returns
-
The minimum number of digits allowed in the fraction
portion.
- Description
-
This method returns the minimum number of digits that can be in the fraction
part of the number.
- Returns
-
The minimum number of digits allowed in the integer
portion.
- Description
-
This method returns the minimum number of digits that can be in the integer
part of the number.
- Returns
-
A hashcode for this object.
- Overrides
-
Object.hashCode()
- Description
-
This method returns a hashcode for this NumberFormat.
- Returns
-
A boolean value that indicates
whether or not this NumberFormat
uses a grouping character to break up long sequences of digits in the integer
part of a number.
- Description
-
This method returns true if
this NumberFormat uses a grouping
character. For example, it is common in the United States to use a comma
as a grouping character: 1,234.56.
- Returns
-
A boolean value that indicates
whether or not this NumberFormat
parses only integers.
- Description
-
This method returns true if
this NumberFormat parses only
integers.
- Parameters
-
- text
-
The string to be parsed.
- Returns
-
The Number object represented
by the given string.
- Throws
-
- ParseException
-
If the text cannot be parsed as a number.
- Description
-
This method parses a number from the given string, starting from the beginning
of the string.
- Parameters
-
- text
-
The string to be parsed.
- parsePosition
-
A ParsePosition
object that specifies a position in the string.
- Returns
-
The Number object represented
by the text starting at the given position.
- Description
-
This method parses a number from the given string, starting from the given
position. After the string has been parsed, the given ParsePosition
object is updated so that its index is after the parsed text.
- Parameters
-
- source
-
The string to be parsed.
- parsePosition
-
A ParsePosition
object that specifies a position in the string.
- Returns
-
The object represented by the text starting at the given position.
- Overrides
-
Format.parseObject(String,
ParsePosition)
- Description
-
This method parses a number from the given string, starting from the given
position. After the string has been parsed, the given ParsePosition
object is updated so that its index is after the parsed text.
- Parameters
-
- newValue
-
The new grouping flag.
- Description
-
This method sets whether or not this NumberFormat
uses a grouping character to break up long sequences of digits in the integer
part of a number. For example, it is common in the United States to use
a comma as a grouping character: 1,234.56.
- Parameters
-
- newValue
-
The new maximum number of fraction digits.
- Description
-
This method sets the maximum number of digits that may be present
in the fraction part of the number. The maximum value must be greater than
the minimum number of fraction digits allowed. If the value is less than
the current minimum, the minimum is also set to this value.
- Parameters
-
- newValue
-
The new maximum number of integer digits.
- Description
-
This method sets the maximum number of digits that may be present
in the integer part of the number. The maximum value must be greater than
the minimum number of integer digits allowed. If the value is less than
the current minimum, the minimum is also set to this value.
- Parameters
-
- newValue
-
The new minimum number of fraction digits.
- Description
-
This method sets the minimum number of digits that may be present
in the fraction part of the number. The minimum value must be less than
the maximum number of fraction digits allowed. If the value is greater
than the current maximum, the maximum is also set to this value.
- Parameters
-
- newValue
-
The new minimum number of integer digits.
- Description
-
This method sets the minimum number of digits that may be present
in the integer part of the number. The minimum value must be less than
the maximum number of integer digits allowed. If the value is greater than
the current maximum, the maximum is also set to this value.
- Parameters
-
- value
-
The new parsing flag.
- Description
-
This method sets whether or not this NumberFormat
parses only integers. If the value is true,
this NumberFormat only parse
integers. Otherwise it parses both the integer and fractional parts of
numbers.
ChoiceFormat,
DecimalFormat,
FieldPosition,
Format,
Number,
ParseException,
ParsePosition,
String,
StringBuffer
|