PHP echo,print,printf,sprintf函数之间的区别与用法详解(2)


<?php 
$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 
?>  

The printout of this program would be:  
[monkey] 
[    monkey] 
[monkey    ] 
[0000monkey] 
[####monkey] 
[many monke]


例3:zero-padded integers

复制代码 代码如下:


<?php 
$isodate = sprintf("%04d-%02d-%02d", $year, $month, $day); 
?> 


例4:formatting currency

复制代码 代码如下:


<?php 
$money1 = 68.75; 
$money2 = 54.35; 
$money = $money1 + $money2; 
// echo $money will output "123.1"; 
$formatted = sprintf("%01.2f", $money); 
// echo $formatted will output "123.10" 
?>


例5: sprintf() : scientific notation

复制代码 代码如下:


<?php 
$number = 362525200; 

echo sprintf("%.3e", $number); // outputs 3.63e+8 
?> 

您可能感兴趣的文章:

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/d1fd86f186665e1f884e2b856ff2a359.html