Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

perlfunc:sprintf

by gods
on Aug 24, 1999 at 22:43 UTC ( #297=perlfunc: print w/ replies, xml ) Need Help??

sprintf

See the current Perl documentation for sprintf.

Here is our local, out-dated (pre-5.6) version:


sprintf - formatted print into a string



sprintf FORMAT, LIST



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

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.

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

   %%   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

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

   %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 

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

   %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

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

   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"

There is also one Perl-specific flag:

   V       interpret integer as Perl's standard integer type

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.

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 the perllocale manpage.


Log In?
Username:
Password:

What's my password?
Create A New User
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (15)
As of 2014-09-02 17:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite cookbook is:










    Results (26 votes), past polls