First acquaintance with Java (Java number processing class - number format r)

1. Formatting of numbers

    Formatting of numbers is very common to solve practical problems, such as expressing the price of commodities in a supermarket, two significant digits need to be reserved. Java mainly performs digital formatting operations on floating-point data, among which floating-point data includes double and float data. In Java, use java.text.DecimalFormat to format numbers.

    Unformatted data in Java follows these guidelines:

    (1) If the absolute value of the data is greater than 0.001 and less than 10000000, Java will represent it in regular decimal form.

    (2) If the absolute value of the data is less than 0.001 or greater than 10000000, use scientific notation.

    Since the above output format cannot meet the requirements of the actual problem, the result is usually formatted into the specified form and then output. Formatting can be done in Java using the DecimalFormat class.

    DecimalFormat is a subclass of NumberFormat not used to format decimal numbers. It can format some numbers as integers, floats, scientific notation, percents, etc. By using this class, you can add units to the number to output or control the precision of the number. In general, you can pass the number format when instantiating the DecimalFormat object, or you can implement the number format through the applyPattern() method in the DecimalFormat class.

    When formatting numbers, some special characters are used in the DecimalFormat class to form a formatting template, and numbers are matched according to certain special character rules. as follows:

Description of special characters in DecimalFormat class
character illustrate
0

Represents an Arabic numeral, using the special character "0" to represent an Arabic digit of a number,

If no digit exists for the bit, 0 is displayed

#

Represents an Arabic numeral, using the special character " # " to represent an Arabic digit of a number,

If the digit exists, display the character; if the digit does not exist, it will not display

. Decimal Separator or Currency Decimal Separator
- negative
grouping separator
E Separate mantissa and exponent in scientific notation
% This symbol is placed in the prefix or suffix of a number, multiplying the number by 100 to display it as a percentage
\u2030 This symbol is placed in the prefix or suffix of the number, and the number is multiplied by 10000 and displayed as a thousandth
\u00A4 This symbol is placed in the prefix or or after a number, as a currency token
'

This symbol is a single quotation mark. When the above special characters appear in numbers, single quotation marks should be added to the special characters.

The system will treat this symbol as a normal symbol

    eg : Create a class and define the SimgleForm() method and the UseApplyPatternMethodFormat() method in the class to implement formatting.

import java.text.DecimalFormat;
public class DecimalFormatSimpleDemo {
    //Set the formatting when using the instantiated object
    static public void SimpleFormat(String pattern ,double value) {
        //Instantiate the DecimalFormat object
        DecimalFormat myFormat = new DecimalFormat(pattern);
        String output=myFormat.format(value);//Format the number
        System.out.println(value + " " + pattern + " " + output);
    }
    //Use the applyPattern() method to format the number
    static public void UseApplyPatternMethodFormat(String pattern ,double value) {
        //Instantiate the DecimalFormat object
        DecimalFormat myFormat = new DecimalFormat(pattern);
        myFormat.applyPattern(pattern); //Call the applyPattern() method to set the format template
        System.out.println(value + " " + pattern + " " + myFormat.format(value));
    }
    public static void main(String[] args) {
        SimpleFormat("###,###.###", 123456.789);                       //调用静态 SimFormat() 方法
        SimpleFormat("00000000.###kg", 123456.789);                    //在数字后面加上单位
        //按照格式模板格式化数字,不存的位以 0 显示
        SimpleFormat("000000.000", 123.78);
        //田勇静态 UseApplyPatternMethodFormat() 方法
        UseApplyPatternMethodFormat("#.###%", 0.789);                  //将数字转换为百分数形式
        //将小数点后格式化为两位
        UseApplyPatternMethodFormat("###.##", 123456.789);
        //将数字转化为千分数形式
        UseApplyPatternMethodFormat("0.00\u2030", 0.789);
    }
}

    运行结果为 :

123456.789 ###,###.### 123,456.789
123456.789 00000000.###kg 00123456.789kg
123.78 000000.000 000123.780
0.789 #.###% 78.9%
123456.789 ###.## 123456.79
0.789 0.00‰ 789.00‰

    在本例中可以看到 ,代码使用 import 导入了 java.text.DecimalFormat 这个类,这是首先告诉系统下面的代码将使用到 DecimalFormat 类;然后定义两个格式化数字的方法,这两个方法的参数个数都为两个,分别代表数字格式化模板和具体需要格式化的数字,虽然这两个方法都可以实现格式化数字的操作,但使用的方式不同, SimleFormat() 方法是在实例化 DecimalFormat 对象时设置数字格式化模板,而 UseApplyPatternMethodFormat() 方法是在实例化 DecimalFormat 对象后调用 applyPattern() 方法设置数字格式化模板了最后在主方法中根据不同形式模板格式化数字。

    在 DecimalFormat 类中定义除了可以设置格式化模板来格式化数字之外,话可以使用一些特殊方法对数字进行格式化设置。 如 :

DecimalFormat myFormat = new DecimalFormat();                          //实例化 DecimalFormat 类对象
myFormat.setGroupingSize(2);                                           //设置将数字分组的大小
myFormat.setGroupingUsed(false);                                       //设置是否支持分组

    eg :创建类,在类的主方法中调用 setGroupingSize() 与 serGroupingUsed() 方法实现数字的分组。

import java.text.DecimalFormat;
public class DecimalMethod {
    public static void main(String[] args) {
        DecimalFormat myFormat = new DecimalFormat();
        myFormat.setGroupingSize(2);                                   //设置将数字分组为 2
        String output  =myFormat.format(123456.789);
        System.out.println("将数字以每两个数字分组:" + output);
        myFormat.setGroupingUsed(false);                               //设置不允许数字进行分组
        String output2 = myFormat.format(123456.789);
        System.out.println("不允许数字分组:" + output2);
    }
}

    运行结果为 :

将数字以每两个数字分组:12,34,56.789
不允许数字分组:123456.789



Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325621597&siteId=291194637