The %d, %o, %x and %X format characters can all format byte, Byte, short, Short, int, Integer, long and Long type data, among which:
%d formats the value as a decimal integer.
%o formats the value as an octal integer.
%x formats the value as a lowercase hexadecimal integer.
%X formats the value as an uppercase hexadecimal integer.
For example:
Strings=String.format(%d,%o,%x,%X,300,300,300,300);
Then, the string s is 300,454,12c,12C.
When formatting a positive integer, it is mandatory to add a positive sign, for example: %+d formats 123 as +123.
When formatting integers, group by "thousands", for example:
Strings=String.format(Group by thousands:%,d. Group by thousands with positive sign %+,d,12345678,9876);
Then, the string s is grouped by thousands: 12,345,678. Grouped by thousands with plus sign +9,876.
The so-called data width is the length of the string returned by the format method . The general format for specifying the data width is %md , whose effect is to add spaces to the left of the number; if it is %-md , its effect is to add spaces to the right of the number.
For example, to format the number 10 into a string of width 8:
Strings=String.format(%8d,10);
Then, the string s is 10, and its length (s.length()) is 8, that is, s adds 6 space characters to the left of 10, for:
Strings=String.format(%-8d,10);
Then, the string s is 10, and its length (s.length()) is 8, that is, s adds 6 space characters to the right of 10.
Note : If the actual number width is greater than the width specified in the format, the number will be formatted according to the actual width.
We can also add a prefix of 0 in front of the width, indicating that the remaining part on the left side of the width will be filled with the number 0 without spaces, for example:
Strings=String.format(%08d,12);
Then, the string s is 00000012, and its length (s.length()) is 8, that is, s adds 6 digits 0 to the left of 12.