Formatted Output: Format Specificaton Reference

The following characters, when found inside a format marker, are interpreted as format specification: 

= - < > _ ^ # + ! 0 1 2 3 4 5 6 7 8 9 . a b d o x X f F e E g G

Any other character will be used as fill.

The following table explains all of the above characters.


Format Chars
Equivalent std ostream manip or method() Notes
Examples
Output
Alignment arg:
-70
n
width(n) n is integer > 0, otherwise it is fill char
%10s
       -70
- or <
left Alignment is active only when width has been specified and is larger than width of entity being printed.
Internal makes a difference only if extra info is specified or, if not, for negative numbers
%-10s %<10s
-70
_
internal %_10s
-      70
>
right %>10s
      -70  
= n/a Center %=10s -70
^ n/a Let stream choose alignment %^10s -70
c fill(c)
c is ASCII printable charcter
Fill spaces with c.
For one of the format chars, escape it ('\\')
%*_10s
-****70
!c
fill(c)
c is any one of the format chars %_!_10s
-____70

Extra info arg:
8
#
showbase
When representing integers: prepends 0x for hexadecimal values (see x and X), 0 for octal values (see o) (and nothing for decimal values)
%#xs
%#os
0x8
08

showpoint
When representing floating-point values: always show the decimal point (e.g. even if specified precision is 0, in fixed notation)
%#f.0s
8. (if arg is 8.0)
+
showpos Insert plus sign (+) before every non-negative value %+s
+8

Integer format arg:
1
a boolalpha Print "true" and "false" instead of 0 and 1 %bs 1
b
noboolalpha Print 0 or 1 for bool (default)
%bs
true
d
dec
Represent integers in base 10
%ds
1
o
oct
Represent integers in base 8
%os
01
x
hex
Represent integers in base 16
%xs
0x1
X
x + uppercase
Will print 0X instead of 0x if # used
%Xs
0X1

Floating point format arg:
12.34
f
fixed
Represent floating point values in fixed notation (i.e. no exponents)
%fs
12.34
e
scientific
%es
1.234e1
E
e + uppercase
%Es
1.234E1
g
best of fixed or scientific
This is the default
%gs
12.34
G
g + uppercase
%Gs
12.34
.N
precision() N is an integer >= 0
if f, e, or E: N is number of digits desried after decimal point;
if g or G: N is number of significant digits
%.1fs
%.1Es
12.3
1.2E1