Perl 5 version 6.2 documentation


  • sprintf FORMAT, LIST

    Returns a string formatted by the usual printf conventions of the C library function sprintf. See below for more details and see sprintf(3) or printf(3) on your system for an explanation of the general principles.

    For example:

    1. # Format number with up to 8 leading zeroes
    2. $result = sprintf("%08d", $number);
    3. # Round number to 3 digits after decimal point
    4. $rounded = sprintf("%.3f", $number);

    Perl does its own sprintf formatting--it emulates the C function 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 sprintf are not available from Perl.

    Unlike printf, sprintf does not do what you probably mean when you pass it an array as your first argument. The array is given scalar context, and instead of using the 0th element of the array as the format, Perl will use the count of elements in the array as the format, which is almost never useful.

    Perl's sprintf permits the following universally-known conversions:

    1. %% a percent sign
    2. %c a character with the given number
    3. %s a string
    4. %d a signed integer, in decimal
    5. %u an unsigned integer, in decimal
    6. %o an unsigned integer, in octal
    7. %x an unsigned integer, in hexadecimal
    8. %e a floating-point number, in scientific notation
    9. %f a floating-point number, in fixed decimal notation
    10. %g a floating-point number, in %e or %f notation

    In addition, Perl permits the following widely-supported conversions:

    1. %X like %x, but using upper-case letters
    2. %E like %e, but using an upper-case "E"
    3. %G like %g, but with an upper-case "E" (if applicable)
    4. %b an unsigned integer, in binary
    5. %p a pointer (outputs the Perl value's address in hexadecimal)
    6. %n special: *stores* the number of characters output so far
    7. into the next variable in the parameter list

    Finally, for backward (and we do mean "backward") compatibility, Perl permits these unnecessary but widely-supported conversions:

    1. %i a synonym for %d
    2. %D a synonym for %ld
    3. %U a synonym for %lu
    4. %O a synonym for %lo
    5. %F a synonym for %f

    Note that the number of exponent digits in the scientific notation by %e , %E , %g and %G for numbers with the modulus of the exponent less than 100 is system-dependent: it may be three or less (zero-padded as necessary). In other words, 1.23 times ten to the 99th may be either "1.23e99" or "1.23e099".

    Perl permits the following universally-known flags between the % and the conversion letter:

    1. space prefix positive number with a space
    2. + prefix positive number with a plus sign
    3. - left-justify within the field
    4. 0 use zeros, not spaces, to right-justify
    5. # prefix non-zero octal with "0", non-zero hex with "0x"
    6. number minimum field width
    7. .number "precision": digits after decimal point for
    8. floating-point, max length for string, minimum length
    9. for integer
    10. l interpret integer as C type "long" or "unsigned long"
    11. h interpret integer as C type "short" or "unsigned short"
    12. If no flags, interpret integer as C type "int" or "unsigned"

    There are also two Perl-specific flags:

    1. V interpret integer as Perl's standard integer type
    2. v interpret string as a vector of integers, output as
    3. numbers separated either by dots, or by an arbitrary
    4. string received from the argument list when the flag
    5. is preceded by C<*>

    Where a number would appear in the flags, an asterisk (* ) 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 * is negative, it has the same effect as the - flag: left-justification.

    The v flag is useful for displaying ordinal values of characters in arbitrary strings:

    1. printf "version is v%vd\n", $^V; # Perl's version
    2. printf "address is %*vX\n", ":", $addr; # IPv6 address
    3. printf "bits are %*vb\n", " ", $bits; # random bitstring

    If use locale is in effect, the character used for the decimal point in formatted real numbers is affected by the LC_NUMERIC locale. See perllocale.

    If Perl understands "quads" (64-bit integers) (this requires either that the platform natively support quads or that Perl be specifically compiled to support quads), the characters

    1. d u o x X b i D U O

    print quads, and they may optionally be preceded by

    1. ll L q

    For example

    1. %lld %16LX %qo

    You can find out whether your Perl supports quads via Config:

    1. use Config;
    2. ($Config{use64bitint} eq 'define' || $Config{longsize} == 8) &&
    3. print "quads\n";

    If Perl understands "long doubles" (this requires that the platform support long doubles), the flags

    1. e f g E F G

    may optionally be preceded by

    1. ll L

    For example

    1. %llf %Lg

    You can find out whether your Perl supports long doubles via Config:

    1. use Config;
    2. $Config{d_longdbl} eq 'define' && print "long doubles\n";