If the decimal point is not aligned, the accounts receivable report will become ugly! Or, if you want to align the characters right, Sun adds java. Io. printstream in Java 5 to meet your requirements. Using printf () and format () for formatting means that the literal data values can be mixed in the format string. The two methods work in the same way. Public printstream printf (string format, object... ARGs) {return format (format, argS);} printf calls format. Therefore, any discussion on one of the methods applies to both methods. One saying is that sun is doing this only to please the previous C programmers,
During use, note that do not write printf as println. println only includes one parameter .. Similarly, formatting data always starts with a percent sign (%). The specific format is as follows:
% [Arg_index $] [flags] [width] [. Precision] conversion char
Here, the values in square brackets are optional. The only required format string elements are percent signs and Conversion characters. Flags flag:
Purpose of the Flag + sign before the number printing + 3333.33 space plus a space before a positive number | 3333.33 | 0 add 0 003333.33-left alignment before the number | 3333.33 | (negative values are enclosed in parentheses (3333.33 ), add group separator 3,333.33 # (F) Contains the decimal point 3,333. # (XOrO) Add the prefix 0x or 0 0 xcafe ^ to convert it to uppercase 0 xcafe $ to specify the formatting parameter index. For example, % 1 $ D, % 1 $ D indicates that the index is in decimal format.
And hexadecimal print the first parameter 159 9f <format the previous parameter, for example, % d % <X indicates to be in decimal or hexadecimal notation
Print the same parameter 159 9f.
Width: This value specifies the minimum character bit to be output, which makes the result look good; perfill: precision, which is only required when the floating point is formatted. The precision specifies the number of digits to be output after the decimal point.
If the conversion character does not match the type specified in the variable, a runtime exception is returned. Do not fantasized about % d. If the conversion character corresponds to 3.14, it will be more identified and rounded up, similarly % F, it does not automatically upgrade to 3.00, system. out. format is very rigid: illegalformatconversioexception. here we will mention a few very weird output identifiers: % B and % s can correspond to any data or object. % B Outputs false only when null or false, % s: for basic data types, bind them, call the tostring method of their packaging class, and call other override methods for all objects
If the tostring method is not overwritten, The tostring method of the object is called.
Example:
System. Out. printf ("1: >%1 $ (7D <\ n", i1); // 1:> (123) <
System. Out. printf ("2:> % 0, 7d <\ n", I2); // 2:> 012,345 <
System. Out. printf ("3: >%+-7d <\ n", I2); // 3: >+ 12345 <
System. Out. printf ("4:> % 2 $ B + % 1 $ 5d <\ n", i1, false); // 4:> false +-123 <
In actual work, you often need to set the output format of numbers, such as output in percentages or decimal places. The following is a summary:
Main classes used: Java. Text. decimalformat
1. The following two methods can be used to instantiate an object:
Decimalformat df = (decimalformat) numberformat. getinstance ();
Decimalformat DF1 = (decimalformat) decimalformat. getinstance ();
Because decimalformat inherits from numberformat.
2. Set the number of decimal places
The default number of decimal places is 3, for example:
Decimalformat df = (decimalformat) numberformat. getinstance ();
System. Out. println (DF. Format (12.3456789 ));
Output: 12.346 (default private int maximumfractiondigits = 3; defined in its parent class numberformat, setmaximumfractiondigits can be reset)
Now we can set the decimal point to two places using the following method:
DF. setmaximumfractiondigits (2 );
System. Out. println (DF. Format (12.3456789 ));
The output is: 12.35.
3. To convert a number to a percentage, you can use either of the following methods:
(1)
DF. applypattern ("##. ##% ");
System. Out. println (DF. Format (12.3456789 ));
System. Out. println (DF. Format (1 ));
System. Out. println (DF. Format (0.015 ));
Output: 1234.57% 100% 1.5%
(2)
DF. setmaximumfractiondigits (2 );
System. Out. println (DF. Format (12.3456789*100) + "% ");
System. Out. println (DF. Format (1*100) + "% ");
System. Out. println (DF. Format (0.015*100) + "% ");
The output is as follows:
1,234.57% 100% 1.5%
4. Set Group Size
Decimalformat DF1 = (decimalformat) decimalformat. getinstance ();
Df1.setgroupingsize (2 );
System. Out. println (df1.format (123456789 ));
Output:, 89
You can also disable group settings by using df1.setgroupingused (false);, for example:
Decimalformat DF1 = (decimalformat) decimalformat. getinstance ();
Df1.setgroupingsize (2 );
Df1.setgroupingused (false );
System. Out. println (df1.format (123456789 ));
Output: 123456789
5. Set decimal to 2 digits
Decimalformat df2 = (decimalformat) decimalformat. getinstance ();
Df2.applypattern ("0.00 ");
System. Out. println (df2.format (1.2 ));
Output: 1.20
Sometimes we need to control the format of the output number. How can we use the Java class library to achieve this? Maybe you don't care about the format, but you need to care that your program can be used all over the world. The following simple statement is region-dependent: system. out. println (1234.56); in the United States ,". "It is a decimal point, but it is not necessarily in other places. How can this problem be solved? The following simple example uses the classes to solve the above problems:
Import java. Text. numberformat;
Import java. util. locale;
Public class decimalformat1 {
Public static void main (string ARGs []) {
// Obtain the local default format
Numberformat nf1 = numberformat. getinstance ();
System. Out. println (nf1.format (1234.56 ));
// Obtain the German format
Numberformat NF2 = numberformat. getinstance (locale. German );
System. Out. println (nf2.format (1234.56 ));
}
}