Index (Frames) | Index (No Frames) | Package | Package Tree | Tree
java.text

Class NumberFormat

java.lang.Object
|
+--java.text.Format
   |
   +--java.text.NumberFormat

All Implemented Interfaces:

Cloneable, Serializable, Cloneable


public abstract class NumberFormat

extends Format

implements Cloneable

This is the abstract superclass of all classes which format and parse numeric values such as decimal numbers, integers, currency values, and percentages. These classes perform their parsing and formatting in a locale specific manner, accounting for such items as differing currency symbols and thousands separators.

To create an instance of a concrete subclass of NumberFormat, do not call a class constructor directly. Instead, use one of the static factory methods in this class such as getCurrencyInstance.

Authors:

Field Summary

static intFRACTION_FIELD

This is a constant used to create a FieldPosition object that will return the fractional portion of a formatted number.
static intINTEGER_FIELD

This is a constant used to create a FieldPosition object that will return the integer portion of a formatted number.

Constructor Summary

NumberFormat()

This is a default constructor for use by subclasses.

Method Summary

booleanequals(java.lang.Object obj)

This method tests the specified object for equality against this object.
java.lang.Stringformat(long number)

This method is a specialization of the format method that performs a simple formatting of the specified long number.
java.lang.StringBufferformat(java.lang.Object obj, java.lang.StringBuffer sbuf, java.text.FieldPosition pos)

java.lang.StringBufferformat(double number, java.lang.StringBuffer sbuf, java.text.FieldPosition pos)

This method formats the specified double and appends it to a StringBuffer.
java.lang.StringBufferformat(long number, java.lang.StringBuffer sbuf, java.text.FieldPosition pos)

This method formats the specified long and appends it to a StringBuffer.
java.lang.Stringformat(double number)

This method is a specialization of the format method that performs a simple formatting of the specified double number.
static java.util.Locale[]getAvailableLocales()

This method returns a list of locales for which concrete instances of NumberFormat subclasses may be created.
static java.text.NumberFormatgetCurrencyInstance()

This method returns an instance of NumberFormat suitable for formatting and parsing currency values in the default locale.
static java.text.NumberFormatgetCurrencyInstance(java.util.Locale loc)

This method returns an instance of NumberFormat suitable for formatting and parsing currency values in the specified locale.
static java.text.NumberFormatgetInstance()

This method returns a default instance for the default locale.
static java.text.NumberFormatgetInstance(java.util.Locale loc)

This method returns a default instance for the specified locale.
intgetMaximumFractionDigits()

This method returns the maximum number of digits allowed in the fraction portion of a number.
intgetMaximumIntegerDigits()

This method returns the maximum number of digits allowed in the integer portion of a number.
intgetMinimumFractionDigits()

This method returns the minimum number of digits allowed in the fraction portion of a number.
intgetMinimumIntegerDigits()

This method returns the minimum number of digits allowed in the integer portion of a number.
static java.text.NumberFormatgetNumberInstance()

This method returns a default instance for the specified locale.
static java.text.NumberFormatgetNumberInstance(java.util.Locale loc)

This method returns a general purpose number formatting and parsing class for the default locale.
static java.text.NumberFormatgetPercentInstance()

This method returns an instance of NumberFormat suitable for formatting and parsing percentage values in the default locale.
static java.text.NumberFormatgetPercentInstance(java.util.Locale loc)

This method returns an instance of NumberFormat suitable for formatting and parsing percentage values in the specified locale.
inthashCode()

This method returns a hash value for this object.
booleanisGroupingUsed()

This method tests whether or not grouping is in use.
booleanisParseIntegerOnly()

This method tests whether or not only integer values should be parsed.
java.lang.Numberparse(java.lang.String sourceStr, java.text.ParsePosition pos)

This method parses the specified string into a Number.
java.lang.Numberparse(java.lang.String sourceStr)

This method parses the specified string into a Number.
java.lang.ObjectparseObject(java.lang.String sourceStr, java.text.ParsePosition pos)

