Name

format

format formatString [arg...]

Format a string using ANSI sprintf( ) -style formatString and arguments. Returns the formatted string. The format string placeholders have the form:

%[argpos$] [flag] [width] [.prec] [h|l]char

where argpos, width, and prec are integers and possible values for char are as follows:

d

Signed integer

u

Unsigned integer

i

Signed integer (n, 0n, or 0xn)

o

Unsigned octal

x

Unsigned hex

X

Unsigned HEX

c

Int to char

s

String

f

Float (fixed)

e

Float (0e0)

E

Float (0E0)

g

Auto float (f or e)

G

Auto float (F or E)

%

Percent sign

Possible values for flag are as follows:

-

Left justified

+

Always signed

0

Zero padding

space

Space padding

#

Alternate output format

  

Example

set i 12
   set j 1.2
   puts [format "%4d %5.3f" $i $j]
     12 1.200
   puts [format "%04X %5.3e" $i $j]
   000C 1.200e+00

Get Tcl/Tk in a Nutshell 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.