Class DecimalFormat in Java

digitization, transformation, binary

Class DecimalFormat in Java:

DecimalFormat class is a subclass of Format class mostly used by NumberFormat class.
DecimalFormat works for all locales. It uses the base 10.

Most of the applications can use the static methods of NumberFormat to get the object for the desired locale and can perform minor locale-independent customization on that object.
DecimalFormat uses patterns to format and parse the customized number.

Ads code goes here

The DecimalFormat class is a concrete subclass of NumberFormat that formats and parses numbers using a formatting pattern. Typically, we do not need to instantiate DecimalFormat ourselves. Instead, the factory methods of NumberFormat return instances of DecimalFormat that are appropriate for particular locales.

However, if we need a specialized number format, we can instantiate our own DecimalFormat using a pattern string.

We can also modify the formatting pattern of an existing DecimalFormat object using the applyPattern() method. A pattern string has the following form:

positive-pattern[;negative-pattern]

If the negative pattern is omitted, negative numbers are formatted using the positive pattern with a – character prepended to the result. Each pattern has the following form:

[prefix]integer[.fraction][suffix]

Any characters other than these special characters can appear in the prefix or the suffix. A single quote can be used to escape a special character if we need to use one of these symbols in a prefix or a suffix.

For example, the pattern string for U.S. currency values is:

$#,##0.00;($#,##0.00)

This indicates that a $ character is prepended to all formatted values. The grouping separator character , is inserted every three digits. Exactly two digits after the decimal place are always shown. Negative values are shown in parentheses. Thus, the value -1234.56 produces output like:

($1,234.56)

Internally, the DecimalFormat class uses a DecimalFormatSymbols object to get the numerical strings that are appropriate for a particular locale. If you want to modify these strings, you can get the DecimalFormatSymbols object by calling  getDecimalFormatSymbols().

The pattern of DecimalFormat may be as follows:

 

CharacterMeaning
#A digit, zeros shows as absent
0A digit, zeros shows as 0
.Locale specific decimal separator
,Locale specific grouping separator
Locale specific negative prefix
%Show value in percentage
;Separates positive number format from optional
negative number format.
positive numbers are on left.
negative numbers on right.
Reserved character is shown in the output
othershown in the output

We can provide pattern like-“$#,###0.00” etc.If we do not specify the format or pattern, the default local specific format is used

The class structure DecimalFormat is given as:

public class java.text.DecimalFormat extends java.text.NumberFormat {
//Constructors
public DecimalFormat();
public DecimalFormat(String pattern);
public DecimalFormat(String pattern, DecimalFormatSymbols symbols);
//Methods
public void applyLocalizedPattern(String pattern);
public void applyPattern(String pattern);
public Object clone(); 
public boolean equals(Object obj); 
public StringBuffer format(double number, StringBuffer result,FieldPosition fieldPosition); 
public StringBuffer format(long number, StringBuffer result,FieldPosition fieldPosition); 
public DecimalFormatSymbols getDecimalFormatSymbols();
public int getGroupingSize();
public int getMultiplier();
public String getNegativePrefix();
public String getNegativeSuffix();
public String getPositivePrefix();
public String getPositiveSuffix();
public int hashCode(); 
public boolean isDecimalSeparatorAlwaysShown();
public Number parse(String text, ParsePosition status); 
public void setDecimalFormatSymbols(DecimalFormatSymbols newSymbols);
public void setDecimalSeparatorAlwaysShown(boolean newValue);
public void setGroupingSize(int newValue);
public void setMultiplier(int newValue);
public void setNegativePrefix(String newValue);
public void setNegativeSuffix(String newValue);
public void setPositivePrefix(String newValue);
public void setPositiveSuffix(String newValue);
public String toLocalizedPattern();
public String toPattern();
}
 

The details of the class structure are given as follows:

public DecimalFormat();

public DecimalFormat() constructor creates a DecimalFormat that uses the default formatting pattern and DecimalFormatSymbols that are appropriate for the default locale.

public DecimalFormat(String pattern);

public DecimalFormat(String pattern) constructor creates a DecimalFormat that uses the given formatting pattern and a
DecimalFormatSymbols that is appropriate for the default locale.

