java.text

Class DecimalFormat

Implemented Interfaces:
Cloneable, Serializable

public class DecimalFormat
extends NumberFormat

See Also:
Serialized Form

Nested Class Summary

Nested classes/interfaces inherited from class java.text.NumberFormat

NumberFormat.Field

Nested classes/interfaces inherited from class java.text.Format

Format.Field

Field Summary

Fields inherited from class java.text.NumberFormat

FRACTION_FIELD, INTEGER_FIELD

Constructor Summary

DecimalFormat()
Constructs a DecimalFormat which uses the default pattern and symbols.
DecimalFormat(String pattern)
Constructs a DecimalFormat which uses the given pattern and the default symbols for formatting and parsing.
DecimalFormat(String pattern, DecimalFormatSymbols symbols)
Constructs a DecimalFormat using the given pattern and formatting symbols.

Method Summary

void
applyLocalizedPattern(String pattern)
void
applyPattern(String pattern)
Object
clone()
boolean
equals(Object obj)
Tests this instance for equality with an arbitrary object.
StringBuffer
format(double number, StringBuffer dest, FieldPosition fieldPos)
This method formats the specified double and appends it to a StringBuffer.
StringBuffer
format(long number, StringBuffer dest, FieldPosition fieldPos)
This method formats the specified long and appends it to a StringBuffer.
AttributedCharacterIterator
formatToCharacterIterator(Object value)
Currency
getCurrency()
Returns the currency corresponding to the currency symbol stored in the instance of DecimalFormatSymbols used by this DecimalFormat.
DecimalFormatSymbols
getDecimalFormatSymbols()
Returns a copy of the symbols used by this instance.
int
getGroupingSize()
int
getMultiplier()
String
getNegativePrefix()
String
getNegativeSuffix()
String
getPositivePrefix()
String
getPositiveSuffix()
int
hashCode()
Returns a hash code for this object.
boolean
isDecimalSeparatorAlwaysShown()
Number
parse(String str, ParsePosition pos)
This method parses the specified string into a Number.
void
setCurrency(Currency currency)
Sets the Currency on the DecimalFormatSymbols used, which also sets the currency symbols on those symbols.
void
setDecimalFormatSymbols(DecimalFormatSymbols newSymbols)
Sets the symbols used by this instance.
void
setDecimalSeparatorAlwaysShown(boolean newValue)
void
setGroupingSize(int groupSize)
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(String newValue)
void
setNegativeSuffix(String newValue)
void
setPositivePrefix(String newValue)
void
setPositiveSuffix(String newValue)
String
toLocalizedPattern()
String
toPattern()

Methods inherited from class java.text.NumberFormat

equals, format, format, format, format, format, getAvailableLocales, getCurrency, getCurrencyInstance, getCurrencyInstance, getInstance, getInstance, getIntegerInstance, getIntegerInstance, getMaximumFractionDigits, getMaximumIntegerDigits, getMinimumFractionDigits, getMinimumIntegerDigits, getNumberInstance, getNumberInstance, getPercentInstance, getPercentInstance, hashCode, isGroupingUsed, isParseIntegerOnly, parse, parse, parseObject, setCurrency, setGroupingUsed, setMaximumFractionDigits, setMaximumIntegerDigits, setMinimumFractionDigits, setMinimumIntegerDigits, setParseIntegerOnly

Methods inherited from class java.text.Format

clone, format, format, formatToCharacterIterator, parseObject, parseObject

Methods inherited from class java.lang.Object

clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

Constructor Details

DecimalFormat

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

DecimalFormat