This method parses the specified string into an Object.
voidsetGroupingUsed(boolean newValue)

This method sets the grouping behavior of this formatter.
voidsetMaximumFractionDigits(int newValue)

This method sets the maximum number of digits allowed in the fraction portion of a number to the specified value.
voidsetMaximumIntegerDigits(int newValue)

This method sets the maximum number of digits allowed in the integer portion of a number to the specified value.
voidsetMinimumFractionDigits(int newValue)

This method sets the minimum number of digits allowed in the fraction portion of a number to the specified value.
voidsetMinimumIntegerDigits(int newValue)

This method sets the minimum number of digits allowed in the integer portion of a number to the specified value.
voidsetParseIntegerOnly(boolean value)

This method sets the parsing behavior of this object to parse only integers or not.

Field Details

FRACTION_FIELD

public static final int FRACTION_FIELD

This is a constant used to create a FieldPosition object that will return the fractional portion of a formatted number.


INTEGER_FIELD

public static final int INTEGER_FIELD

This is a constant used to create a FieldPosition object that will return the integer portion of a formatted number.


Constructor Details

NumberFormat

public NumberFormat()

This is a default constructor for use by subclasses.


Method Details

equals

public boolean equals(java.lang.Object obj)

This method tests the specified object for equality against this object. This will be true if the following conditions are met:

Since this method does not test much, it is highly advised that concrete subclasses override this method.

Parameters:

Returns:


format

public final String format(double number)

This method is a specialization of the format method that performs a simple formatting of the specified double number.

Parameters:

Returns:


format

public StringBuffer format(double number, java.lang.StringBuffer sbuf, java.text.FieldPosition pos)

This method formats the specified double and appends it to a StringBuffer.

Parameters:

Returns:


format

public final StringBuffer format(java.lang.Object obj, java.lang.StringBuffer sbuf, java.text.FieldPosition pos)

Parameters:


format

public final String format(long number)

This method is a specialization of the format method that performs a simple formatting of the specified long number.

Parameters:

Returns:


format

public StringBuffer format(long number, java.lang.StringBuffer sbuf, java.text.FieldPosition pos)

This method formats the specified long and appends it to a StringBuffer.

Parameters:

Returns:


getAvailableLocales

public static Locale[] getAvailableLocales()

This method returns a list of locales for which concrete instances of NumberFormat subclasses may be created.

Returns:


getCurrencyInstance

public static final NumberFormat getCurrencyInstance()

This method returns an instance of NumberFormat suitable for formatting and parsing currency values in the default locale.

Returns:


getCurrencyInstance

public static NumberFormat getCurrencyInstance(java.util.Locale loc)

This method returns an instance of NumberFormat suitable for formatting and parsing currency values in the specified locale.

Parameters:

Returns:


getInstance

public static final NumberFormat getInstance()

This method returns a default instance for the default locale. This will be a concrete subclass of NumberFormat, but the actual class returned is dependent on the locale.

Returns:


getInstance

public static NumberFormat getInstance(java.util.Locale loc)

This method returns a default instance for the specified locale. This will be a concrete subclass of NumberFormat, but the actual class returned is dependent on the locale.

Parameters:

Returns:


getMaximumFractionDigits

public int getMaximumFractionDigits()

This method returns the maximum number of digits allowed in the fraction portion of a number.

Returns:


getMaximumIntegerDigits

public int getMaximumIntegerDigits()

This method returns the maximum number of digits allowed in the integer portion of a number.

Returns:


getMinimumFractionDigits

public int getMinimumFractionDigits()

This method returns the minimum number of digits allowed in the fraction portion of a number.

Returns:


getMinimumIntegerDigits

public int getMinimumIntegerDigits()

This method returns the minimum number of digits allowed in the integer portion of a number.

Returns:


getNumberInstance

public static final NumberFormat getNumberInstance()