Parameter
pattern – The pattern string.

public DecimalFormat(String pattern, DecimalFormatSymbols symbols);

public DecimalFormat(String pattern, DecimalFormatSymbols symbols) constructor creates a DecimalFormat that uses the given formatting pattern and DecimalFormatSymbols object.

Parameter
pattern – The pattern string.
symbols – The DecimalFormatSymbols to use.

public void applyLocalizedPattern(String pattern);

public void applyLocalizedPattern(String pattern) method tells this DecimalFormat to use the given formatting pattern to format and parse numbers. The pattern string is assumed to have been localized to the DecimalFormatSymbols object this DecimalFormat uses.

Parameter
pattern – The pattern string.

public void applyPattern(String pattern);

public void applyPattern(String pattern) method tells this DecimalFormat to use the given formatting pattern to format and parse numbers. The pattern string is localized to the DecimalFormatSymbols object this DecimalFormat uses.

Parameter
pattern – The pattern string.

public Object clone(); 

public Object clone(); method creates a copy of this DecimalFormat and then returns it.

This method returns a copy of this DecimalFormat.

public boolean equals(Object obj); 

public boolean equals(Object obj); method returns true if obj is an instance of DecimalFormat and is equivalent to this DecimalFormat.

This method returns true if the objects are equal; false if they are not.

Parameter
obj – The object to be compared with this object.

public StringBuffer format(double number, StringBuffer result,FieldPosition fieldPosition); 

public StringBuffer format(double number, StringBuffer result,FieldPosition fieldPosition); method formats the given number and appends the result to the given StringBuffer. If fieldPosition refers to one of the number fields, its beginning and ending indices are filled with the beginning and ending positions of the given field in the resulting formatted string.

This method returns the given buffer result with the formatted representation of the number appended to it.

Parameter
number – The double value to be formatted.
result – A StringBuffer on which to append the formatted information.
fieldPosition – A number field.

public StringBuffer format(long number, StringBuffer result,FieldPosition fieldPosition); 

public StringBuffer format(long number, StringBuffer result,FieldPosition fieldPosition); method formats the given number and appends the result to the given StringBuffer. If fieldPosition refers to one of the number fields, its beginning and ending indices are filled with the beginning and ending positions of the given field in the resulting formatted string.

This method returns the given buffer result with the formatted representation of the number appended to it.

Parameter
number – The long value to be formatted.
result – A StringBuffer on which to append the formatted information.
fieldPosition – A number field.

public DecimalFormatSymbols getDecimalFormatSymbols();

public DecimalFormatSymbols getDecimalFormatSymbols() method returns the DecimalFormatSymbols object that this DecimalFormat uses internally.

This method returns the DecimalFormatSymbols object used by this DecimalFormat.

public int getGroupingSize();

public int getGroupingSize() method returns the grouping size of this DecimalFormat. The grouping size is the number of digits between grouping separators in the integer portion of a number. For example, in the number 1,234.56, the grouping size is 3 (and the grouping symbol is “,”).

This method returns the grouping size of this DecimalFormat.

public int getMultiplier();

