您好,登錄后才能下訂單哦!
如何使用php number_format函數(shù)?針對這個問題,這篇文章詳細介紹了相對應(yīng)的分析和解答,希望可以幫助更多想解決這個問題的小伙伴找到更簡單易行的方法。
number_format()函數(shù)是PHP中的一個內(nèi)置函數(shù),用于格式化一個包含數(shù)千個分組的數(shù)字。它在成功時返回格式化的數(shù)字,否則在失敗時給出E_WARNING
。
語法:
string number_format ( $number, $decimals, $decimalpoint, $sep )
參數(shù):
該函數(shù)接受上述四個參數(shù),如下所述:
$number
:它是指定要格式化的數(shù)字的必需參數(shù)。如果沒有設(shè)置其他參數(shù),則該數(shù)字將被格式化為不帶小數(shù),并使用逗號(,)作為千位分隔符。必須參數(shù)指定要格式化的數(shù)字。
$decimals
:它是可選參數(shù),用于指定小數(shù)。如果設(shè)置了此參數(shù),則該數(shù)字將使用圓點(.)作為小數(shù)點進行格式化。
$decimalpoint
:它是一個可選參數(shù),用于指定要用于小數(shù)點的字符串。
$sep
:它是可選參數(shù),用于指定用于千位分隔符的字符串。如果給定了該參數(shù),則需要所有其他參數(shù)。
返回值:
成功時返回格式化的數(shù)字,失敗時返回E_WARNING。
number_format()代碼示例1:
<?php $num1 = "999999.49"; // 沒有小數(shù)點參數(shù) echo number_format($num1)."\n"; // 帶小數(shù)點參數(shù) echo number_format($num1, 3)."\n"; $num2 = "9999999.99"; // 沒有小數(shù)點參數(shù) //返回整數(shù)值 echo number_format($num2)."\n"; //帶小數(shù)點參數(shù) echo number_format($num2, 3)."\n"; // 包含所有四個參數(shù) echo number_format("1000000.99", 3, "#", "GGG"); ?>
輸出:
999,999 999,999.490 10,000,000 9,999,999.990 1GGG000GGG000#990
number_format()代碼示例2:
如果傳遞任何東西而不是數(shù)字,它則會給出警告。
<?php $num = "GFG"; echo number_format($num)."\n\n"; echo number_format($num, 3); ?>
輸出:
PHP Warning: number_format() expects parameter 1 to be float, string given in /home/ac476aaecea758334cb8ed146bcbb8f6.php on line 5 PHP Warning: number_format() expects parameter 1 to be float, string given in /home/ac476aaecea758334cb8ed146bcbb8f6.php on line 8
number_format()代碼示例3:
此函數(shù)不接受三個參數(shù),只接受1、2或4個參數(shù)。
<?php $num = 1000000; echo number_format($num, 3, ", "); ?>
輸出:
PHP Warning: Wrong parameter count for number_format() in /home/e426108b066d9a86366249bf7b626d19.php on line 6
關(guān)于如何使用php number_format函數(shù)問題的解答就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關(guān)注億速云行業(yè)資訊頻道了解更多相關(guān)知識。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。