This method returns a default instance for the specified locale. This will be a concrete subclass of NumberFormat, but the actual class returned is dependent on the locale.

Returns:


getNumberInstance

public static NumberFormat getNumberInstance(java.util.Locale loc)

This method returns a general purpose number formatting and parsing class for the default locale. This will be a concrete subclass of NumberFormat, but the actual class returned is dependent on the locale.

Parameters:

Returns:


getPercentInstance

public static final NumberFormat getPercentInstance()

This method returns an instance of NumberFormat suitable for formatting and parsing percentage values in the default locale.

Returns:


getPercentInstance

public static NumberFormat getPercentInstance(java.util.Locale loc)

This method returns an instance of NumberFormat suitable for formatting and parsing percentage values in the specified locale.

Parameters:

Returns:


hashCode

public int hashCode()

This method returns a hash value for this object.

Returns:


isGroupingUsed

public boolean isGroupingUsed()

This method tests whether or not grouping is in use. Grouping is a method of marking separations in numbers, such as thousand separators in the US English locale. The grouping positions and symbols are all locale specific. As an example, with grouping disabled, the number one million would appear as "1000000". With grouping enabled, this number might appear as "1,000,000". (Both of these assume the US English locale).

Returns:


isParseIntegerOnly

public boolean isParseIntegerOnly()

This method tests whether or not only integer values should be parsed. If this class is parsing only integers, parsing stops at the decimal point.

Returns:


parse

public Number parse(java.lang.String sourceStr)

This method parses the specified string into a Number. This will be a Long if possible, otherwise it will be a Double. If no number can be parsed, an exception will be thrown.

Parameters:

Returns:

Throws:


parse

public Number parse(java.lang.String sourceStr, java.text.ParsePosition pos)

This method parses the specified string into a Number. This will be a Long if possible, otherwise it will be a Double. If no number can be parsed, no exception is thrown. Instead, the parse position remains at its initial index.

Parameters:

Returns:


parseObject

public final Object parseObject(java.lang.String sourceStr, java.text.ParsePosition pos)

This method parses the specified string into an Object. This will be a Long if possible, otherwise it will be a Double. If no number can be parsed, no exception is thrown. Instead, the parse position remains at its initial index.

Parameters:

Returns:


setGroupingUsed

public void setGroupingUsed(boolean newValue)

This method sets the grouping behavior of this formatter. Grouping is a method of marking separations in numbers, such as thousand separators in the US English locale. The grouping positions and symbols are all locale specific. As an example, with grouping disabled, the number one million would appear as "1000000". With grouping enabled, this number might appear as "1,000,000". (Both of these assume the US English locale).

Parameters:


setMaximumFractionDigits

public void setMaximumFractionDigits(int newValue)

This method sets the maximum number of digits allowed in the fraction portion of a number to the specified value. If this is less than the current minimum allowed digits, the minimum allowed digits value will be lowered to be equal to the new maximum allowed digits value.

Parameters:


setMaximumIntegerDigits

public void setMaximumIntegerDigits(int newValue)

This method sets the maximum number of digits allowed in the integer portion of a number to the specified value. If this is less than the current minimum allowed digits, the minimum allowed digits value will be lowered to be equal to the new maximum allowed digits value.

Parameters:


setMinimumFractionDigits

public void setMinimumFractionDigits(int newValue)

This method sets the minimum number of digits allowed in the fraction portion of a number to the specified value. If this is greater than the current maximum allowed digits, the maximum allowed digits value will be raised to be equal to the new minimum allowed digits value.

Parameters:


setMinimumIntegerDigits

public void setMinimumIntegerDigits(int newValue)

This method sets the minimum number of digits allowed in the integer portion of a number to the specified value. If this is greater than the current maximum allowed digits, the maximum allowed digits value will be raised to be equal to the new minimum allowed digits value.

Parameters:


setParseIntegerOnly

public void setParseIntegerOnly(boolean value)

This method sets the parsing behavior of this object to parse only integers or not.

Parameters: