-
- //Generate 4 digits, add 0 if necessary
- $var=sprintf("%04d", 2);
- echo $var;//The result is 0002
- echo date(' y_m_d', time()).'_'.sprintf('d', rand(0,99));
- ?>
-
Copy code
sprintf() function
Does it feel like C language?
1. Grammar
sprintf(format,arg1,arg2,arg++)
Parameter Description
format required. Convert format.
arg1 required. Specifies the parameters to be inserted at the first % sign in the format string.
arg2 optional. Specifies the parameter to be inserted into the format string at the second % sign.
arg++ Optional. Specifies the parameters to be inserted into the format string at the third, fourth, etc. % symbols.
2, Description (edited and organized by bbs.it-home.org Script School)
-
- $number = 123;
- $txt = sprintf("%f",$number);
- echo $txt;
- ?>
Copy code
3, Format number number_format()
-
-
$number = 1234.56;
// english notation (default)
- $english_format_number = number_format($number);
- / / 1,235
// french notation
- $nombre_format_francais = number_format($number, 2, ',', ' ');
- // 1 234,56
// english notation without thousands seperator
- $english_format_number = number_format($number, 2, '.', '');
- // 1234.57
- ?>
-
Copy code
|