perlfunc
gods
<P>
sprintf - formatted print into a string
<P>
<HR>
<P>
sprintf
<FONT SIZE=-1>FORMAT,</FONT>
<FONT SIZE=-1>LIST</FONT>
<P>
<HR>
<P>
Returns a string formatted by the usual [perlfunc:printf|printf()] conventions of the
<FONT SIZE=-1>C</FONT> library function [perlfunc:sprintf|sprintf()]. See <EM>sprintf(3)</EM> or <EM>printf(3)</EM>
on your system for an explanation of the general principles.
<P>
Perl does its own [perlfunc:sprintf|sprintf()] formatting -- it emulates the
<FONT SIZE=-1>C</FONT> function [perlfunc:sprintf|sprintf()], but it doesn't use it (except for floating-point numbers, and even then
only the standard modifiers are allowed). As a result, any non-standard
extensions in your local [perlfunc:sprintf|sprintf()] are not available from Perl.
<P>
Perl's [perlfunc:sprintf|sprintf()] permits the following universally-known conversions:
<P>
<PRE> %% a percent sign
%c a character with the given number
%s a string
%d a signed integer, in decimal
%u an unsigned 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 a floating-point number, in %e or %f notation
</PRE>
<P>
In addition, Perl permits the following widely-supported conversions:
<P>
<PRE> %X like %x, but using upper-case letters
%E like %e, but using an upper-case "E"
%G like %g, but with an upper-case "E" (if applicable)
%p a pointer (outputs the Perl value's address in hexadecimal)
%n special: *stores* the number of characters output so far
into the next variable in the parameter list
</PRE>
<P>
Finally, for backward (and we do mean ``backward'') compatibility, Perl
permits these unnecessary but widely-supported conversions:
<P>
<PRE> %i a synonym for %d
%D a synonym for %ld
%U a synonym for %lu
%O a synonym for %lo
%F a synonym for %f
</PRE>
<P>
Perl permits the following universally-known flags between the <CODE>%</CODE>
and the conversion letter:
<P>
<PRE> space prefix positive number with a space
+ prefix positive number with a plus sign
- left-justify within the field
0 use zeros, not spaces, to right-justify
# prefix non-zero octal with "0", non-zero hex with "0x"
number minimum field width
.number "precision": digits after decimal point for
floating-point, max length for string, minimum length
for integer
l interpret integer as C type "long" or "unsigned long"
h interpret integer as C type "short" or "unsigned short"
</PRE>
<P>
There is also one Perl-specific flag:
<P>
<PRE> V interpret integer as Perl's standard integer type
</PRE>
<P>
Where a number would appear in the flags, an asterisk (``<CODE>*</CODE>'') may be used instead, in which case Perl uses the next item in the
parameter list as the given number (that is, as the field width or
precision). If a field width obtained through ``<CODE>*</CODE>'' is negative, it has the same effect as the ``<CODE>-</CODE>'' flag: left-justification.
<P>
If <CODE>use locale</CODE> is in effect, the character used for the decimal point in formatted real numbers is affected by the
<FONT SIZE=-1>LC_NUMERIC</FONT> locale. See
[perlman:perllocale|the perllocale manpage].
<HR>