printf() and sprintf() work just like in C, with a few slight differences. printf() gives you a lot of control over the formatting of your values, which is what it was intended to do.

If you've ever wanted to make a nicely behaved, field-aligned report, round to an integer or specific decimal place, get octal or hexadecimal representations of your values, or just display your values in any other form imaginable, keep reading. printf() and sprintf() give you the utility to do that, and more.

printf() versus sprintf()

printf() and sprintf() look argumentally the same. That is, whatever arguments you pass to one, you can pass to the other without change. The difference is that printf() prints to a filehandle, and sprintf() returns the string printf() would have outputted.

To use printf() on a filehandle other than STDOUT, specify the filehandle you want to use just as you would with print, like so:

printf(HANDLE $format, @values);

The result will be printed to HANDLE.

sprintf doesn't take a filehandle, but instead returns the output into a string.

my $string = sprintf($format, @values);

The Format String

The format string in printf() is a number of tokens which describe how
to print the variables you supply, and whatever else you want. Each
variable format specifier starts with a %, is followed by zero or more
of the optional modifiers, and ends with a conversion specifier.

A typical format string could look like this:

"foo is %d"

Printed, it may look something like: 'foo is 12'. The %d is replaced by
a variable specified after the format string argument. In this case, you
would say:

printf("foo is %d", $decimal);

so that %d is replaced with the value of $decimal. You can put as many specifiers
in the format string as you like, with the same number of following arguments as
there are specifiers. An example:

printf("%d %s %f", $decimal, $string, $float);

Conversion Specifiers

You put these in your Format String. Each one, except %%, is replaced by
the corresponding value in the printf argument list.

%% prints a single % (percent sign).

%c prints a single character with the given ASCII number (66 is printed as
B, for example).

%s prints a given string

%d a signed integer in decimal

