String. In Java Summary of using format
String. In Java Usage Summary of format
1. Format integer:% [index $] [ID] [minimum width] conversion method
We can see that the format string is composed of four parts, in which the meaning of% [index $] has been mentioned above, and the meaning of [minimum width] is also well understood, that is, the minimum number of digits in the final integer converted string. Let's take a look at the meaning of the remaining two parts:
identification:
Conversion mode:
The above description is too boring. Let's look at some specific examples. It should be noted that most identification characters can be used at the same time.
2. Format floating-point numbers:% [index $] [ID] [minimum width] [. Precision] conversion method
We can see that the conversion of floating-point numbers has an "precision" option, which can control the number of digits after the decimal point.
identification:
Conversion mode:
3. Format characters:
Formatting characters is very simple. C represents characters, '-' in the logo represents left alignment, and nothing else.
4. Format the percentage symbol:
After reading the above description, you will find that the percentage symbol "%" is a prefix of a special format. So what if we need to enter a percentage symbol? You must escape characters, but note that the escape character here is not "\", but "%". In other words, the following statement can output a "12%":
System. out. println(String.format("%1$d%%",12));
5. Obtain platform independent line separator:
System. Getproperty ("line. Separator") can obtain platform independent line separators, but it is too cumbersome to use it in the middle of format. So the format function comes with a platform independent line separator, which is string format("%n")。
6. Format the date type:
The following suffix characters for date and time conversions are defined for't 'and't' conversions. These types are similar to but not identical to those defined by GNU date and POSIX strftime (3C). Other conversion types are provided to access Java specific functionality (such as using 'l' as a millisecond in seconds).
The following conversion characters are used to format the time:
The following conversion characters are used to format the date:
The following conversion characters are used to format common date / time combinations.
Thank you for reading, hope to help you, thank you for your support to this site!