Java - Format Specifier Flags

Introduction

Flags or modifiers modify the formatted output.

The following table lists all flags that can be used in a format specifier.

Flag
Description
Format String
Argument
Formatted Text
'-'


result is left justified.
result is right justified when you do
not use the '-' flag in a format specifier.
"'%6s'"
"'%-6s'"

"Java"
"Java"

' Java'
'Java '

'#'

The argument is formatted in alternate
form depending on the conversion part.
"%x"
"%#x"
123456789
123456789
75bcd15
0x75bcd15
'+'

The result will have a + sign for positive
values. It applies only to numeric values.
"%d"
"%+d"
105
105
105
+105
' '


The result contains a leading space
for positive values. It applies only to
numeric values.
"'%d'"
"'% d'"

105
105

'105'
' 105'

'0'

The result is zero padded. It applies
only to numeric values.
"'%6d'"
"'%06d'"
105
105
' 105'
'000105'
' , '


The result contains a locale-specific
grouping separator. It applied only to
numeric values.
"%,d"
"%,d"

12345
12345

12,345 (US Locale)
12 345 (France locale)

'('


The result is enclosed in
parentheses for a negative number.
It applies only to numeric values.
"%d"
"%(d"

-2028
-2028

-2028
(2028)

'<'


It causes the argument for the previous
format specifier to be reused. It is mostly
used in formatting dates and times.
"%s and %<s"


"Java"


Java and Java


The valid use of a flag depends on the context of its use.