1. 程式人生 > 其它 >ASP.NET網路應用開發課程筆記

ASP.NET網路應用開發課程筆記

PHP程式碼

<?php   
    $var=sprintf("%04d", 2);//生成4位數,不足前面補0   
    echo $var;//結果為0002   
?> 

PHP字串sprintf()函式

語法

sprintf(format,arg1,arg2,arg++)
引數描述
format 必需。轉換格式。
arg1 必需。規定插到 format 字串中第一個 % 符號處的引數。
arg2 可選。規定插到 format 字串中第二個 % 符號處的引數。
arg++ 可選。規定插到 format 字串中第三、四等等 % 符號處的引數。

說明

引數format

是轉換的格式,以百分比符號 ("%") 開始到轉換字元結束。下面的可能的format值:

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

arg1, arg2, ++ 等引數將插入到主字串中的百分號 (%) 符號處。該函式是逐步執行的。在第一個 % 符號中,插入 arg1,在第二個 % 符號處,插入 arg2,依此類推。

PHP程式碼
<?php   
$number = 123;   
$txt = sprintf("%f",$number);   
echo $txt;   
?>  

格式數字 number_format()
例項:

<?php

$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 $number = 1234.5678; // english notation without thousands seperator $english_format_number = number_format($number, 2, '.', ''); // 1234.57 ?>