- 格式化数字为带有千位分隔符的形式:
$number = 1234567.89; $formatted_number = number_format($number); echo $formatted_number; // 输出:1,234,567.89
- 设置小数点后的位数:
$number = 1234.56789; $formatted_number = number_format($number, 2); echo $formatted_number; // 输出:1,234.57
- 自定义千位分隔符和小数点符号:
$number = 1234.56789; $formatted_number = number_format($number, 2, ',', '.'); echo $formatted_number; // 输出:1.234,57
- 处理负数:
$number = -1234.56789; $formatted_number = number_format($number, 2); echo $formatted_number; // 输出:-1,234.57
- 禁用千位分隔符:
$number = 1234567.89; $formatted_number = number_format($number, 2, '.', ''); echo $formatted_number; // 输出:1234567.89
- 将科学计数法转换为普通数字格式:
$number = 1.2e6; $formatted_number = number_format($number, 0); echo $formatted_number; // 输出:1,200,000
这些是一些常见的使用技巧,根据具体需求,可以灵活运用number_format函数来格式化数字。