Generic Formatting

Jun 18, 2009 Author: LinuxAdmin

If you are not handling numbers or currency values, you can use the printf() family
of functions to perform arbitrary formatting of a value. All the functions in this
group performin an essentially identical way: they take an input string that specifies
the output format and one or more values. The only difference is in the way they return
their results: the “plain” printf() function simply writes it to the script’s output,
while other variants may return it (sprintf()), write it out to a file (fprintf()), and
so on.
The formatting string usually contains a combination of literal text—that is copied
directly into the function’s output—and specifiers that determine how the input
should be formatted. The specifiers are then used to format each input parameter
in the order in which they are passed to the function (thus, the first specifier is used
to format the first data parameter, the second specified is used to format the second
parameter, and so on).
A formatting specifier always starts with a percent symbol (if you want to insert a
literal percent character in your output, you need to escape it as %%) and is followed by a type specification token, which identifies the type of formatting to be applied; a
number of optional modifiers can be inserted between the two to affect the output:

  • A sign specifier (a plus of minus symbol) to determine how signed numbers areto be rendered
  •  A padding specifier that indicates what character should be used to make up the required output length, should the input not be long enough on its own
  •  An alignment specifier that indicates if the output should be left or right aligned
  • A numeric width specifier that indicates theminimumlength of the output
  • A precision specifier that indicates how many decimal digits should be displayed for floating-point numbers

It is important that you be familiarwith some of the most commonly-used type specifiers:


b Output an integer as a Binary number.
c Output the character which has the input integer as its ASCII value.
d Output a signed decimal number
e Output a number using scientific notation (e.g., 3.8e+9)
u Output an unsigned decimal number
f Output a locale aware float number
F Output a non-locale aware float number
o Output a number using its Octal representation
s Output a string
x Output a number as hexadecimal with lowercase letters
X Output a number as hexadecimal with uppercase letters

 

Here are some simple examples of printf() usage:

$n = 123;
$f = 123.45;
$s = "A string";
printf ("%d", $n); // prints 123
printf ("%d", $f); // prints 1
// Prints "The string is A string"printf ("The string is %s", $s);
// Example with precision printf ("%3.3f", $f); // prints 123.450
// Complex formatting 
function show Error($msg, $line, $file)
{return sprintf("An error occured in %s on "."line %d: %s", $file, $line, $msg);}
showError ("Invalid deconfibulator", __LINE__, __FILE__);


views 3340
  1. Add New Comment