java.text.NumberFormat
java.text.Format
java.text.ChoiceFormat, java.text.DecimalFormat
java.lang.Cloneable
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:
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:
L 1.234,56
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); }
A field constant that represents the fractional part of the number.
A field constant that represents the integer part of the number.
An array of Locale objects.
This method returns an array of the Locale objects for which this class can create NumberFormat objects.
A NumberFormat appropriate for the default Locale that formats currency values.
This method creates a NumberFormat that formats and parses currency values in the default locale.
The Locale to use.
A NumberFormat appropriate for the given Locale that formats currency values.
This method creates a NumberFormat that formats and parses currency values in the given locale.
A default NumberFormat appropriate for the default Locale.
This method creates a default NumberFormat that formats and parses values in the default locale.
The Locale to use.
A default NumberFormat appropriate for the given Locale.
This method creates a NumberFormat that formats and parses values in the given locale.
A NumberFormat appropriate for the default Locale that formats normal numbers.
This method creates a NumberFormat that formats and parses number values in the default locale.
The Locale to use.
A NumberFormat appropriate for the given Locale that formats normal numbers.
This method creates a NumberFormat that formats and parses number values in the given locale.
A NumberFormat appropriate for the default Locale that formats percentage values.
This method creates a NumberFormat that formats and parses percent values in the default locale.
The Locale to use.
A NumberFormat appropriate for the given Locale that formats percentage values.
This method creates a NumberFormat that formats and parses percent values in the given locale.
A copy of this NumberFormat.
Format.clone()
This method creates a copy of this NumberFormat and returns it.
The object to be compared with this object.
true if the objects are equal; false if they are not.
Object.equals()
This method returns true if obj is an instance of NumberFormat and is equivalent to this NumberFormat.
The double value to be formatted.
A string that contains a formatted representation of the value.
This method formats the given number and returns the result as a string.
The long value to be formatted.
A string that contains a formatted representation of the value.
This method formats the given number and returns the result as a string.
public final StringBuffer format(Object number, StringBuffer toAppendTo, FieldPosition pos)
The object to be formatted.
A StringBuffer on which to append the formatted information.
A number field.
The given buffer toAppendTo with the formatted representation of the object appended to it.
Format.format(Object, StringBuffer, FieldPosition)
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.
public abstract StringBuffer format(double number, StringBuffer toAppendTo, FieldPosition pos)
The double value to be formatted.
A StringBuffer on which to append the formatted information.
A number field.
The given buffer toAppendTo with the formatted representation of the object appended to it.
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.
public abstract StringBuffer format(long number, StringBuffer toAppendTo, FieldPosition pos)
The long value to be formatted.
A StringBuffer on which to append the formatted information.
A number field.
The given buffer toAppendTo with the formatted representation of the object appended to it.
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.
The maximum number of digits allowed in the fraction portion.
This method returns the maximum number of digits that can be in the fraction part of the number.
The maximum number of digits allowed in the integer portion.
This method returns the maximum number of digits that can be in the integer part of the number.
The minimum number of digits allowed in the fraction portion.
This method returns the minimum number of digits that can be in the fraction part of the number.
The minimum number of digits allowed in the integer portion.
This method returns the minimum number of digits that can be in the integer part of the number.
A hashcode for this object.
Object.hashCode()
This method returns a hashcode for this NumberFormat.
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.
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.
A boolean value that indicates whether or not this NumberFormat parses only integers.
This method returns true if this NumberFormat parses only integers.
The string to be parsed.
The Number object represented by the given string.
If the text cannot be parsed as a number.
This method parses a number from the given string, starting from the beginning of the string.
public abstract Number parse(String text, ParsePosition parsePosition)
The string to be parsed.
A ParsePosition object that specifies a position in the string.
The Number object represented by the text starting at the given position.
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.
public final Object parseObject(String source, ParsePosition parsePosition)
The string to be parsed.
A ParsePosition object that specifies a position in the string.
The object represented by the text starting at the given position.
Format.parseObject(String, ParsePosition)
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.
The new grouping flag.
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.
The new maximum number of fraction digits.
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.
The new maximum number of integer digits.
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.
The new minimum number of fraction digits.
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.
The new minimum number of integer digits.
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.
The new parsing flag.
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.
Method | Inherited From | Method | Inherited From |
---|---|---|---|
finalize() |
Object |
format(Object) |
Format |
getClass() |
Object |
notify() |
Object |
notifyAll() |
Object |
parseObject(String) |
Format |
toString() |
Object |
wait() |
Object |
wait(long) |
Object |
wait(long, int) |
Object |
ChoiceFormat, DecimalFormat, FieldPosition, Format, Number, ParseException, ParsePosition, String, StringBuffer