java.text.DecimalFormat
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
Public Constructors
Public Methods
clone,
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
clone,
equals,
finalize,
getClass,
hashCode,
notify,
notifyAll,
toString,
wait,
wait,
wait
Details
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 |
Constructs a new DecimalFormat using the specified non-localized pattern
and DecimalFormatSymbols.
Parameters
pattern
| the non-localized pattern |
value
| the DecimalFormatSymbols |
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 |
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 |
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
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
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
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
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
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
public
Currency
getCurrency()
Returns the currency used by this decimal format.
Returns
- currency of DecimalFormatSymbols used by this decimal format
Returns the DecimalFormatSymbols used by this DecimalFormat.
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.
public
String
getNegativePrefix()
Returns the prefix which is formatted or parsed before a negative number.
public
String
getNegativeSuffix()
Returns the suffix which is formatted or parsed after a negative number.
public
String
getPositivePrefix()
Returns the prefix which is formatted or parsed before a positive number.
public
String
getPositiveSuffix()
Returns the suffix which is formatted or parsed after a positive number.
public
int
hashCode()
Returns an integer hash code for the receiver. Objects which are equal
answer the same value for this method.
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
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.
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.
public
String
toPattern()
Returns the pattern of this DecimalFormat using non-localized pattern
characters.
Returns
- the non-localized pattern