Php number_format 是 Php 中的函數,用於格式化給定的千位數字。它是 Php 中的內建函數,根據程式設計師在參數中給出的具體要求傳回格式化的數字,否則在失敗時會向使用者發出 E_WARNING。可以用逗號或其他分隔符號分隔千位元。它接受浮點數並傳回以千位分隔的格式化數字的字串,該數字具有四捨五入值或小數值到特定數字。預設情況下,number_format() 函數在向其中傳遞數字時使用 (,) 運算子分隔一千組。它是表示浮點數的簡單且用戶友好的方式之一,用戶很容易理解。
開始您的免費軟體開發課程
網頁開發、程式語言、軟體測試及其他
下面給出了在 Php 中使用 number_format() 函數的基本語法:
string number_format( float $num, int $decimal =0, string $dec_pt = ' . ' , string $thousand_sep = ' , ' )
哪裡,
下面給出了描述 Php 中 number_format() 函數運作原理的一些要點:
程式中描述 Php number_format() 函數運作原理的一些範例如下:
代碼:
<!DOCTYPE html> <html> <body> <?php $f_num = "Number"; //Using only 1 parameter, i.e. number to be formatted echo " The formatted number is ". number_format($f_num)."\n\n"; //Using the decimal point and printing the decimal upto the specific digits (2 parameters) echo number_format($f_num, 5); ?> </body> </html>
輸出:
解釋:在上面的程式碼中,變數‘f_num’中的數字應該是浮點數,但卻是一個字串。因此基本上,字串作為 number_format() 函數中的參數傳遞,這在技術上是不正確的。因此,在控制台上會向使用者顯示語法錯誤的錯誤訊息。
代碼:
<!DOCTYPE html> <html> <body> <?php $f_num = "123.78"; $f_num1 = "67.09"; // With only 1 parameter , i.e. number to be formatted echo number_format($f_num).'<br>'; echo number_format($f_num1).'<br>'; // Using the decimal point (2 parameters) echo number_format($f_num, 5).'<br>'; echo number_format($f_num1, 3); ?> </body> </html>
輸出:
解釋:在上面的程式碼中,使用變數「f_num」和「f_num1」給了兩個浮點數。僅使用數字變數作為 number_format() 函數中的參數,將分離一千個群組。由於兩個給定的數字中都不存在一千個組,因此不存在分離。為了將值列印到特定的小數點,需要傳遞值為 5 和 3 的第二個參數。因此,在輸出中,小數點到給定位數的浮點數將列印在控制台上。
代碼:
<!DOCTYPE html> <html> <body> <?php $f_num = "178923.78"; $f_num1 = "665467.09"; // Using only 1 parameter, i.e. number to be formatted echo number_format($f_num).'<br>'; echo number_format($f_num1).'<br>'; // Using the separators for decimal point and thousand groups(4 parameters) echo number_format($f_num, 5, '.', ',').'<br>'; echo number_format($f_num1, 3, ':', '.'); ?> </body> </html>
輸出:
Explanation: In the above code, a single parameter (number to be formatted) is passed in the function number_format() and a thousand groups in the number given are separated with the (,) operator by default. In order to print the number with the specific separators in thousand groups and decimal points, third and fourth parameters are passed using those separators in the function and printed on the console.
Code:
<!DOCTYPE html> <html> <body> <?php $f_num = "178923.78"; $f_num1 = "665467.09"; //Using the separators for decimal point only(3 parameters) echo number_format($f_num, 5, ",").'<br>'; echo number_format($f_num1, 3, "."); ?> </body> </html>
Output:
Explanation: In the above code, 2 floating numbers are given and only 3 parameters are passed in the number_format() function which is not allowed technically. As the number_format() function either accepts 1, 2 or 4 parameters. So a Warning message is displayed to the user indicating the wrong parameters.
The above description clearly explains what is the number_format() function in Php and how it works to format the floating numbers. This function provides the flexibility to the user to display the output according to the specific requirements by passing the different parameters. It also shows warnings and results in unexpected outputs. So one needs to understand it properly and needs to be very careful before using it in a program.
以上是PHP number_format()的詳細內容。更多資訊請關注PHP中文網其他相關文章!