org.dhmp.util
Class DecimalFormat

java.lang.Object
  extended byjava.text.Format
      extended byorg.dhmp.util.NumberFormat
          extended byorg.dhmp.util.DecimalFormat
All Implemented Interfaces:
java.lang.Cloneable, java.io.Serializable

public class DecimalFormat
extends org.dhmp.util.NumberFormat

DecimalFormat used internally. It has modified to support setParseBigDecimal.

Author:
Tom Tromey (tromey@cygnus.com), Andrew John Hughes (gnu_andrew@member.fsf.org) March 4, 1999
See Also:
Serialized Form

Nested Class Summary
static class NumberFormat.Field
           
 
Field Summary
static int FRACTION_FIELD
          This is a constant used to create a FieldPosition object that will return the fractional portion of a formatted number.
static int INTEGER_FIELD
          This is a constant used to create a FieldPosition object that will return the integer portion of a formatted number.
 
Constructor Summary
DecimalFormat()
          Constructs a DecimalFormat which uses the default pattern and symbols.
DecimalFormat(java.lang.String pattern)
          Constructs a DecimalFormat which uses the given pattern and the default symbols for formatting and parsing.
DecimalFormat(java.lang.String pattern, java.text.DecimalFormatSymbols symbols)
          Constructs a DecimalFormat using the given pattern and formatting symbols.
 
Method Summary
 void applyLocalizedPattern(java.lang.String pattern)
           
 void applyPattern(java.lang.String pattern)
           
 java.lang.Object clone()
           
 boolean equals(java.lang.Object obj)
          Tests this instance for equality with an arbitrary object.
 java.lang.String format(double number)
          This method is a specialization of the format method that performs a simple formatting of the specified double number.
 java.lang.StringBuffer format(double number, java.lang.StringBuffer dest, java.text.FieldPosition fieldPos)
          This method formats the specified double and appends it to a StringBuffer.
 java.lang.String format(long number)
          This method is a specialization of the format method that performs a simple formatting of the specified long number.
 java.lang.StringBuffer format(long number, java.lang.StringBuffer dest, java.text.FieldPosition fieldPos)
          This method formats the specified long and appends it to a StringBuffer.
 java.lang.StringBuffer format(java.lang.Object obj, java.lang.StringBuffer sbuf, java.text.FieldPosition pos)
           
 java.text.AttributedCharacterIterator formatToCharacterIterator(java.lang.Object value)
           
static java.util.Locale[] getAvailableLocales()
          This method returns a list of locales for which concrete instances of NumberFormat subclasses may be created.
 java.util.Currency getCurrency()
          Returns the currency corresponding to the currency symbol stored in the instance of DecimalFormatSymbols used by this DecimalFormat.
static org.dhmp.util.NumberFormat getCurrencyInstance()
          This method returns an instance of NumberFormat suitable for formatting and parsing currency values in the default locale.
static org.dhmp.util.NumberFormat getCurrencyInstance(java.util.Locale loc)
          This method returns an instance of NumberFormat suitable for formatting and parsing currency values in the specified locale.
 java.text.DecimalFormatSymbols getDecimalFormatSymbols()
          Returns a copy of the symbols used by this instance.
 int getGroupingSize()
           
static org.dhmp.util.NumberFormat getInstance()
          This method returns a default instance for the default locale.
static org.dhmp.util.NumberFormat getInstance(java.util.Locale loc)
          This method returns a default instance for the specified locale.
static org.dhmp.util.NumberFormat getIntegerInstance()
          This method returns an integer formatting and parsing class for the default locale.
static org.dhmp.util.NumberFormat getIntegerInstance(java.util.Locale locale)
          This method returns an integer formatting and parsing class for the default locale.
 int getMaximumFractionDigits()
          This method returns the maximum number of digits allowed in the fraction portion of a number.
 int getMaximumIntegerDigits()
          This method returns the maximum number of digits allowed in the integer portion of a number.
 int getMinimumFractionDigits()
          This method returns the minimum number of digits allowed in the fraction portion of a number.
 int getMinimumIntegerDigits()
          This method returns the minimum number of digits allowed in the integer portion of a number.
 int getMultiplier()
           
 java.lang.String getNegativePrefix()
           
 java.lang.String getNegativeSuffix()
           
