php中sprintf與printf函數用法區別,sprintfprintf_PHP教程

來源:互聯網
上載者:User

php中sprintf與printf函數用法區別,sprintfprintf


下面是一個樣本:四捨五入保留小數點後兩位

"; //輸出 21.00$num2 = 16.3287;echo sprintf("%0.2f",$num2)."
"; //輸出 16.33$num3 = 32.12329;echo sprintf("%0.2f",$num3)."
"; //輸出 32.12 ?>

解釋下 %0.2f 的含義:

% 表示起始字元
0 表示空位用0填滿
2 表示小數點後必須佔兩位
f 表示轉換成浮點數


轉換字元
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
% 印出百分比符號,不轉換。
b 整數轉成二進位。
c 整數轉成對應的 ASCII 字元。
d 整數轉成十進位。
f 倍精確度數字轉成浮點數。
o 整數轉成八進位。
s 整數轉成字串。
x 整數轉成小寫十六進位。
X 整數轉成大寫十六進位。

printf與sprintf的區別

1. printf函數:

int printf ( string format [, mixed args [, mixed ...]] )

Produces output according to format , which is described in the documentation for sprintf() .

Returns the length of the outputted string.

把文字格式化以後輸出,如:

$name="hunte"; $age=25; printf("my name is %s, age %d", $name, $age);

2. sprintf函數:
string sprintf ( string format [, mixed args [, mixed ...]] )

Returns a string produced according to the formatting string format .

跟printf相似,但不列印,而是返回格式化後的文字,其他的與printf一樣。


3. print函數:

是函數,可以返回一個值,只能有一個參數。

int print ( string arg )

Outputs arg . Returns 1 , always.

http://www.bkjia.com/PHPjc/1089038.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/1089038.htmlTechArticlephp中sprintf與printf函數用法區別,sprintfprintf 下面是一個樣本:四捨五入保留小數點後兩位 ?php$num1 = 21;echo sprintf("%0.2f",$num1)."br /"; //輸出 21...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.