php printf()

巴扎黑
Release: 2023-03-01 12:14:01
Original
1855 people have browsed it

The

printf() function is a formatted output function, generally used to output information in a specified format to a standard output device. This function is often used when writing programs. The prototype of the function is:
int printf(const char *format, ...);
The return value of the function is an integer. If successful, the number of characters output is returned. If an error occurs, a negative value is returned.
The calling format of the printf() function is:
printf("", );
The formatted string includes two parts: one part is normal characters, these characters It will be output as is; the other part is the formatting specified characters, starting with "%", followed by one or several specified characters, which are used to determine the format of the output content.
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 described in the format string. Each parameter is separated by "," and the order corresponds one to one, otherwise it will An unexpected error occurred.
1. Format specifiers
Turbo C2.0 provides the following format specifiers:

a symbol function

%d decimal signed integer
%u decimal unsigned integer
%f floating point number
%s string
%c Single character
%p Pointer value
%e Floating point number in exponential form
%x, %X Unsigned integer in hexadecimal
%o Unsigned integer in octal
%g Automatic selection Suitable notation

Explanation:

(1). You can insert numbers between "%" and letters to indicate the maximum field width.

For example: %3d means outputting a 3-digit integer, which is not enough to be right-aligned.

%9.2f represents a floating point number with an output field width of 9, in which the decimal digits are 2, the integer digits are 6,

the decimal point occupies one decimal place, and it is not enough for 9 digits to be right-aligned.

%8s means outputting a string of 8 characters, which is not enough to be right-aligned.

If the length of the string or the number of integer digits exceeds the specified field width, it will be output according to its actual length.

But for floating point numbers, if the number of digits in the integer part exceeds the specified integer digit width, it will be output as the actual integer digits;

If the number of digits in the decimal part exceeds the specified decimal digit width, it will be output by rounding according to the specified width. .

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

For example: %04d means that when outputting a value less than 4 digits, 0 will be added in front to make the total width 4 digits.

If floating point numbers are used to represent the output format of characters or integers, 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 means displaying a string with a length of no less than 6 and no more than 9. If it is greater than 9, the content after the 9th character will be deleted.

(2). You can add a lowercase letter l between "%" and the letter to indicate that the output is a long number.

For example: %ld represents the output of long integer

%lf represents the output of double floating point number

(3). You can control the output to be left or right aligned, that is, add a "-" sign between "%" and the letter. Indicates that the output is left-aligned, otherwise it is right-aligned.

For example: %-7d means outputting a 7-digit integer left-aligned

%-10s means outputting 10 characters left-aligned

2. Some special characters

b Character functions

n Line feed
f Clear the screen and change the page
r Carriage Enter
t Tab symbol
xhh means an ASCII code is expressed in hexadecimal,
where hh is 1 to 2 hexadecimal numbers
The printf() function learned in this section is combined with what was learned in the previous section Data types, compile the following program to deepen your understanding of Turbo C2.0 data types.

Application of printf() and sprintf() in php
printf("$%01.2f", 43.2); // Running result: $43.20
echo "
";
printf("%d bottles of beer on %s", 100, "the wall");
echo "
";
//Run result: 100 bottles of beer on the wall
printf("%15s", "some text"); / /Run result: some text
?>
echo "
";
printf("The %2$s likes to %1$s", 111, dog);
echo "
";
//Run result: The dog likes to bark
printf("The %1$s says: %2$s, %2$s.", "dog", "bark");
//Run result: The dog says: bark, bark.
?>
echo "
";
$var1 = 68.75;
$var2 = 54.35;
$var3 = $var1 + $var2;
echo $var3;
echo "
";
//The value of variable $var3 is "123.1";
$formatted = sprintf ("%01.2f", $var3);
echo "
";
echo $formatted;
// The value of variable $var3 for "123.10"
?>
echo "
";
$money = 1.4;
$formatted = sprintf ("%-01.2f", $money);
echo $formatted;
?> ;
Results:
$43.20
100 bottles of beer on the wall
some text
The dog likes to 111
The dog says: bark, bark.
123.1
123.10
1.40


Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template