java.text
public class DecimalFormat extends NumberFormat
NumberFormat
base class.NumberFormat.Field
FRACTION_FIELD, INTEGER_FIELD
Constructor and Description |
---|
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. |
Modifier and Type | Method and Description |
---|---|
void |
applyLocalizedPattern(String pattern)
Apply the given localized patern to the current DecimalFormat object.
|
void |
applyPattern(String pattern)
Apply the given localized pattern to the current DecimalFormat object.
|
Object |
clone()
Creates a copy of this object.
|
boolean |
equals(Object obj)
Tests this instance for equality with an arbitrary object.
|
StringBuffer |
format(double number,
StringBuffer dest,
FieldPosition fieldPos)
Produce a formatted
String representation of this double. |
StringBuffer |
format(long number,
StringBuffer dest,
FieldPosition fieldPos)
Produce a formatted
String representation of this long. |
StringBuffer |
format(Object obj,
StringBuffer sbuf,
FieldPosition pos)
Produce a formatted
String representation of this object. |
AttributedCharacterIterator |
formatToCharacterIterator(Object value)
Return an
AttributedCharacterIterator as a result of
the formatting of the passed Object . |
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()
Gets the interval used between a grouping separator and the next.
|
int |
getMultiplier()
Gets the multiplier used in percent and similar formats.
|
String |
getNegativePrefix()
Gets the negative prefix.
|
String |
getNegativeSuffix()
Gets the negative suffix.
|
String |
getPositivePrefix()
Gets the positive prefix.
|
String |
getPositiveSuffix()
Gets the positive suffix.
|
int |
hashCode()
Returns a hash code for this object.
|
boolean |
isDecimalSeparatorAlwaysShown() |
boolean |
isParseBigDecimal()
Returns
true if
parse(java.lang.String, java.text.ParsePosition) returns
a BigDecimal , false otherwise. |
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)
Define if the decimal separator should be always visible or only
visible when needed.
|
void |
setGroupingSize(int groupSize)
Sets the number of digits used to group portions of the integer part of
the number.
|
void |
setMaximumFractionDigits(int newValue)
Sets the maximum number of digits allowed in the fraction
portion of a number to the specified value.
|
void |
setMaximumIntegerDigits(int newValue)
Sets the maximum number of digits allowed in the integer
portion of a number to the specified value.
|
void |
setMinimumFractionDigits(int newValue)
Sets the minimum number of digits allowed in the fraction
portion of a number to the specified value.
|
void |
setMinimumIntegerDigits(int newValue)
Sets the minimum number of digits allowed in the integer
portion of a number to the specified value.
|
void |
setMultiplier(int newValue)
Sets the multiplier for use in percent and similar formats.
|
void |
setNegativePrefix(String newValue)
Sets the negative prefix.
|
void |
setNegativeSuffix(String newValue)
Sets the negative suffix.
|
void |
setParseBigDecimal(boolean newValue)
Define if
parse(java.lang.String, java.text.ParsePosition)
should return a BigDecimal or not. |
void |
setPositivePrefix(String newValue)
Sets the positive prefix.
|
void |
setPositiveSuffix(String newValue)
Sets the new positive suffix.
|
String |
toLocalizedPattern()
This method returns a string with the formatting pattern being used
by this object.
|
String |
toPattern()
This method returns a string with the formatting pattern being used
by this object.
|
format, format, getAvailableLocales, getCurrencyInstance, getCurrencyInstance, getInstance, getInstance, getIntegerInstance, getIntegerInstance, getMaximumFractionDigits, getMaximumIntegerDigits, getMinimumFractionDigits, getMinimumIntegerDigits, getNumberInstance, getNumberInstance, getPercentInstance, getPercentInstance, isGroupingUsed, isParseIntegerOnly, parse, parseObject, setGroupingUsed, setParseIntegerOnly
format, parseObject
public DecimalFormat()
DecimalFormat
which uses the default
pattern and symbols.public DecimalFormat(String pattern)
DecimalFormat
which uses the given
pattern and the default symbols for formatting and parsing.pattern
- the non-localized pattern to use.NullPointerException
- if any argument is null.IllegalArgumentException
- if the pattern is invalid.public DecimalFormat(String pattern, DecimalFormatSymbols symbols)
DecimalFormat
using the given pattern
and formatting symbols. This construction method is used to give
complete control over the formatting process.pattern
- the non-localized pattern to use.symbols
- the set of symbols used for parsing and formatting.NullPointerException
- if any argument is null.IllegalArgumentException
- if the pattern is invalid.public void applyLocalizedPattern(String pattern)
pattern
- The localized pattern to apply.IllegalArgumentException
- if the given pattern is invalid.NullPointerException
- if the input pattern is null.public void applyPattern(String pattern)
pattern
- The localized pattern to apply.IllegalArgumentException
- if the given pattern is invalid.NullPointerException
- if the input pattern is null.public boolean equals(Object obj)
true
if:
obj
is not null
;obj
is an instance of DecimalFormat
;obj
have the same attributes;equals
in class NumberFormat
obj
- the object (null
permitted).Object.hashCode()
public int hashCode()
hashCode
in class NumberFormat
Object.equals(Object)
,
System.identityHashCode(Object)
public final StringBuffer format(Object obj, StringBuffer sbuf, FieldPosition pos)
String
representation of this object.
The passed object must be of type number.format
in class NumberFormat
obj
- The Number
to format.sbuf
- The destination String; text will be appended to this String.pos
- If used on input can be used to define an alignment
field. If used on output defines the offsets of the alignment field.public StringBuffer format(double number, StringBuffer dest, FieldPosition fieldPos)
String
representation of this double.format
in class NumberFormat
number
- The double to format.dest
- The destination String; text will be appended to this String.fieldPos
- If used on input can be used to define an alignment
field. If used on output defines the offsets of the alignment field.NullPointerException
- if dest
or fieldPos are nullpublic StringBuffer format(long number, StringBuffer dest, FieldPosition fieldPos)
String
representation of this long.format
in class NumberFormat
number
- The long to format.dest
- The destination String; text will be appended to this String.fieldPos
- If used on input can be used to define an alignment
field. If used on output defines the offsets of the alignment field.public AttributedCharacterIterator formatToCharacterIterator(Object value)
AttributedCharacterIterator
as a result of
the formatting of the passed Object
.formatToCharacterIterator
in class Format
AttributedCharacterIterator
.NullPointerException
- if value is null
.IllegalArgumentException
- if value is not an instance of
Number
.public Currency getCurrency()
DecimalFormatSymbols
used by this
DecimalFormat
.getCurrency
in class NumberFormat
Currency
if
the currency code matches a known one, null otherwise.public DecimalFormatSymbols getDecimalFormatSymbols()
public int getGroupingSize()
DecimalFormatSymbols.getDecimalSeparator()
public int getMultiplier()
public String getNegativePrefix()
public String getNegativeSuffix()
public String getPositivePrefix()
public String getPositiveSuffix()
public boolean isDecimalSeparatorAlwaysShown()
public void setParseBigDecimal(boolean newValue)
parse(java.lang.String, java.text.ParsePosition)
should return a BigDecimal
or not.newValue
- public boolean isParseBigDecimal()
true
if
parse(java.lang.String, java.text.ParsePosition)
returns
a BigDecimal
, false
otherwise.
The default return value for this method is false
.true
if the parse method returns a BigDecimal
,
false
otherwise.setParseBigDecimal(boolean)
public Number parse(String str, ParsePosition pos)
Number
.
The parsing starts at pos
, which is updated as the parser
consume characters in the passed string.
On error, the Position
object index is not updated, while
error position is set appropriately, an null
is returned.parse
in class NumberFormat
str
- The string to parse.pos
- The desired ParsePosition
.Number
public void setCurrency(Currency currency)
Currency
on the
DecimalFormatSymbols
used, which also sets the
currency symbols on those symbols.setCurrency
in class NumberFormat
currency
- The new Currency
on the
DecimalFormatSymbols
.public void setDecimalFormatSymbols(DecimalFormatSymbols newSymbols)
newSymbols
- the symbols (null
not permitted).public void setDecimalSeparatorAlwaysShown(boolean newValue)
true
if you want the decimal separator to be
always shown, false
otherwise.newValue
- true if you want the decimal separator to be
always shown, false
otherwise.public void setGroupingSize(int groupSize)
123456
, with a grouping
size of 3, is rendered 123,456
.groupSize
- The number of digits used while grouping portions
of the integer part of a number.public void setMaximumIntegerDigits(int newValue)
newvalue
and 309. Any value below zero will be
replaced by zero.setMaximumIntegerDigits
in class NumberFormat
newValue
- The new maximum integer digits value.public void setMinimumIntegerDigits(int newValue)
newvalue
and 309. Any value below zero will be
replaced by zero.setMinimumIntegerDigits
in class NumberFormat
newValue
- The new minimum integer digits value.public void setMaximumFractionDigits(int newValue)
newvalue
and 309. Any value below zero will be
replaced by zero.setMaximumFractionDigits
in class NumberFormat
newValue
- The new maximum fraction digits value.public void setMinimumFractionDigits(int newValue)
newvalue
and 309. Any value below zero will be
replaced by zero.setMinimumFractionDigits
in class NumberFormat
newValue
- The new minimum fraction digits value.public void setMultiplier(int newValue)
newValue
- the new value for multiplier.public void setNegativePrefix(String newValue)
newValue
- The new negative prefix.public void setNegativeSuffix(String newValue)
newValue
- The new negative suffix.public void setPositivePrefix(String newValue)
newValue
- The new positive prefix.public void setPositiveSuffix(String newValue)
newValue
- The new positive suffix.public String toLocalizedPattern()
String
with the formatting pattern.toPattern()
public String toPattern()
String
with the formatting pattern.toLocalizedPattern()