format
Printf-style string formatting.
Syntax
tcl
format formatString ?arg ...?Parameters
- formatString: A format string containing literal text and conversion specifiers
- arg: Values to substitute into the format string
Format Specifiers
| Specifier | Description |
|---|---|
%d, %i | Signed decimal integer |
%u | Unsigned decimal integer |
%o | Octal integer |
%x, %X | Hexadecimal integer (lowercase/uppercase) |
%b | Binary integer |
%c | Character (from integer code) |
%s | String |
%f | Floating-point (fixed notation) |
%e, %E | Floating-point (exponential notation) |
%g, %G | Floating-point (shorter of %f or %e) |
%a, %A | Floating-point (hexadecimal) |
%p | Pointer (platform-specific) |
%% | Literal percent sign |
Flags
| Flag | Description |
|---|---|
- | Left-justify within field width |
0 | Pad with zeros instead of spaces |
+ | Always show sign for numeric values |
# | Alternate form (prefix for octal/hex) |
| Space before positive numbers |
Width and Precision
- width: Minimum field width (e.g.,
%10s) - .precision: For floats, decimal places; for strings, max length (e.g.,
%.2f) - positional: Use
%n$to reference argument by position (e.g.,%2$s)
Examples
Basic formatting
Output
Number formatting
Output
Floating-point formatting
Output
Width and alignment
Output
Positional arguments
Output
Character conversion
Output
