加载中...
The Wayback Machine - https://sup1a9wrlpyh5li9ro.vcoronado.top/web/20090226040510/http://developer.android.com:80/reference/java/text/DecimalFormat.html
public class

DecimalFormat

extends NumberFormat
java.lang.Object
   ↳ java.text.Format
     ↳ java.text.NumberFormat
       ↳ java.text.DecimalFormat

Class Overview

DecimalFormat is used to format and parse numbers, both integers and fractions, based on a pattern. The pattern characters used can be either localized or non-localized.

Summary

[Expand]
Inherited Constants
From class java.text.NumberFormat
Public Constructors
DecimalFormat()
Constructs a new DecimalFormat for formatting and parsing numbers for the default Locale.
DecimalFormat(String pattern)
Constructs a new DecimalFormat using the specified non-localized pattern and the DecimalFormatSymbols for the default Locale.
DecimalFormat(String pattern, DecimalFormatSymbols value)
Constructs a new DecimalFormat using the specified non-localized pattern and DecimalFormatSymbols.
Public Methods
void applyLocalizedPattern(String pattern)
Changes the pattern of this DecimalFormat to the specified pattern which uses localized pattern characters.
void applyPattern(String pattern)
Changes the pattern of this SimpleDateFormat to the specified pattern which uses non-localized pattern characters.
Object clone()
Returns a new instance of DecimalFormat with the same pattern and properties as this DecimalFormat.
boolean equals(Object object)
Compares the specified object to this DecimalFormat and answer if they are equal.
final StringBuffer format(Object number, StringBuffer toAppendTo, FieldPosition pos)
Formats the number into the specified StringBuffer using the pattern of this DecimalFormat.
StringBuffer format(double value, StringBuffer buffer, FieldPosition position)
Formats the double value into the specified StringBuffer using the pattern of this DecimalFormat.
StringBuffer format(long value, StringBuffer buffer, FieldPosition position)
Formats the long value into the specified StringBuffer using the pattern of this DecimalFormat.
AttributedCharacterIterator formatToCharacterIterator(Object object)
Formats the specified object using the rules of this DecimalNumberFormat and returns an AttributedCharacterIterator with the formatted number and attributes.
Currency getCurrency()
Returns the currency used by this decimal format.
DecimalFormatSymbols getDecimalFormatSymbols()
Returns the DecimalFormatSymbols used by this DecimalFormat.
int getGroupingSize()
Returns the number of digits grouped together by the grouping separator.
int getMultiplier()
Returns the multiplier which is applied to the number before formatting or after parsing.
String getNegativePrefix()
Returns the prefix which is formatted or parsed before a negative number.
String getNegativeSuffix()
Returns the suffix which is formatted or parsed after a negative number.
String getPositivePrefix()
Returns the prefix which is formatted or parsed before a positive number.
String getPositiveSuffix()
Returns the suffix which is formatted or parsed after a positive number.
int hashCode()
Returns an integer hash code for the receiver.
boolean isDecimalSeparatorAlwaysShown()
Returns whether the decimal separator is shown when there are no fractional digits.
boolean isGroupingUsed()
This value indicates whether grouping will be used in this format.
boolean isParseBigDecimal()
This value indicates whether the return object of the parse operation will be of type BigDecimal.
boolean isParseIntegerOnly()
Returns true if this DecimalFormat's all resulting number will be of type java.lang.Integer
Number parse(String string, ParsePosition position)
Parse a Long or Double from the specified String starting at the index specified by the ParsePosition.
void setCurrency(Currency currency)
Sets the currency used by this decimal format.
void setDecimalFormatSymbols(DecimalFormatSymbols value)
Sets the DecimalFormatSymbols used by this DecimalFormat.
void setDecimalSeparatorAlwaysShown(boolean value)
Sets whether the decimal separator is shown when there are no fractional digits.
void setGroupingSize(int value)
Sets the number of digits grouped together by the grouping separator.
void setGroupingUsed(boolean value)
Sets whether or not grouping will be used in this format.
void setMaximumFractionDigits(int value)
Sets the maximum number of fraction digits that are printed when formatting.
void setMaximumIntegerDigits(int value)
Sets the maximum number of integer digits that are printed when formatting.
void setMinimumFractionDigits(int value)
Sets the minimum number of fraction digits that are printed when formatting.
void setMinimumIntegerDigits(int value)
Sets the minimum number of integer digits that are printed when formatting.
void setMultiplier(int value)
Sets the multiplier which is applied to the number before formatting or after parsing.
void setNegativePrefix(String value)
Sets the prefix which is formatted or parsed before a negative number.
void setNegativeSuffix(String value)
Sets the suffix which is formatted or parsed after a negative number.
void setParseBigDecimal(boolean newValue)
Let users change the behavior of a DecimalFormat, If set to true all the returned objects will be of type BigDecimal
void setParseIntegerOnly(boolean value)
When DecimalFormat is used to parsing, and this value is set to true, then all the resulting number will be of type java.lang.Integer.
void setPositivePrefix(String value)
Sets the prefix which is formatted or parsed before a positive number.
void setPositiveSuffix(String value)
Sets the suffix which is formatted or parsed after a positive number.
String toLocalizedPattern()
Returns the pattern of this DecimalFormat using localized pattern characters.
String toPattern()
Returns the pattern of this DecimalFormat using non-localized pattern characters.
[Expand]
Inherited Methods
From class java.text.NumberFormat
From class java.text.Format
From class java.lang.Object

