myAdBanner

2013年10月28日 星期一

數字前面補0 使用sprintf

* %% - 返回百分比符號
* %b - 二進位數字
* %c - 依照 ASCII 值的字元
* %d - 帶符號十進位數字
* %e - 可續計數法(比如 1.5e+3)
* %u - 無符號十進位數字
* %f - 浮點數(local settings aware)
* %F - 浮點數(not local settings aware)
* %o - 八進位數
* %s - 字串
* %x - 十六進位數(小寫字母)
* %X - 十六進位數(大寫字母)

< ?php
$number = 123;
$txt = sprintf("With 2 decimals: %1\$.2f
With no decimals: %1\$u",$number);
echo $txt."<br>";
echo sprintf("%05d", 1); 
?>
輸出: With 2 decimals: 123.00 With no decimals: 123 00001