From Javadoc : https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/Formatter.html
Example :
Suppose we have int x = 123, and we want to pad 0 .
String.format("%1$013d", x);- The format specifiers for general, character, and numeric types have the following syntax:
%[argument_index$][flags][width][.precision]conversion
The optional argument_index is a decimal integer indicating the position of the argument in the argument list. The first argument is referenced by "
1$
", the second by "2$
", etc.The optional flags is a set of characters that modify the output format. The set of valid flags depends on the conversion.
The optional width is a positive decimal integer indicating the minimum number of characters to be written to the output.
The optional precision is a non-negative decimal integer usually used to restrict the number of characters. The specific behavior depends on the conversion.
The required conversion is a character indicating how the argument should be formatted. The set of valid conversions for a given argument depends on the argument's data type.
- The format specifiers for types which are used to represents dates and times have the following syntax:
%[argument_index$][flags][width]conversion
The optional argument_index, flags and width are defined as above.
The required conversion is a two character sequence. The first character is
't'
or'T'
. The second character indicates the format to be used. These characters are similar to but not completely identical to those defined by GNUdate
and POSIXstrftime(3c)
. - The format specifiers which do not correspond to arguments have the following syntax:
%[flags][width]conversion
The optional flags and width is defined as above.
The required conversion is a character indicating content to be inserted in the output.
Format Specifier | Data Type | Output |
---|---|---|
%a | floating point (except BigDecimal) | Returns Hex output of floating point number. |
%b | Any type | "true" if non-null, "false" if null |
%c | character | Unicode character |
%d | integer (incl. byte, short, int, long, bigint) | Decimal Integer |
%e | floating point | decimal number in scientific notation |
%f | floating point | decimal number |
%g | floating point | decimal number, possibly in scientific notation depending on the precision and value. |
%h | any type | Hex String of value from hashCode() method. |
%n | none | Platform-specific line separator. |
%o | integer (incl. byte, short, int, long, bigint) | Octal number |
%s | any type | String value |
%t | Date/Time (incl. long, Calendar, Date and TemporalAccessor) | %t is the prefix for Date/Time conversions. More formatting flags are needed after this. See Date/Time conversion below. |
%x | integer (incl. byte, short, int, long, bigint) | Hex string. |
Source :
https://www.javatpoint.com/java-string-format
https://stackoverflow.com/questions/5762836/what-does-1-mean-when-used-in-string-format-java
https://www.geeksforgeeks.org/format-specifiers-in-java/
Aucun commentaire:
Enregistrer un commentaire
to criticize, to improve