php格式化数字输出number_format

时间:2023-12-05 19:24:38
<?php
$num = 4999.944444;
$formattedNum = number_format($num).PHP_EOL;
echo $formattedNum;
$formattedNum = number_format($num, 2);
echo $formattedNum;
5,000
4,999.94
<?php
$num = 4999.945444;
$formattedNum = number_format($num).PHP_EOL;
echo $formattedNum;
$formattedNum = number_format($num, 2);
echo $formattedNum;
5,000
4,999.95

由此可见,number_format是4舍5入的策略。

<?php
$num = 4999.944444;
$formattedNum = number_format($num).PHP_EOL;
echo $formattedNum;
$formattedNum = number_format($num, 1);
echo $formattedNum;
5,000
4,999.9
<?php
$num = 4999.954444;
$formattedNum = number_format($num).PHP_EOL;
echo $formattedNum;
$formattedNum = number_format($num, 1);
echo $formattedNum;
5,000
5,000.0

它会将留的小数位后面一位作为四舍五入的标准。