Public Constructors

public DecimalFormat ()

Constructs a new DecimalFormat for formatting and parsing numbers for the default Locale.

public DecimalFormat (String pattern)

Constructs a new DecimalFormat using the specified non-localized pattern and the DecimalFormatSymbols for the default Locale.

Parameters
pattern the non-localized pattern
Throws
IllegalArgumentException when the pattern cannot be parsed

public DecimalFormat (String pattern, DecimalFormatSymbols value)

Constructs a new DecimalFormat using the specified non-localized pattern and DecimalFormatSymbols.

Parameters
pattern the non-localized pattern
value the DecimalFormatSymbols
Throws
IllegalArgumentException when the pattern cannot be parsed

Public Methods

public void applyLocalizedPattern (String pattern)

Changes the pattern of this DecimalFormat to the specified pattern which uses localized pattern characters.

Parameters
pattern the localized pattern
Throws
IllegalArgumentException when the pattern cannot be parsed

public void applyPattern (String pattern)

Changes the pattern of this SimpleDateFormat to the specified pattern which uses non-localized pattern characters.

Parameters
pattern the non-localized pattern
Throws
IllegalArgumentException when the pattern cannot be parsed

public Object clone ()

Returns a new instance of DecimalFormat with the same pattern and properties as this DecimalFormat.

Returns
  • a shallow copy of this DecimalFormat
See Also

public boolean equals (Object object)

Compares the specified object to this DecimalFormat and answer if they are equal. The object must be an instance of DecimalFormat with the same pattern and properties.

Parameters
object the object to compare with this object
Returns
  • true if the specified object is equal to this DecimalFormat, false otherwise
See Also

public final StringBuffer format (Object number, StringBuffer toAppendTo, FieldPosition pos)

Formats the number into the specified StringBuffer using the pattern of this DecimalFormat. If the field specified by the FieldPosition is formatted, set the begin and end index of the formatted field in the FieldPosition.

Parameters
number the object to format
toAppendTo the StringBuffer
pos the FieldPosition
Returns
  • the StringBuffer parameter buffer
Throws
IllegalArgumentException if the given number is not instance of Number

public StringBuffer format (double value, StringBuffer buffer, FieldPosition position)

Formats the double value into the specified StringBuffer using the pattern of this DecimalFormat. If the field specified by the FieldPosition is formatted, set the begin and end index of the formatted field in the FieldPosition.

Parameters
value the double to format
buffer the StringBuffer
position the FieldPosition
Returns
  • the StringBuffer parameter buffer

public StringBuffer format (long value, StringBuffer buffer, FieldPosition position)

Formats the long value into the specified StringBuffer using the pattern of this DecimalFormat. If the field specified by the FieldPosition is formatted, set the begin and end index of the formatted field in the FieldPosition.

Parameters
value the long to format
buffer the StringBuffer
position the FieldPosition
Returns
  • the StringBuffer parameter buffer

public AttributedCharacterIterator formatToCharacterIterator (Object object)

Formats the specified object using the rules of this DecimalNumberFormat and returns an AttributedCharacterIterator with the formatted number and attributes.

Parameters
object the object to format
Returns
  • an AttributedCharacterIterator with the formatted number and attributes
Throws
NullPointerException when the object is null
IllegalArgumentException when the object cannot be formatted by this Format

public Currency getCurrency ()

Returns the currency used by this decimal format.

Returns
  • currency of DecimalFormatSymbols used by this decimal format
See Also

public DecimalFormatSymbols getDecimalFormatSymbols ()

Returns the DecimalFormatSymbols used by this DecimalFormat.

Returns
  • a DecimalFormatSymbols

public int getGroupingSize ()

Returns the number of digits grouped together by the grouping separator.

Returns
  • the number of digits grouped together

public int getMultiplier ()

Returns the multiplier which is applied to the number before formatting or after parsing.

Returns
  • the multiplier

public String getNegativePrefix ()

Returns the prefix which is formatted or parsed before a negative number.

Returns
  • the negative prefix

public String getNegativeSuffix ()

Returns the suffix which is formatted or parsed after a negative number.

Returns
  • the negative suffix

public String getPositivePrefix ()

Returns the prefix which is formatted or parsed before a positive number.

Returns
  • the positive prefix

public String getPositiveSuffix ()

Returns the suffix which is formatted or parsed after a positive number.

Returns
  • the positive suffix

