The %f, %e(%E), %g(%G) and %a(%A) format characters can format float, Float, double and Double, where:
%f formats the value as a decimal floating point number, retaining 6 decimal places.
%e (%E) formats the value as a decimal floating point number in scientific notation. %E capitalizes the exponent symbol when formatting.
For example:
StringS=String.format(%f,%e,1234.56,1234.56);
Then, the string s is 1234.560000,1.23456e+03.
When formatting a positive number, a positive sign is forced to be added. For example: %+f formats 123.45 as +123.45, and %+E formats 123.45 as +1.2345E+2.
When formatting floating point numbers, group the integer parts by "thousands", for example:
Strings=String.format(the integer part is grouped by thousands:%+,f,123456789.987);
Then, the string s is the integer part grouped by thousands: +123,456,789.987000.
"%.nf" can limit the number of decimal places, where n is the number of decimal places retained, for example: %.3f formats 3.1415926 into 3.142 (retaining 3 decimal places).
The general format for specifying 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 12.34 into a string of width 10:
Strings=String.format(%10f,12.34);
Then, the string s is 12.340000, and its length (s.length()) is 10, that is, s adds a space character to the left of 12.340000, for:
Strings=String.format(%-10f,12.34);
Then, the string s is 12.340000, and its length (s.length()) is 10, that is, s adds a space character to the right of 12.340000.
While specifying the width, you can also limit the number of decimal places (%m.nf), for:
Strings=String.format(%10.2f,12.34);
Then, the string s is 12.34, and its length (s.length()) is 10, that is, s adds 5 space characters to the left of 12.34.
We can also add a prefix of 0 in front of the width, which means using the number 0 and not using spaces to fill the remaining part of the width on the left, for example:
Strings=String.format(%010f,12.34);
Then, the string s is 012.340000, and its length (s.length()) is 10, that is, s adds a number 0 to the left of 12.340000.
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.