printf,sprintf,vsprintf的区别及用法

printf,sprintf,vsprintf的区别及用法

返回格式化字符串
string sprintf ( string $format [, mixed $args [, mixed $... ]] )

格式化字符串并打印
int printf ( string $format [, mixed $args [, mixed $... ]] )
依据 format 格式参数产生输出,返回输出字符串的长度。

返回格式化字符串
string vsprintf ( string $format , array $args )

printf打印结果。
sprintf和vsprintf效果一样,区别在于后者参数为数组


关于$format

Type		Specifiers
string		s
integer		d, u, c, o, x, X, b
double		g, G, e, E, f, F

$format = 'There are %d monkeys in the %s';

echo sprintf("%'.9d\n", 123);	// ......123
echo sprintf("%'.09d\n", 123);	// 000000123


$s = 'monkey';
$t = 'many monkeys';

printf("[%s]\n",      $s); // standard string output
printf("[%10s]\n",    $s); // right-justification with spaces
printf("[%-10s]\n",   $s); // left-justification with spaces
printf("[%010s]\n",   $s); // zero-padding works on strings too
printf("[%'#10s]\n",  $s); // use the custom padding character '#'
printf("[%10.10s]\n", $t); // left-justification but with a cutoff of 10 characters

[monkey]
[    monkey]
[monkey    ]
[0000monkey]
[####monkey]
[many monke]

四舍五入,保留两位小数
sprintf("%.2f", $num);

你可能感兴趣的:(PHP)