php number_format函數(shù)的用法是什么

PHP
小億
76
2023-11-01 22:14:00

number_format函數(shù)是用來格式化數(shù)字的函數(shù),它可以將一個(gè)數(shù)字格式化為帶有千位分隔符、小數(shù)點(diǎn)和小數(shù)位數(shù)的字符串。

函數(shù)的語(yǔ)法是:number_format(number, decimals, decimalpoint, separator)

參數(shù)說明:

  • number:要格式化的數(shù)字
  • decimals:可選參數(shù),指定小數(shù)位數(shù),默認(rèn)為0
  • decimalpoint:可選參數(shù),指定小數(shù)點(diǎn)符號(hào),默認(rèn)為"."
  • separator:可選參數(shù),指定千位分隔符,默認(rèn)為","

示例代碼:

$number = 12345.6789;
$formatted_number = number_format($number); // 默認(rèn)格式化為12346
$formatted_number = number_format($number, 2); // 格式化為12345.68
$formatted_number = number_format($number, 2, ",", "."); // 格式化為12,345.68

注意:number_format函數(shù)會(huì)對(duì)傳入的參數(shù)進(jìn)行四舍五入操作。

0