关于php:显示数字到小数点后两位

Show a number to two decimal places

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

1
2
3
4
5
$number ="520"; // It's a string from a database

$formatted_number = round_to_2dp($number);

echo $formatted_number;

输出应为520.00;

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


您可以使用number_format():

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

例:

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

该函数返回一个字符串。


或者,

1
$padded = sprintf('%0.2f', $unpadded); // 520 -> 520.00


使用round()(如果仅希望使用浮点格式的数字,请使用此代码,否则,请使用number_format()作为Codemwnci给出的答案):

1
2
3
echo round(520.34345,2);    // 520.34

echo round(520, 2);         // 520

从手册中:

Description:

1
float round(float $val [, int $precision = 0 [, int $mode = PHP_ROUND_HALF_UP ]]);

Returns the rounded value of val to specified precision (number of digits after the decimal point). precision can also be negative or zero (default).

...

Example #1 round() examples

1
2
3
4
5
6
7
8
9
10
<?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

1
2
3
4
5
6
7
8
9
10
11
<?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
?>


http://php.net/manual/en/function.round.php

例如

1
2
3
echo round(5.045, 2);    // 5.05

echo round(5.055, 2);    // 5.06


尝试:

1
2
$number = 1234545454;
echo  $english_format_number = number_format($number, 2);

输出将是:

1
1,234,545,454.00


您可以使用PHP printfsprintf函数:

sprintf的示例:

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

您也可以在没有echo的情况下运行相同的代码。示例:sprintf("%.3f", $num);

输出:

1
2.120

或者,使用printf

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

输出:

1
2.124

解决此问题的另一种更奇特的方法是使用bcadd()0的$ right_operand虚拟值。

1
$formatted_number = bcadd($number, 0, 2);


使用PHP number_format()函数。

例如,

1
2
$num = 7234545423;
echo number_format($num, 2);

输出将是:

1
7,234,545,423.00

使用PHP number_format()函数。


round_to_2dp是用户定义的函数,除非发布了该函数的声明,否则将无法执行任何操作。

但是,我的猜测是这样做的:number_format($number, 2);


1
bcdiv($number, 1, 2) // 2 varies for digits after the decimal point

这将在小数点后精确显示两位数。

优点:

如果只想在浮点数后显示两位数而不是int,请使用此位。


对于有条件的舍入即。在真正需要的地方显示小数,否则为整数

123.56 => 12.56

123.00 => 123

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
$somenumber = 123.56;

$somenumber = round($somenumber,2);

if($somenumber == intval($somenumber))
{
    $somenumber = intval($somenumber);
}

echo $somenumber; // 123.56


$somenumber = 123.00;

$somenumber = round($somenumber,2);

if($somenumber == intval($somenumber))
{
    $somenumber = intval($somenumber);
}

echo $somenumber; // 123

我自己做。

1
2
3
4
5
$decimals = 2;
$number = 221.12345;
$number = $number * pow(10, $decimals);
$number = intval($number);
$number = $number / pow(10, $decimals);


1
2
$retailPrice = 5.989;
echo number_format(floor($retailPrice*100)/100,2, '.', '');

它将返回5.98,而不四舍五入数字。


1
$number = sprintf('%0.2f', $numbers); // 520.89898989 -> 520.89

小数点后将为您提供2个数字。


1
$twoDecNum = sprintf('%0.2f', round($number, 2));

如果四舍五入后恰好只有1个小数位,则四舍五入会正确舍入该数字,并且sprintf将其强制小数点后两位。


在这里,我使用函数在.(点)后得到两个小数。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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;
}

以上功能的结果:

1
2
3
4
5
6
7
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

1
2
3
4
5
6
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

如果要在整个项目中使用两位十进制数字,则可以定义:

1

然后,以下函数将产生您想要的结果:

1
2
3
$myvalue = 10.165445;
echo bcadd(0, $myvalue);
// result=10.11

但是,如果您不使用bcscale函数,则需要按如下所示编写代码以获得所需的结果。

1
2
3
$myvalue = 10.165445;
echo bcadd(0, $myvalue, 2);
// result=10.11

了解更多

  • BC数学函数

  • bcscale


不带轮数

1
2
$double = '21.188624';
echo intval($double) . '.' . substr(end(explode('.', $double)), 0, 2);


这是使用strtok和str_pad的另一个解决方案:

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

您可以使用PHP round()函数。

1
2
3
4
5
6
7
8
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