%u an unsigned (can't be negative) integer in decimal

%o an unsigned integer in octal

%x an unsigned integer in hexadecimal

%e a floating point number in scientific notation

%f a floating point number in fixed decimal notation

%g same as %e or %f, whichever printf thinks is best.

%X is the same as %x, except it uses upper-case letters

%E like %e, but with an upper-case 'E'

%G same as %E when scientific notation is used for the value.

%p a pointer; it outputs Perl's value address in hexadecimal.

%n an odd little bugger that *stores* (in the variable given
for the position) the number of characters output so far.

Others that simply exist for backward compatibility:

%i synonym for %d

%D same as %ld (long decimal)

%U same as %lu (long unsigned decimal)

%O same as %lo (long octal)

%F same as %f

Format Specifiers

Each item below is optional (unless otherwise stated), and should be used in the
order they appear here. If this is confusing, skip to the next section. It's intended
as a reference, and copied from man 3 printf with some extra explanation and
examples.

% - a percent sign. This is required, of course.

Zero or more of the following:

'#' (pound sign): Specifies that the value should be converted
to an 'alternate form.' This has no effect on 'c', 'd', 'i',
'n', 'p', 's', and 'u' conversions.
For 'o' (octal) conversions,
this prepends a '0' to the beginning. For 'x' and 'X' (hexadecimal),
0x or 0X is prepended to the value. For 'e', 'E',
'f', 'g' and 'G',
the value is always printed with a trailing decimal point (.), even
if no numbers follow it. For 'g' and 'G', trailing zeros are not
removed from the result.

'0' (zero): To specify zero-padding on a digit. The converted value
is padded on the left with the specified number of zeros minus the
number of digits to be printed. This is described in more detail
below.

' ' (a space): To specify that a blank should be left before a
positive number.

printf("% d", 7); # prints ' 7'
printf("% d", -7); # prints '-7'

'+' (plus sign): This specifies that a sign always be placed
before the value. '+' overrides ' ' (space) if both are used.

printf(*quot;%+d", 7); # prints '+7'

A decimal digit specifying the minimum field width. Using the '-' modifier
(see above) will left-align the value, otherwise it is right-aligned. With the '0'
modifier for numeric conversions, the value is right-padded with zeros to fill the
field width.

A precision value in the form of a period ('.'), followed by an optional
digit string. If the digit string is omitted, a precision of zero is used.
This specifies the minimum number of digits to print for 'd', 'i', 'o',
'u', 'x', and 'X' conversions. For 'e',
'E', and 'F' conversions, it is
the number of digits to appear after the decimal point. For 'g' and 'G'
conversions, it specifies the maximum number of signifigant digits. For
the 's' (string) conversion, it is the maximum number of characters of the
string to print. Use the latter to make sure long strings don't exceed their field
width.

The character 'h' here will specify to treat the decimal value as a
C type 'short' or 'unsigned short'.

printf("%hd\n", 400000); # prints '6784'

The character 'l' (ell) can be used to treat the value as a C 'long'
type.

The character 'V' will interpret an integer as Perl's standard integer
type.

printf("%Vd", 7); # prints '7'

Finally, the required conversion specifier. Valid conversions are listed
in the previous section.

Leading Zeros

Say you have a number, something like 642, and you want to output it as
00642 instead. The %0nC specifier syntax lets you do just that, where 'n'
is the field width, and C is the conversion specifier you want to use. A
field width is the minimum (in this case) number of characters the value
should fill. Any less than that, and the remainder is filled by prepending
zeros on your value until it fits perfectly.

printf("%05d", 642); # outputs '00642'

You should note that certain conversions, like %f, are a little trickier.
Floating point numbers (with %f) are always outputted with 6 places after
the decimal point, unless you specify a precision with the '.' modifier
(see below for a discussion of the '.' precision modifier). In
other words, printing a value of '2' as %f will actually output as
2.000000. This means you have to take into account, when specifying the
field width, that there are already 7 characters tacked on. To get the value
of 2 to print with one leading zero, you have to use a field width of 9 (7
for the '.' and 6 zeros, 1 for the '2', and 1 for the leading zero).

All other specifiers act in this way, too. To find out how many characters
are output by default for a specifier, output a value of 0 (zero) for it and
count how many characters there are:

Which should tell you there are 12 characters for 0 in scientific
notation.

Padding with spaces

This is more or less the same as leading zeros, except it uses leading
(or, if told, trailing) spaces to complete the field width. This is useful
for lining up multiple lines of data into a report, for instance (though in
that case, you may also want to specify a maximum field width to truncate
long values - more on that below). The syntax is just like leading zeros,
but drop the leading zero:

printf("%6s", 'foo'); # prints ' foo'

By default, leading spaces are used, so values appear to be right-aligned
in their field. To reverse this, put a '-' sign before the field width:

printf("%-6s", 'foo'); # prints 'foo '

For numeric values with default precision, like %f and %e, act here just like
they do with leading zeros. %f, for example, won't have any padding unless you
put a field width of more than 8.

Precision Modifier

The precision modifier tells printf() how many digits you want after
the decimal point, if its a floating point specifier. If there are
more digits than you specified, the value is rounded. If there are less,
zeros are used to fill the space.

For decimal values, the precision modifier has the same effect
as the '0' modifier described above:

printf("%.3f", 7); # prints 007

For string values, it has the nice effect of specifying a maximum
field width, where it will only print out the first n characters
of the string. In combonation with the field width modifier described
above, you can have a well-behaved-under-all-circumstances string field.

%d specifies to convert the given value to a decimal integer. The
conversion rounds as necessary. %.4f specifies to convert the value
given to a floating point number with a precision to 4 decimal places, rounding
the value as needed.

Octal and Hexadecimal

You can convert your decimal based values to Hexadecimal and Octal values using printf() and sprintf(). To do so, specify the conversion as %o for octal, and %x for hexadecimal. %X is equivilant to %x, except the result is printed using capital letters.

As explained in the Format Modifiers section, using a '#' (pound sign) right after the % will convert the value to "alternate form." For %x and %X, it will prepend to the value '0x' and '0X' respectively. For %o, a single leading '0' (zero) is added. The extra characters using the # modifier are considered part of the field width.

In the last example, the field width of 4 is specified. Since the # modifier adds two extra characters to the value, it ends up taking 3 characters in total. Thus the single leading space.

When to Use, and When Not to

While printf() is more powerful than print(), it is also less efficient
and more error-prone. The printf manpage tells us not to use printf()
where a simple print() would suffice, and that's good advice. You should
use printf where you need the control over a value's format that printf()
gives you, when you want to use field widths (and don't care much for
perl's reports), and when you want to round floating point numbers.