public DecimalFormat(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:
NullPointerException - if any argument is null.
IllegalArgumentException - if the pattern is invalid.

DecimalFormat

public DecimalFormat(String pattern,
                     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:
NullPointerException - if any argument is null.
IllegalArgumentException - if the pattern is invalid.

Method Details

applyLocalizedPattern

public void applyLocalizedPattern(String pattern)

applyPattern

public void applyPattern(String pattern)

clone

public Object clone()
Overrides:
clone in interface Format

equals

public boolean equals(Object obj)
Tests this instance for equality with an arbitrary object. This method returns true if:
  • obj is not null;
  • obj is an instance of DecimalFormat;
  • this instance and obj have the same attributes;
Overrides:
equals in interface NumberFormat
Parameters:
obj - the object (null permitted).
Returns:
A boolean.

format

public StringBuffer format(double number,
                           StringBuffer dest,
                           FieldPosition fieldPos)
This method formats the specified double and appends it to a StringBuffer.
Overrides:
format in interface NumberFormat
Parameters:
number - The double to format.
Returns:
The StringBuffer with the appended number.

format

public StringBuffer format(long number,
                           StringBuffer dest,
                           FieldPosition fieldPos)
This method formats the specified long and appends it to a StringBuffer.
Overrides:
format in interface NumberFormat
Parameters:
number - The long to format.
Returns:
The StringBuffer with the appended number.

formatToCharacterIterator

public AttributedCharacterIterator formatToCharacterIterator(Object value)
Overrides:
formatToCharacterIterator in interface Format

getCurrency

public Currency getCurrency()
Returns the currency corresponding to the currency symbol stored in the instance of DecimalFormatSymbols used by this DecimalFormat.
Overrides:
getCurrency in interface NumberFormat
Returns:
A new instance of Currency if the currency code matches a known one, null otherwise.

getDecimalFormatSymbols

public 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 String getNegativePrefix()

getNegativeSuffix

public String getNegativeSuffix()

getPositivePrefix

public String getPositivePrefix()

getPositiveSuffix

public String getPositiveSuffix()

hashCode

public int hashCode()
Returns a hash code for this object.
Overrides:
hashCode in interface NumberFormat
Returns:
A hash code.

isDecimalSeparatorAlwaysShown

public boolean isDecimalSeparatorAlwaysShown()

parse

public Number parse(String str,
                    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.
Overrides:
parse in interface NumberFormat
Parameters:
pos - The desired ParsePosition.
Returns:
The parsed Number

setCurrency

public void setCurrency(Currency currency)
Sets the Currency on the DecimalFormatSymbols used, which also sets the currency symbols on those symbols.
Overrides:
setCurrency in interface NumberFormat

setDecimalFormatSymbols

public void setDecimalFormatSymbols(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)
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.
Overrides:
setMaximumFractionDigits in interface NumberFormat
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.
Overrides:
setMaximumIntegerDigits in interface NumberFormat
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.
Overrides:
setMinimumFractionDigits in interface NumberFormat
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.
Overrides:
setMinimumIntegerDigits in interface NumberFormat
Parameters:

setMultiplier

public void setMultiplier(int newValue)

setNegativePrefix

public void setNegativePrefix(String newValue)

setNegativeSuffix

public void setNegativeSuffix(String newValue)

setPositivePrefix

public void setPositivePrefix(String newValue)

setPositiveSuffix

public void setPositiveSuffix(String newValue)

toLocalizedPattern

public String toLocalizedPattern()

toPattern

public String toPattern()

DecimalFormat.java -- Formats and parses numbers Copyright (C) 1999, 2000, 2001, 2003, 2004, 2005 Free Software Foundation, Inc. This file is part of GNU Classpath. GNU Classpath is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2, or (at your option) any later version. GNU Classpath is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with GNU Classpath; see the file COPYING. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. Linking this library statically or dynamically with other modules is making a combined work based on this library. Thus, the terms and conditions of the GNU General Public License cover the whole combination. As a special exception, the copyright holders of this library give you permission to link this library with independent modules to produce an executable, regardless of the license terms of these independent modules, and to copy and distribute the resulting executable under terms of your choice, provided that you also meet, for each linked independent module, the terms and conditions of the license of that module. An independent module is a module which is not derived from or based on this library. If you modify this library, you may extend this exception to your version of the library, but you are not obligated to do so. If you do not wish to do so, delete this exception statement from your version.