Let's take a look at an example: the requirement is to generate 4 digits, insufficient front complement 0
<?php
//Generate 4 digits, insufficient front complement 0
$var =sprintf ("%04d", 2);
echo $var//The result is 0002
echo date (' Y_m_d ', Time ()). ' _ '. sprintf (' d ', rand (0,99));
? >
sprintf () function
Does it feel like a C language?
1. Grammar
sprintf (format,arg1,arg2,arg++)
Parameter description
Format required. Conversion format.
Arg1 required. Specify the parameter to be inserted into the first% symbol in the format string.
Arg2 Optional. Specify the parameter to be inserted into the second% symbol in the format string.
arg++ Optional. Specify the parameters to be inserted into the format string at the third to fourth and so% symbols.
2. Notes
The parameter format is the format of the transformation, starting with the percent sign ("%") to the end of the converted character. The following possible format values:
%%-return percent symbol
%b-Binary number
%c-Characters with ASCII values
%d-Signed decimal digits
%e-Continuous counting method (e.g. 1.5e+3)
%u-Unsigned decimal digits
%f-floating-point number (Local settings Aware)
%F-floating-point number (not local settings aware)
%o-Eight binary number
%s-String
%x-16 Number (small letter)
%x-16 Number (capital letter)
Arg1, arg2, + + etc parameters will be inserted into the main string of the percent semicolon (%) Symbol Place. The function is executed incrementally. In the first% symbol, insert arg1, at the second% symbol, insert arg2, and so on.
<?php
$number = 123;
$txt = sprintf ("%f", $number);
echo $txt;
? >
3. Format Digital Number_format ()
<?php
$number = 1234.56;
中文版 notation (default)
$english _format_number = Number_format ($number);
1,235
//French notation
$nombre _format_francais = Number_format ($number, 2, ', ', ', ');
1 234,56
$number = 1234.5678;
中文版 notation without thousands seperator
$english _format_number = Number_format ($number, 2, '. ', ');
1234.57
?>