static org.dhmp.util.NumberFormat getNumberInstance()
          This method returns a default instance for the specified locale.
static org.dhmp.util.NumberFormat getNumberInstance(java.util.Locale loc)
          This method returns a general purpose number formatting and parsing class for the default locale.
static org.dhmp.util.NumberFormat getPercentInstance()
          This method returns an instance of NumberFormat suitable for formatting and parsing percentage values in the default locale.
static org.dhmp.util.NumberFormat getPercentInstance(java.util.Locale loc)
          This method returns an instance of NumberFormat suitable for formatting and parsing percentage values in the specified locale.
 java.lang.String getPositivePrefix()
           
 java.lang.String getPositiveSuffix()
           
 int hashCode()
          Returns a hash code for this object.
 boolean isDecimalSeparatorAlwaysShown()
           
 boolean isGroupingUsed()
          This method tests whether or not grouping is in use.
 boolean isParseIntegerOnly()
          This method tests whether or not only integer values should be parsed.
 java.lang.Number parse(java.lang.String sourceStr)
          This method parses the specified string into a Number.
 java.lang.Number parse(java.lang.String str, java.text.ParsePosition pos)
          This method parses the specified string into a Number.
 java.lang.Object parseObject(java.lang.String sourceStr, java.text.ParsePosition pos)
          This method parses the specified string into an Object.
 void setCurrency(java.util.Currency currency)
          Sets the Currency on the DecimalFormatSymbols used, which also sets the currency symbols on those symbols.
 void setDecimalFormatSymbols(java.text.DecimalFormatSymbols newSymbols)
          Sets the symbols used by this instance.
 void setDecimalSeparatorAlwaysShown(boolean newValue)
           
 void setGroupingSize(int groupSize)
           
 void setGroupingUsed(boolean newValue)
          This method sets the grouping behavior of this formatter.
 void setMaximumFractionDigits(int newValue)
          This method sets the maximum number of digits allowed in the fraction portion of a number to the specified value.
 void setMaximumIntegerDigits(int newValue)
          This method sets the maximum number of digits allowed in the integer portion of a number to the specified value.
 void setMinimumFractionDigits(int newValue)
          This method sets the minimum number of digits allowed in the fraction portion of a number to the specified value.
 void setMinimumIntegerDigits(int newValue)
          This method sets the minimum number of digits allowed in the integer portion of a number to the specified value.
 void setMultiplier(int newValue)
           
 void setNegativePrefix(java.lang.String newValue)
           
 void setNegativeSuffix(java.lang.String newValue)
           
 void setParseBigDecimal(boolean isBigDecimal)
           
 void setParseIntegerOnly(boolean value)
          This method sets the parsing behavior of this object to parse only integers or not.
 void setPositivePrefix(java.lang.String newValue)
           
 void setPositiveSuffix(java.lang.String newValue)
           
 java.lang.String toLocalizedPattern()
           
 java.lang.String toPattern()
           
 
Methods inherited from class java.text.Format
format, parseObject
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

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.

See Also:
Constant Field Values

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.

See Also:
Constant Field Values
Constructor Detail

DecimalFormat

public DecimalFormat()
Constructs a DecimalFormat which uses the default pattern and symbols.


DecimalFormat

public DecimalFormat(java.lang.String pattern)
Constructs a DecimalFormat which uses the given pattern and the default symbols for formatting and parsing.

Parameters:
pattern - the non-localized pattern to use.
Throws:
java.lang.NullPointerException - if any argument is null.
java.lang.IllegalArgumentException - if the pattern is invalid.

DecimalFormat

public DecimalFormat(java.lang.String pattern,
                     java.text.DecimalFormatSymbols symbols)
Constructs a DecimalFormat using the given pattern and formatting symbols. This construction method is used to give complete control over the formatting process.

Parameters:
pattern - the non-localized pattern to use.
symbols - the set of symbols used for parsing and formatting.
Throws:
java.lang.NullPointerException - if any argument is null.
java.lang.IllegalArgumentException - if the pattern is invalid.
Method Detail