public int getMultiplier() method returns the multiplier of this DecimalFormat. The multiplier is used to adjust a number before it is formatted or after it is parsed. For example, a percent format has a multiplier of 100 and a suffix of `%’. Thus, a value of .42 could be formatted as 42%.

This method returns the multiplier of this DecimalFormat.

public String getNegativePrefix();

public String getNegativePrefix() method returns the prefix string for negative numbers.

This method returns the string that is prepended to negative values.

public String getNegativeSuffix();

public String getNegativeSuffix() method returns the suffix string for negative numbers.

This method returns the string that is appended to negative values.

public String getPositivePrefix();

public String getPositivePrefix() method returns the prefix string for positive numbers.

This method returns the string that is prepended to positive values.

public String getPositiveSuffix();

public String getPositiveSuffix() method returns the suffix string for positive numbers.

This method returns the string that is appended to positive values.

public int hashCode(); 

public int hashCode(); method returns a hashcode for this DecimalFormat.

This method returns a hashcode for this object.

public boolean isDecimalSeparatorAlwaysShown();

public boolean isDecimalSeparatorAlwaysShown() method returns true if this DecimalFormat always shows the decimal separator. The method returns false if the decimal separator is only shown if there is a fractional portion of the number being formatted.

This method returns a boolean value that indicates whether or not the decimal separator symbol is always shown.

public Number parse(String text, ParsePosition status); 

public Number parse(String text, ParsePosition status); method parses a number from the given string, starting from the given position. After the string has been parsed, the given ParsePosition object is updated so that its index is after the parsed text.

This method returns the Number object represented by the text starting at the given position.

Parameter
text – The string to be parsed.
status – A ParsePosition object that specifies a position in the string.

public void setDecimalFormatSymbols(DecimalFormatSymbols newSymbols);

public void setDecimalFormatSymbols(DecimalFormatSymbols newSymbols) method sets the DecimalFormatSymbols object that this DecimalFormat uses internally.

Parameter
newSymbols – The new DecimalFormatSymbols object to use.

public void setDecimalSeparatorAlwaysShown(boolean newValue);

public void setDecimalSeparatorAlwaysShown(boolean newValue) method specifies whether or not the decimal separator symbol is always shown in formatted numbers. If newValue is false, the separator is only shown for numbers that have a fractional part. Otherwise, the separator is always shown.

Parameter
newValue – The new decimal separator value.

public void setGroupingSize(int newValue);

public void setGroupingSize(int newValue) method sets the grouping size of this DecimalFormat. The grouping size is the number of digits between grouping separators in the integer portion of a number. For example, in the number 1,234.56, the grouping size is 3 (and the grouping symbol is “,”).

Parameter
newValue – The new grouping size.

public void setMultiplier(int newValue);

public void setMultiplier(int newValue) method sets the multiplier of this DecimalFormat. The multiplier is used to adjust a number before it is formatted or after it is parsed. For example, a percent format has a multiplier of 100 and a suffix of %. Thus, a value of .42 could be formatted as 42%.

Parameter
newValue – The new multiplier.

public void setNegativePrefix(String newValue);

public void setNegativePrefix(String newValue) method sets the prefix string for negative values.

Parameter
newValue – The new prefix.

public void setNegativeSuffix(String newValue);

public void setNegativeSuffix(String newValue) method sets the suffix string for negative values.

Parameter
newValue – The new suffix.

public void setPositivePrefix(String newValue);

public void setPositivePrefix(String newValue) method sets the prefix string for positive values.

Parameter
newValue – The new prefix.

public void setPositiveSuffix(String newValue);

public void setPositiveSuffix(String newValue) method sets the suffix string for positive values.

Parameter
newValue – The new suffix.

public String toLocalizedPattern();

public String toLocalizedPattern() method returns the pattern string of this DecimalFormat, localized with the DecimalFormatSymbols object of this DecimalFormat.

This method returns the pattern string of this DecimalFormat.

public String toPattern();

public String toPattern() method returns the pattern string of this DecimalFormat.

This method returns the pattern string of this DecimalFormat.

Apart from these DecimalFormat class also has inherited methods from class- Object. They are as follows:

  • finalize()
  • notifyAll()
  • wait()
  • wait(long, int)
  • getClass()
  • notify()
  • toString()
  • wait(long)

DecimalFormat class also has inherited methods from NumberFormat. They are as follows:

  • format(long)
  • getMaximumIntegerDigits()
  • getMinimumIntegerDigits()
  • isParseIntegerOnly()
  • setGroupingUsed(boolean)
  • setMaximumIntegerDigits(int)
  • setMinimumIntegerDigits(int)
  • format(double)
  • format(Object, StringBuffer,FieldPosition)
  • getMaximumFractionDigits()
  • getMinimumFractionDigits()
  • isGroupingUsed()
  • parse(String)
  • parseObject(String, ParsePosition)
  • setMaximumFractionDigits(int)
  • setMinimumFractionDigits(int)
  • setParseIntegerOnly(boolean)

DecimalFormat class also has inherited a method from Format. That is as follows:

  • parseObject(String)

Share and Enjoy !

Leave a Comment

Your email address will not be published. Required fields are marked *