php - 显示2位小数的数字
将PHP字符串舍入到2位小数的正确方法是什么?
$number = "520"; // It's a string from a DB
$formatted_number = round_to_2dp($number);
echo $formatted_number;
输出应为round_to_2dp();
round_to_2dp()函数定义应该如何?
20个解决方案
847 votes
你可以使用number_format():
return number_format((float)$number, 2, '.', '');
例:
$foo = "105";
echo number_format((float)$foo, 2, '.', ''); // Outputs -> 105.00
此函数返回一个字符串。
Codemwnci answered 2019-01-21T18:05:39Z
167 votes
或者,
$padded = sprintf('%0.2f', $unpadded); // 520 -> 520.00
Marc B answered 2019-01-21T18:05:58Z
143 votes
使用round()(如果您只期望浮点格式的数字,请使用,否则使用number_format()作为Codemwnci给出的答案):
echo round(520.34345,2); // 520.34
echo round(520, 2); // 520
从手册:
描述:
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
?>
将舍入值<?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
?>返回到指定的precision(小数点后的位数)。 精度也可以是负数或零(默认值)。
...
示例#1 <?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
?>示例
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
?>
示例#2模式示例
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
?>
Somnath Muluk answered 2019-01-21T18:07:05Z
21 votes
尝试:
$number = 1234545454;
echo $english_format_number = number_format($number, 2);
输出将是:
1,234,545,454.00
Biju answered 2019-01-21T18:07:25Z
20 votes
[http://php.net/manual/en/function.round.php]
例如
echo round(5.045, 2); // 5.05
echo round(5.055, 2); // 5.06
SystemX17 answered 2019-01-21T18:07:51Z
9 votes
你可以使用php printf或ex: sprintf("%.3f",$num);功能:
printf的示例:
$num = 2.12;
echo sprintf("%.3f",$num);
您也可以在没有printf的情况下运行,ex: sprintf("%.3f",$num);
输出:
2.120
或者,使用printf:
echo printf("%.2f",$num);
输出:
2.124
Aditya P Bhatt answered 2019-01-21T18:08:36Z
8 votes
使用PHP number_format()函数。
Shamim Hafiz answered 2019-01-21T18:08:58Z
7 votes
解决此问题的另一种更奇特的方法是使用0,其值为$ right_operand 0的虚拟值。
$formatted_number = bcadd($number, 0, 2);
powtac answered 2019-01-21T18:09:22Z
6 votes
number_format($number,2);是用户定义的函数,除非您发布了该函数的声明,否则无法执行任何操作
但是,我的猜测是这样做的number_format($number,2);
ajreal answered 2019-01-21T18:09:51Z
6 votes
使用PHP number_format()函数。例如
$num = 7234545423;
echo number_format($num, 2);
输出将是:
7,234,545,423.00
Sani Kamal answered 2019-01-21T18:10:21Z
5 votes
我自己做。
$decimals = 2;
$number = 221.12345;
$number = $number * pow(10,$decimals);
$number = intval($number);
$number = $number / pow(10,$decimals);
joanlgr answered 2019-01-21T18:10:43Z
5 votes
bcdiv($number, 1, 2) //2 varies for digits after decimal
这将在十进制后正好显示两位数。
优点:如果要仅在浮点值后显示两位数而不是int,则使用此值。
Wasim A. answered 2019-01-21T18:11:12Z
5 votes
对于有条件的舍入,即。 显示小数,它确实需要,否则整数
123.56 =&gt;12.56
123.00 =&gt;123
$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
Viney answered 2019-01-21T18:11:47Z
4 votes
$number = sprintf('%0.2f', $numbers); // 520.89898989 -> 520.89
这将在小数点后给你2个数字。
S'copion Sam answered 2019-01-21T18:12:09Z
4 votes
$retailPrice = 5.989;
echo number_format(floor($retailPrice*100)/100,2, '.', '');
如果没有舍入数字,它将返回5.98。
mrphpguru answered 2019-01-21T18:12:33Z
3 votes
如果要在整个项目中使用2位十进制数,则可以定义
bcscale(2);
然后以下函数将产生您想要的结果
$myvalue=10.165445;
echo bcadd(0,$myvalue);
//result=10.11
但是如果你不使用bcscale函数,你需要编写如下代码来获得你想要的结果
$myvalue=10.165445;
echo bcadd(0,$myvalue,2);
//result=10.11
了解更多
[http://php.net/manual/en/ref.bc.php]
[http://php.net/manual/en/function.bcscale.php]
jewelhuq answered 2019-01-21T18:13:37Z
2 votes
$twoDecNum = sprintf('%0.2f', round($number, 2));
舍入正确地舍入数字,并且sprintf强制它到2位小数,如果它在舍入后恰好只有1位小数。
JohnS answered 2019-01-21T18:13:59Z
1 votes
没有回合的数字
$double = '21.188624';
echo intval($double).'.'.substr(end(explode('.',$double)),0,2);
Abhishek Sharma answered 2019-01-21T18:14:21Z
1 votes
在这里我得到2位小数。(点)使用函数..
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
Mr. HK answered 2019-01-21T18:15:01Z
-3 votes
您可以使用PHP round()函数。
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
Sadikhasan answered 2019-01-21T18:15:23Z