setParseBigDecimal

public void setParseBigDecimal(boolean isBigDecimal)

applyLocalizedPattern

public void applyLocalizedPattern(java.lang.String pattern)

applyPattern

public void applyPattern(java.lang.String pattern)

clone

public java.lang.Object clone()

equals

public boolean equals(java.lang.Object obj)
Tests this instance for equality with an arbitrary object. This method returns true if:

Parameters:
obj - the object (null permitted).
Returns:
A boolean.

format

public java.lang.StringBuffer format(double number,
                                     java.lang.StringBuffer dest,
                                     java.text.FieldPosition fieldPos)
Description copied from class: org.dhmp.util.NumberFormat
This method formats the specified double and appends it to a StringBuffer.

Parameters:
number - The double to format.
dest - The StringBuffer to append the formatted number to.
fieldPos - The desired FieldPosition.
Returns:
The StringBuffer with the appended number.

formatToCharacterIterator

public java.text.AttributedCharacterIterator formatToCharacterIterator(java.lang.Object value)

format

public java.lang.StringBuffer format(long number,
                                     java.lang.StringBuffer dest,
                                     java.text.FieldPosition fieldPos)
Description copied from class: org.dhmp.util.NumberFormat
This method formats the specified long and appends it to a StringBuffer.

Parameters:
number - The long to format.
dest - The StringBuffer to append the formatted number to.
fieldPos - The desired FieldPosition.
Returns:
The StringBuffer with the appended number.

getCurrency

public java.util.Currency getCurrency()
Returns the currency corresponding to the currency symbol stored in the instance of DecimalFormatSymbols used by this DecimalFormat.

Returns:
A new instance of Currency if the currency code matches a known one, null otherwise.

getDecimalFormatSymbols

public java.text.DecimalFormatSymbols getDecimalFormatSymbols()
Returns a copy of the symbols used by this instance.

Returns:
A copy of the symbols.

getGroupingSize

public int getGroupingSize()

getMultiplier

public int getMultiplier()

getNegativePrefix

public java.lang.String getNegativePrefix()

getNegativeSuffix

public java.lang.String getNegativeSuffix()

getPositivePrefix

public java.lang.String getPositivePrefix()

getPositiveSuffix

public java.lang.String getPositiveSuffix()

hashCode

public int hashCode()
Returns a hash code for this object.

Returns:
A hash code.

isDecimalSeparatorAlwaysShown

public boolean isDecimalSeparatorAlwaysShown()

parse

public java.lang.Number parse(java.lang.String str,
                              java.text.ParsePosition pos)
Description copied from class: org.dhmp.util.NumberFormat
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:
str - The string to parse.
pos - The desired ParsePosition.
Returns:
The parsed Number

setCurrency

public void setCurrency(java.util.Currency currency)
Sets the Currency on the DecimalFormatSymbols used, which also sets the currency symbols on those symbols.

Parameters:
currency - The new currency to be used by this number format.

setDecimalFormatSymbols

public void setDecimalFormatSymbols(java.text.DecimalFormatSymbols newSymbols)
Sets the symbols used by this instance. This method makes a copy of the supplied symbols.

Parameters:
newSymbols - the symbols (null not permitted).

setDecimalSeparatorAlwaysShown

public void setDecimalSeparatorAlwaysShown(boolean newValue)

setGroupingSize

public void setGroupingSize(int groupSize)

setMaximumFractionDigits

public void setMaximumFractionDigits(int newValue)
Description copied from class: org.dhmp.util.NumberFormat
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:
newValue - The new maximum fraction digits value.

setMaximumIntegerDigits

public void setMaximumIntegerDigits(int newValue)
Description copied from class: org.dhmp.util.NumberFormat
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:
newValue - The new maximum integer digits value.

setMinimumFractionDigits

public void setMinimumFractionDigits(int newValue)
Description copied from class: org.dhmp.util.NumberFormat
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:
newValue - The new minimum fraction digits value.

setMinimumIntegerDigits

public void setMinimumIntegerDigits(int newValue)
Description copied from class: org.dhmp.util.NumberFormat
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:
newValue - The new minimum integer digits value.

