29.10. Using Flags in the printf Format String

Various flags may be used with method printf to supplement its output formatting capabilities. Seven flags are available for use in format strings (Fig 29.14).

Figure 29.14. Format string flags.
FlagDescription
-(minus sign)Left justify the output within the specified field.
+(plus sign)Display a plus sign preceding positive values and a minus sign preceding negative values.
spacePrint a space before a positive value not printed with the + flag.
#Prefix 0 to the output value when used with the octal conversion character o. Prefix 0x to the output value when used with the hexadecimal conversion character x.
0 (zero)Pad a field with leading zeros.
, (comma)Use the locale-specific thousands separator (i.e., ...

Get Java™ How to Program, Seventh Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.