[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

RE: [MacPerl] sprintf cheat sheet



Here's a printf quick reference card, converted to plain
text, that I created for a Perl class that I taught a few
years ago. It's certainly not the missing tutorial
description, but it does mention that "width" is a minimum
size, which was one of the original issues of this thread.  
Use a fixed-width font such as Courier to make the columns
line up.

    -Paul-

<snip>

Perl printf Format Specifiers
-----------------------------

All specifiers are in this form, where square
brackets indicate optional parts:

  %[flag][width][.prec]type


[flag]   What flag specifies
-------  ------------------------------------
 none    Right-justify; pad with 0 or blank
           on left
   -     Left-justify; pad spaces on right
   +     Always begin with + or -
 blank   Print sign for negative values only

   #     Use alternate form for these types
          c,s,d,i,u  (no effect)
          o        Prepend 0 to nonzero value
          x or X   Prepend 0x or 0X
          e, E, f  Always use decimal point
          g or G   Don't strip trailing zeros


[width]   Effect on Output
--------  -----------------------------------
  n       At least n characters, blank-padded
  0n      At least n characters, zero-padded


[.prec]   Effect on Output
--------  -----------------------------------
  none    Default precision
  .n      s         At most n characters
  .n      e,E,f,g,G Digits to right of point
  .0      e,E,f     No digits right of point
                       and no point unless #


type   Format of Output
-----  ------------------------------------
  d    signed decimal integer
  o    unsigned octal integer
  u    unsigned decimal integer

  x    unsigned  hexadecimal integer (a-f)
  X    unsigned  hexadecimal integer (A-F)

  f    Floating point [-]dddd.ddd
  e    Floating point [-]d.ddd e [+/-]ddd
  E    Floating point [-]d.ddd E [+/-]ddd
  g    e or f, whichever uses less space
  G    E or f, whichever uses less space

  c    Single character
  s    Character string
  %    The % character, literally

# ===== Want to unsubscribe from this list?
# ===== Send mail with body "unsubscribe" to macperl-request@macperl.org