public int hashCode ()

Returns an integer hash code for the receiver. Objects which are equal answer the same value for this method.

Returns
  • the receiver's hash
See Also

public boolean isDecimalSeparatorAlwaysShown ()

Returns whether the decimal separator is shown when there are no fractional digits.

Returns
  • true if the decimal separator should always be formatted, false otherwise

public boolean isGroupingUsed ()

This value indicates whether grouping will be used in this format.

Returns
  • true if grouping is used,false otherwise.

public boolean isParseBigDecimal ()

This value indicates whether the return object of the parse operation will be of type BigDecimal. This value will default to false.

Returns
  • true and parse will always return BigDecimals, false and the type of the result will be Long or Double.

public boolean isParseIntegerOnly ()

Returns true if this DecimalFormat's all resulting number will be of type java.lang.Integer

Returns
  • true if this DecimalFormat's all resulting number will be of type java.lang.Integer

public Number parse (String string, ParsePosition position)

Parse a Long or Double from the specified String starting at the index specified by the ParsePosition. If the string is successfully parsed, the index of the ParsePosition is updated to the index following the parsed text.

Parameters
string the String to parse
position the ParsePosition, updated on return with the index following the parsed text, or on error the index is unchanged and the error index is set to the index where the error occurred
Returns
  • a Long or Double resulting from the parse, or null if there is an error. The result will be a Long if the parsed number is an integer in the range of a long, otherwise the result is a Double.

public void setCurrency (Currency currency)

Sets the currency used by this decimal format. The min and max fraction digits remain the same.

Parameters
currency the new Currency

public void setDecimalFormatSymbols (DecimalFormatSymbols value)

Sets the DecimalFormatSymbols used by this DecimalFormat.

Parameters
value the DecimalFormatSymbols

public void setDecimalSeparatorAlwaysShown (boolean value)

Sets whether the decimal separator is shown when there are no fractional digits.

Parameters
value true if the decimal separator should always be formatted, false otherwise

public void setGroupingSize (int value)

Sets the number of digits grouped together by the grouping separator.

Parameters
value the number of digits grouped together

public void setGroupingUsed (boolean value)

Sets whether or not grouping will be used in this format. Grouping affects both parsing and formatting.

Parameters
value true if uses grouping,false otherwise.

public void setMaximumFractionDigits (int value)

Sets the maximum number of fraction digits that are printed when formatting. If the maximum is less than the number of fraction digits, the least significant digits are truncated. Limit the maximum to DOUBLE_FRACTION_DIGITS.

Parameters
value the maximum number of fraction digits

public void setMaximumIntegerDigits (int value)

Sets the maximum number of integer digits that are printed when formatting. If the maximum is less than the number of integer digits, the most significant digits are truncated. Limit the maximum to DOUBLE_INTEGER_DIGITS.

Parameters
value the maximum number of integer digits

public void setMinimumFractionDigits (int value)

Sets the minimum number of fraction digits that are printed when formatting. Limit the minimum to DOUBLE_FRACTION_DIGITS.

Parameters
value the minimum number of fraction digits

public void setMinimumIntegerDigits (int value)

Sets the minimum number of integer digits that are printed when formatting. Limit the minimum to DOUBLE_INTEGER_DIGITS.

Parameters
value the minimum number of integer digits

public void setMultiplier (int value)

Sets the multiplier which is applied to the number before formatting or after parsing.

Parameters
value the multiplier

public void setNegativePrefix (String value)

Sets the prefix which is formatted or parsed before a negative number.

Parameters
value the negative prefix

public void setNegativeSuffix (String value)

Sets the suffix which is formatted or parsed after a negative number.

Parameters
value the negative suffix

public void setParseBigDecimal (boolean newValue)

Let users change the behavior of a DecimalFormat, If set to true all the returned objects will be of type BigDecimal

Parameters
newValue true if all the returned objects should be type of BigDecimal

public void setParseIntegerOnly (boolean value)

When DecimalFormat is used to parsing, and this value is set to true, then all the resulting number will be of type java.lang.Integer. Except that, NaN, positive and negative infinity are still returned as java.lang.Double In this implementation, com.ibm.icu4jni.text.DecimalFormat is wrapped to fulfill most of the format and parse feature. And this method is delegated to the wrapped instance of com.ibm.icu4jni.text.DecimalFormat.

Parameters
value If set to true, all the resulting number will be of type java.lang.Integer except some special cases.

public void setPositivePrefix (String value)

Sets the prefix which is formatted or parsed before a positive number.

Parameters
value the positive prefix

public void setPositiveSuffix (String value)

Sets the suffix which is formatted or parsed after a positive number.

Parameters
value the positive suffix

public String toLocalizedPattern ()

Returns the pattern of this DecimalFormat using localized pattern characters.

Returns
  • the localized pattern

public String toPattern ()

Returns the pattern of this DecimalFormat using non-localized pattern characters.

Returns
  • the non-localized pattern