When you consider which argument types are expected for each conversion, remember that anything that
applies to a primitive type also applies to its wrapper. For brevity we don't mention the wrapper classes
explicitly.
22.1.2. Integer Conversions
The integer conversions apply to arguments of type byte, short, int, and long. The different
conversions are
- d decimal format
- o octal format
- x, X hexadecimal format
The width value specifies the minimum number of characters to outputincluding additional characters that the
flags may cause to be included. No precision value may be given. The flags applicable to each conversion are
shown in the following table. An entry with a x means the flag is not allowed.
Flag d o x/X Meaning
'-' Left justify (otherwise right justify)
'#' x Include radix: 0 for octal and 0x or 0X for hexadecimal
'+' x x Always include the sign
' ' x x (space) Include a leading space for positive values
'0' Use zero-padding (else spaces)
',' x x Include grouping separators
'(' x x Enclose negative values in parentheses
For example, to print a zero-padded, hexadecimal value with a radix indicator and a minimum width of 10,
you could use
System.out.printf("%0#10x %n", val);
which for val equal to 32 prints
0x00000020
You cannot use + and space together, nor can you use and 0 together. If either of 0 or is given then a width
must be given.
22.1.3. Floating-Point Conversions
The floating-point conversions apply to arguments of type float and double. The different conversions
are
- e, Ecomputerized scientific notation (such as 3.142e+00)