PHP字符串四舍五入到小数点后两位的正确方法是什么?

$number = "520"; // It's a string from a database

$formatted_number = round_to_2dp($number);

echo $formatted_number;

输出应该是520.00;

round_to_2dp()函数应该如何定义?


当前回答

使用round(yourValue,decimalPoint) (php手册的页面)或number_format(yourValue,decimalPoint);

Number_format()返回类似12344.67类型的字符串。所以在这种情况下,你不能用它来做加法或任何计算。如果你尝试,那么你必须处理数字格式错误…

在这种情况下,轮(121222.299000000,2)将是更好的选择。 结果是121222.29…

其他回答

下面是strtok和str_pad的另一个解决方案:

$num = 520.00
strtok(round($num, 2), '.') . '.' . str_pad(strtok('.'), 2, '0')

这里我得到两个小数。(点)使用函数…

function truncate_number($number, $precision = 2) {

    // Zero causes issues, and no need to truncate
    if (0 == (int)$number) {
        return $number;
    }

    // Are we negative?
    $negative = $number / abs($number);

    // Cast the number to a positive to solve rounding
    $number = abs($number);

    // Calculate precision number for dividing / multiplying
    $precision = pow(10, $precision);

    // Run the math, re-applying the negative value to ensure
    // returns correctly negative / positive
    return floor( $number * $precision ) / $precision * $negative;
}

上述函数的结果:

echo truncate_number(2.56789, 1); // 2.5
echo truncate_number(2.56789);    // 2.56
echo truncate_number(2.56789, 3); // 2.567

echo truncate_number(-2.56789, 1); // -2.5
echo truncate_number(-2.56789);    // -2.56
echo truncate_number(-2.56789, 3); // -2.567

新的正确答案

使用PHP本地函数bcdiv

echo bcdiv(2.56789, 1, 1);  // 2.5
echo bcdiv(2.56789, 1, 2);  // 2.56
echo bcdiv(2.56789, 1, 3);  // 2.567
echo bcdiv(-2.56789, 1, 1); // -2.5
echo bcdiv(-2.56789, 1, 2); // -2.56
echo bcdiv(-2.56789, 1, 3); // -2.567

你可以使用number_format():

return number_format((float)$number, 2, '.', '');

例子:

$foo = "105";
echo number_format((float)$foo, 2, '.', '');  // Outputs -> 105.00

这个函数返回一个字符串。

你可以使用PHP printf或sprintf函数:

sprintf的示例:

$num = 2.12;
echo sprintf("%.3f", $num);

您也可以在没有echo的情况下运行相同的程序。例如:sprintf(“%。3 f”,num美元);

输出:

2.120

或者,使用printf:

echo printf("%.2f", $num);

输出:

2.124

使用round()(如果你只期望浮点格式的数字,则使用round(),否则使用number_format()作为Codemwnci给出的答案):

echo round(520.34345, 2);   // 520.34
echo round(520.3, 2);       // 520.3
echo round(520, 2);         // 520

摘自手册:

描述: float round(float $val [, int $precision = 0 [, int $mode = PHP_ROUND_HALF_UP]]); 返回val的四舍五入值到指定精度(小数点后的位数)。精度也可以为负或零(默认值)。

...

Example #1 round() examples <?php echo round(3.4); // 3 echo round(3.5); // 4 echo round(3.6); // 4 echo round(3.6, 0); // 4 echo round(1.95583, 2); // 1.96 echo round(1241757, -3); // 1242000 echo round(5.045, 2); // 5.05 echo round(5.055, 2); // 5.06 ?> Example #2 mode examples <?php echo round(9.5, 0, PHP_ROUND_HALF_UP); // 10 echo round(9.5, 0, PHP_ROUND_HALF_DOWN); // 9 echo round(9.5, 0, PHP_ROUND_HALF_EVEN); // 10 echo round(9.5, 0, PHP_ROUND_HALF_ODD); // 9 echo round(8.5, 0, PHP_ROUND_HALF_UP); // 9 echo round(8.5, 0, PHP_ROUND_HALF_DOWN); // 8 echo round(8.5, 0, PHP_ROUND_HALF_EVEN); // 8 echo round(8.5, 0, PHP_ROUND_HALF_ODD); // 9 ?>