Sign and numeric precision
Sign and precision are where formatted numbers stop being an aesthetic choice and start being a correctness one — a truncated price or a missing minus sign is a bug, not a style preference.
Sign
| Spec | 42 | -42 | Meaning |
|---|---|---|---|
- (default) | 42 | -42 | Sign only on negative numbers |
+ | +42 | -42 | Always show a sign |
(space) | 42 | -42 | Space reserved for positive numbers, aligning with negatives |
The alternate form
The # flag changes the presentation of the value itself, not just its padding:
- Integers get their base prefix:
0x/0Xfor hex,0b/0Bfor binary,0ofor octal. - Floats always show a decimal point, even with zero fractional digits (
{:#.0f}on5.0gives5., not5).
Zero padding
{:08} zero-pads to a width of 8, and the padding is sign-aware — the zeros go between the sign and
the first digit, not before it.
fmt::format("{:08}", -42); // "-0000042" — zero padding, sign-aware
fmt::format("{:*>8}", -42); // "*****-42" — explicit fill/align, zeros not implied
0 is shorthand for "zero-fill, sign-aware pad." Writing an explicit fill character and alignment
overrides it entirely rather than combining with it.
Precision on floats
.N with f, e, or g controls decimal places differently per presentation:
| Spec | 3.14159 |
|---|---|
{:.2f} | 3.14 |
{:.2e} | 3.14e0 |
{:.2g} | 3.1 |
{} | 3.14159 (shortest round-trip) |
Precision on strings
For strings, .N truncates to at most N characters instead of controlling decimal places.
fmt::format("{:.5}", "a very long string"); // "a ver"
Combine with a width to build a fixed-width, always-truncated column — see Alignment, fill and width for the layout half.
Default float output
With no explicit precision, fmt prints the shortest decimal representation that reads back to the
exact same double. printf's %g truncates to a fixed number of significant digits by default and
can lose precision silently.
Special values
nan, inf, and -inf print as those literal words. The sign spec still applies to inf: {:+}
on positive infinity prints +inf. nan has no sign to apply regardless of the sign spec.
See also
- Type-specific presentation — the presentation types precision and sign apply to.
- Format spec syntax — where sign and precision sit in the full grammar.
- Numeric grouping and locales — the
Lflag that sits right after precision. - fmt overview — the full doc set.