We all know that programs sometimes need to format numbers, so what is number formatting? The so-called digital formatting is to obtain a string according to the specified format.
For example, suppose we want 3.141592 to retain up to 2 decimal places, then the resulting formatted string should be 3.14; if we want the integer 123456789 to be grouped by "thousands", then the resulting formatted string should be 123,456,789; if we want the number 12.3456 to retain 3 decimal place, the integer part must display at least 3 digits, then the formatted string obtained should be 012.346.
Before JDK 1.5, programs needed to use related classes in the java.text package, such as the DecimalFormat class , to format numeric data. JDK 1.5 version provides a more convenient Formatter class . The Formatter class provides a format method similar to the C language printf function:
format (format mode, value list)
This method returns a string representation of a "list of values" according to the "formatting mode".
Currently, Java has made the format method a static method of the String class. Therefore, the program can directly use the String class to call the format method to format numbers.
The "formatting mode" in the format method is a character sequence enclosed in double quotes, that is, a string. The characters in this character sequence are composed of format characters and ordinary characters.
For example: %d and %f in the output results %d, %f, %d are format symbols. The first four Chinese characters and the two commas in the middle are ordinary characters. It should be noted that anything that is not a format symbol is considered It is an ordinary character. Students can refer to the java.util.Formatter class in the Java API to learn more about format characters. The string returned by the format method is the string after the format characters in the "formatting mode" are replaced with the formatted results it obtains, for example:
Strings=String.format(%.2f,3.141592);
Then s is 3.14, because the result of %.2f formatting 3.141592 is 3.14.
The "list of values" in the format method is a comma-separated list of variables, constants, or expressions. Make sure that the number of format characters in the "format mode" of the format method is the same as the number of values listed in the "value list", for example:
Strings=String.format(%d元%0.3fkg%d台,123,456.777888,999);
Then, s is 123 yuan, 456.778 kilograms and 999 units.
By default, the format method uses the format characters in the "formatting mode" in order from left to right to format the corresponding values in the "value list", while the ordinary characters in the "formatting mode" remain unchanged, for example, assuming the int type The values of variable x and double variable y are 123 and 3.1415926 respectively, then for:
Strings=String.format(from left to right:%d,%.3f,%d,x,y,100);
Then, s is from left to right: 123,3.142,100.
If you do not want to use the default order (from left to right) for formatting, you can also add an index symbol in front of the format character: index$, for example, 1$ means the first one in the "value list", 2$ means "value" 2 in the list" for:
Strings=String.format(not from left to right:%2$.3f,%3$d,%1$d,x,y,100);
Then, s is not from left to right: 3.142,100,123.
Note: If you plan to include ordinary % in the "formatting mode", you need to enter two % consecutively when writing the code, for example:
Strings=String.format(%d%%,89);
Then, s is 89%.