DecimalFormat in Java

0
229
Spread the love
  • 15
  • 16
  • 26
  • 12
  •  
  •  
  •  
  • 9
  •  
  •  
    78
    Shares

DecimalFormat in Java
DecimalFormat in Java

DecimalFormat in Java:

DecimalFormat class is 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 a desired locale and can perform minor locale independent customization on that object.
DecimalFormat uses pattern to format and parse the customized number.

The pattern of DecimalFormat may be as follows:

 

Character Meaning
# A digit,zeros shows as absent
0 A 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 shown in the output
other shown 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();
}
 
Don't miss out!
Subscribe To Newsletter

Receive top technical news, lesson ideas, travel tips and more!

Invalid email address
Give it a try. You can unsubscribe at any time.

Spread the love
  • 15
  • 16
  • 26
  • 12
  •  
  •  
  •  
  • 9
  •  
  •  
    78
    Shares

LEAVE A REPLY

Please enter your comment!
Please enter your name here