setMultiplier

public void setMultiplier(int newValue)

setNegativePrefix

public void setNegativePrefix(java.lang.String newValue)

setNegativeSuffix

public void setNegativeSuffix(java.lang.String newValue)

setPositivePrefix

public void setPositivePrefix(java.lang.String newValue)

setPositiveSuffix

public void setPositiveSuffix(java.lang.String newValue)

toLocalizedPattern

public java.lang.String toLocalizedPattern()

toPattern

public java.lang.String toPattern()

format

public final java.lang.String format(long number)
This method is a specialization of the format method that performs a simple formatting of the specified long number.

Parameters:
number - The long to format.
Returns:
The formatted number

format

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

getAvailableLocales

public static java.util.Locale[] getAvailableLocales()
This method returns a list of locales for which concrete instances of NumberFormat subclasses may be created.

Returns:
The list of available locales.

getCurrencyInstance

public static final org.dhmp.util.NumberFormat getCurrencyInstance()
This method returns an instance of NumberFormat suitable for formatting and parsing currency values in the default locale.

Returns:
An instance of NumberFormat for handling currencies.

getCurrencyInstance

public static org.dhmp.util.NumberFormat getCurrencyInstance(java.util.Locale loc)
This method returns an instance of NumberFormat suitable for formatting and parsing currency values in the specified locale.

Returns:
An instance of NumberFormat for handling currencies.

getInstance

public static final org.dhmp.util.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:
An instance of the default NumberFormat class.

getInstance

public static org.dhmp.util.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:
loc - The desired locale.
Returns:
An instance of the default NumberFormat class.

getMaximumFractionDigits

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

Returns:
The maximum number of digits allowed in the fraction portion of a number.

getMaximumIntegerDigits

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

Returns:
The maximum number of digits allowed in the integer portion of a number.

getMinimumFractionDigits

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

Returns:
The minimum number of digits allowed in the fraction portion of a number.

getMinimumIntegerDigits

public int getMinimumIntegerDigits()
This method returns the minimum number of digits allowed in the integer portion of a number.

Returns:
The minimum number of digits allowed in the integer portion of a number.

getNumberInstance

public static final org.dhmp.util.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:
An instance of the default NumberFormat class.

getNumberInstance

public static org.dhmp.util.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.

Returns:
An instance of a generic number formatter for the default locale.

getIntegerInstance

public static final org.dhmp.util.NumberFormat getIntegerInstance()
This method returns an integer 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.

Returns:
An instance of an integer number formatter for the default locale.
Since:
1.4

getIntegerInstance

public static org.dhmp.util.NumberFormat getIntegerInstance(java.util.Locale locale)
This method returns an integer 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:
locale - the desired locale.
Returns:
An instance of an integer number formatter for the desired locale.
Since:
1.4

getPercentInstance

public static final org.dhmp.util.NumberFormat getPercentInstance()
This method returns an instance of NumberFormat suitable for formatting and parsing percentage values in the default locale.

Returns:
An instance of NumberFormat for handling percentages.

getPercentInstance

public static org.dhmp.util.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:
loc - The desired locale.
Returns:
An instance of NumberFormat for handling percentages.

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:
true if grouping is enabled, false otherwise.

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:
true if only integers are parsed, false otherwise.

parse

public java.lang.Number parse(java.lang.String sourceStr)
                       throws java.text.ParseException
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:
sourceStr - The string to parse.
Returns:
The parsed Number
Throws:
java.text.ParseException - If no number can be parsed.

parseObject

public final java.lang.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:
sourceStr - The string to parse.
pos - The desired ParsePosition.
Returns:
The parsed Object

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:
newValue - true to enable grouping, false to disable it.

setParseIntegerOnly

public void setParseIntegerOnly(boolean value)
This method sets the parsing behavior of this object to parse only integers or not.

Parameters:
value - true to parse only integers, false otherwise.

format

public final java.lang.String format(double number)
This method is a specialization of the format method that performs a simple formatting of the specified double number.

Parameters:
number - The double to format.
Returns:
The formatted number