DecimalFormat usage of java

Keywords: Java

1. In practice, sometimes there is a need to format numbers. For example, a certain section of the order number must be 4 digits. If it is not enough, 0 is used. Or the output needs to be accompanied by a percentage sign, unit, currency symbol, etc. the DecimalFormat of Java can be well supported.

2. After instantiating DecimalFormat, set the pattern string of the instance object format (two ways).

DecimalFormat  myFormat = new DecimalFormat(String pattern);  // With format parameter, instantiate formatter
DecimalFormat  myFormat = new DecimalFormat();  //  Without format parameter, instantiate formatter
myFormat.applyPattern(String pattern);  //  Set formatting parameters for formatter

3. Call the format() method of DecimalFormat object to format and return the string.

String out= myFormat.Format(double value);  //  Formatting Numbers 

4. Description of pattern format expression:

0: if there is a number in this bit, the number will be output; if not, the 0 will be output;
#: if there is a digit in this bit, the digit will be output; if there is no digit, no output will be made;
.: decimal separator;
- minus sign;
,: group separator;
E: separation digit and index in scientific counting method;
%: place the prefix or suffix of the number, multiply the number by 100, and output as a percentage;
\u2030: place the prefix or suffix of the number, multiply the number by 1000, and output in thousands;
\u00A4: place the prefix or suffix of the number as the currency mark;
': this symbol is a single quotation mark. When the above special symbol appears in a number, a single quotation mark should be added for the special symbol. The system will treat the special symbol as a common symbol;

5. Format example

public static void fun(String pattern, double value) {
     // DecimalFormat formater = new DecimalFormat(pattern);
     DecimalFormat formater = new DecimalFormat(pattern);
     formater .applyPattern(pattern);
     System.err.println(formater .format(value));
}
public static void main (String []args) {
   fun("#,###.###", 123456.789);  //123,456.789
   fun("00000000.000kg", 123456.789);  //00123456.789kg
   fun("000000.000", 123.78);  // 000123.780
   fun("#.#%", 0.789);  // 78.9%
   fun("#.##", 123456.789);  //  123456.79
   fun("#.00\u2030", 0.789);  //  789.00‰
   fun("#\u00A4", 1234);  //  1234¥
}

6. DecimalFormat also provides the function of dividing numbers by commas:

DecimalFormat myFormat = new DecimalFormat();
myFormat.setGroupingSize(3); / / when comma is used for segmentation, the size of each segment is 3;
myFormat.setGroupingUsed(false/true); / / when it is false, the group size set above is invalid, and only when it is true can it be grouped;

7. Segment example:

public static void fun0(double value) {
    DecimalFormat myFormat = new DecimalFormat();
    myFormat.setGroupingSize(3);  //Set segment spacing to 3
    System.err.println(myFormat.format(value));
}
public static void fun1(double value)  {
    DecimalFormat myFormat = new DecimalFormat();
    myFormat.setGroupingSize(3);  //Set segment spacing to 3
    myFormat.setGroupingUsed(false);  //Non segmentation
    System.err.println(myFormat.format(value));
}
public static void main (String []args) {
    fun0(123456.789);  //123,456.789
    fun1(123456.789);  //123456.789
}

Posted by bills50000 on Tue, 10 Mar 2020 19:48:31 -0700