icon Top 9 categories map      RocketAware > Perl >

sprintf FORMAT, LIST

Tips: Browse or Search all pages for efficient awareness of Perl functions, operators, and FAQs.


Search Perl pages


By activity
Professions, Sciences, Humanities, Business, ...

User Interface
Text-based, GUI, Audio, Video, Keyboards, Mouse, Images,...

Text Strings
Conversions, tests, processing, manipulation,...

Integer, Floating point, Matrix, Statistics, Boolean, ...

Algorithms, Memory, Process control, Debugging, ...

Stored Data
Data storage, Integrity, Encryption, Compression, ...

Networks, protocols, Interprocess, Remote, Client Server, ...

Hard World
Timing, Calendar and Clock, Audio, Video, Printer, Controls...

File System
Management, Filtering, File & Directory access, Viewers, ...

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 all of 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 octal with "0", 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.

Source: Perl builtin functions
Copyright: Larry Wall, et al.
Next: sqrt EXPR

Previous: split /PATTERN/,EXPR,LIMIT

(Corrections, notes, and links courtesy of RocketAware.com)

[Overview Topics]

Up to: String-Non-Integer-String conversions
Up to: NUL Terminated String processing
Up to: String-Integer-String conversions

Rapid-Links: Search | About | Comments | Submit Path: RocketAware > Perl > perlfunc/sprintf.htm
RocketAware.com is a service of Mib Software
Copyright 2000, Forrest J. Cavalier III. All Rights Reserved.
We welcome submissions and comments