PHP printf ()

Source: Internet
Author: User
Tags clear screen
The printf () function is a formatted output function that is typically used to output information to a standard output device in a prescribed format. This function is often used when writing programs. The prototype of the function is:
int printf (const char *format, ...);
The function return value is an integer type. If successful, returns the number of characters in the output, and returns a negative output error.
The format for calling the printf () function is:
printf ("< formatted string >", < Parameter table >);
Where the formatted string consists of two parts: some are normal characters, and the characters are output as-is; The other part is the formatting of the specified character, starting with "%", followed by one or more specified characters, used to determine the output content format.
The parameter table is a series of parameters that need to be output, the number must be as many as the number of output parameters as indicated by the formatted string, the parameters are separated by "," and the order one by one corresponds, otherwise unexpected errors will occur.
1. Formatting the specified character
The format specifier provided by the Turbo C2.0 is as follows:

A symbolic effect

%d decimal signed integer
%u decimal unsigned integer
%f floating Point
%s string
%c single character
%p the value of the pointer
Floating-point numbers in the form of%e indices
%x,%x unsigned integer in hexadecimal notation
%o unsigned integer represented in octal
%g automatic selection of appropriate representations

Description

(1). You can insert a number between "%" and a letter to indicate the maximum field width.

For example:%3d represents the output of 3-bit integers, not enough 3-bit right-justified.

The%9.2f represents a floating-point number with an output field width of 9, where the decimal bit is 2 and the integer bit is 6.

One decimal point, not enough 9-bit right-aligned.

The%8s represents the output of a 8-character string, which is not aligns aligned to 8 characters.

If the length of a string, or the number of integers exceeds the width of the description, it is output at its actual length.

But for floating-point numbers, if the integer number of bits exceeds the width of the indicated integer digits, it will be output according to the actual integer digits;

If the fractional number of digits exceeds the indicated decimal width, the output is rounded by the width of the description.

In addition, if you want to add some 0 before the output value, you should add a 0 before the field width.

For example,%04d means that when you output a value that is less than 4 bits, it will be preceded by 0 to make its total width 4 bits.

If you use floating-point numbers to represent the output format of a character or integer, the number after the decimal point represents the maximum width, and the number before the decimal point represents the minimum width.

For example,%6.9s indicates that a string with a length of not less than 6 and not greater than 9 is displayed. If it is greater than 9, the contents of the 9th character will be deleted.

(2). You can add a lowercase letter L between "%" and the letter, indicating that the output is a long form.

For example,%ld indicates an output long integer

%LF indicates the output double floating-point number

(3). You can control the output left or right alignment, that is, "%" and the letter by adding a "-" sign to indicate that the output is left-justified, otherwise it is right-justified.

Example:%-7d for output 7-bit integers left justified

%-10s indicates output 10 characters aligns aligned

2. Some special provisions of the character

B-Character Action

\ nthe line break
\f Clear screen and change page
\ r Enter
\ t Tab character
\XHH represents an ASCII code with 16-in notation,
where HH is 1 to 2 x 16 binary number
The printf () function learned in this section, combined with the data types learned in the previous section, compiles the following program to deepen understanding of the turbo C2.0 data types.

The application of printf () and sprintf () in PHP
<?php
printf ("$%01.2f", 43.2); Running Result: $43.20
echo "
";
printf ("%d bottles of beer on%s", "the Wall");
echo "
";
Operation result: Bottles of beer on the wall
printf ("%15s", "some text"); Run Result: some text
?>
<?php
echo "
";
printf ("The%2\ $s likes to%1\ $s", 111, dog);
echo "
";
Running result: the dog likes to bark
printf ("The%1\ $s says:%2\ $s,%2\ $s.", "dog", "bark");
Running result: the dog says:bark, bark.
?>
<?php
echo "
";
$var 1 = 68.75;
$var 2 = 54.35;
$var 3 = $var 1 + $var 2;
echo $var 3;
echo "
";
The variable $var 3 value is "123.1";
$formatted = sprintf ("%01.2f", $var 3);
echo "
";
Echo $formatted;
Variable $var3 value is "123.10"
?>
<?php
echo "
";
$money = 1.4;
$formatted = sprintf ("%-01.2f", $money);
Echo $formatted;
?>
Results:
$43.20
From bottles of beer on the wall
Some text
The dog likes to 111
The dog Says:bark, bark.
123.1
123.10
1.40

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.