将数字添加0
,右侧的零将被删除:
$num1 = 99.00;
$num2 = 99.90;
$num3 = 99.01;
$num4 = 99.012;
$num5 = 99.0123;
$num1 = number_format(99.00, 2, '.', '') + 0;
$num2 = number_format(99.90, 2, '.', '') + 0;
$num3 = number_format(99.01, 2, '.', '') + 0;
$num4 = number_format(99.012, 2, '.', '') + 0;
$num5 = number_format(99.0123, 2, '.', '') + 0;
echo "$num1\n";
echo "$num2\n";
echo "$num3\n";
echo "$num4\n";
echo "$num5\n";
输出:
99
99.9
99.01
99.01
99.01
请在这里尝试。
使用一个function
:
function round2no0(&$num)
{
$num = number_format($num, 2, '.', '') + 0;
}
使用方法:
$num1 = 99.00;
$num2 = 99.90;
$num3 = 99.01;
$num4 = 99.012;
$num5 = 99.0123;
round2no0($num1);
round2no0($num2);
round2no0($num3);
round2no0($num4);
round2no0($num5);
echo "$num1\n";
echo "$num2\n";
echo "$num3\n";
echo "$num4\n";
echo "$num5\n";
function round2no0(&$num)
{
$num = number_format($num, 2, '.', '') + 0;
}
输出:
99
99.9
99.01
99.01
99.01
编辑:
为了处理包含千位分隔符的数字并保留机器格式12345.12
,在number_format
中添加了, '.', ''
参数。
可以在这里进行尝试。
round()
代替number_format()
? - Mark Baker